Updated on 2026-08-14
This commit is contained in:
parent
0e0075a7e0
commit
62fa6fa689
2 changed files with 68 additions and 87 deletions
|
|
@ -1,87 +0,0 @@
|
|||
package com.tangem.presentation.activity;
|
||||
|
||||
import android.Manifest;
|
||||
import android.app.Activity;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.os.Bundle;
|
||||
import android.support.v4.app.ActivityCompat;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
|
||||
import com.google.zxing.Result;
|
||||
|
||||
import me.dm7.barcodescanner.zxing.ZXingScannerView;
|
||||
|
||||
public class QRScanActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler {
|
||||
|
||||
private ZXingScannerView mScannerView;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
//setContentView(R.layout.activity_qrscan);
|
||||
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
|
||||
// Log.e("QRScanActivity", "User hasn't granted permission to use camera");
|
||||
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, 1);
|
||||
} else {
|
||||
runScanner();
|
||||
}
|
||||
}
|
||||
|
||||
void runScanner() {
|
||||
mScannerView = new ZXingScannerView(this); // Programmatically initialize the scanner view
|
||||
setContentView(mScannerView);
|
||||
mScannerView.setResultHandler(this); // Register ourselves as a handler for scan results.
|
||||
mScannerView.startCamera();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
|
||||
switch (requestCode) {
|
||||
case 1: {
|
||||
// If request is cancelled, the result arrays are empty.
|
||||
if (grantResults.length > 0
|
||||
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
||||
|
||||
// Log.i("QRScanActivity", "permission was granted");
|
||||
// permission was granted, yay! Do the
|
||||
// contacts-related task you need to do.
|
||||
runScanner();
|
||||
|
||||
} else {
|
||||
// Log.e("QRScanActivity", "permission denied");
|
||||
setResult(Activity.RESULT_CANCELED);
|
||||
finish();
|
||||
|
||||
// permission denied, boo! Disable the
|
||||
// functionality that depends on this permission.
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// other 'case' lines to check for other
|
||||
// permissions this app might request
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleResult(Result result) {
|
||||
Intent data = new Intent();
|
||||
data.putExtra("QRCode", result.getText());
|
||||
setResult(Activity.RESULT_OK, data);
|
||||
finish();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPause() {
|
||||
super.onPause();
|
||||
if (mScannerView != null) mScannerView.stopCamera(); // Stop camera on pause
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResume() {
|
||||
super.onResume();
|
||||
if (mScannerView != null) mScannerView.startCamera();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,68 @@
|
|||
package com.tangem.presentation.activity
|
||||
|
||||
import android.Manifest
|
||||
import android.app.Activity
|
||||
import android.content.Intent
|
||||
import android.content.pm.PackageManager
|
||||
import android.os.Bundle
|
||||
import android.support.v4.app.ActivityCompat
|
||||
import android.support.v7.app.AppCompatActivity
|
||||
|
||||
import com.google.zxing.Result
|
||||
|
||||
import me.dm7.barcodescanner.zxing.ZXingScannerView
|
||||
|
||||
class QRScanActivity : AppCompatActivity(), ZXingScannerView.ResultHandler {
|
||||
|
||||
private var scannerView: ZXingScannerView? = null
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED)
|
||||
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.CAMERA), 1)
|
||||
else
|
||||
runScanner()
|
||||
}
|
||||
|
||||
override fun onPause() {
|
||||
super.onPause()
|
||||
if (scannerView != null)
|
||||
scannerView!!.stopCamera()
|
||||
}
|
||||
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
if (scannerView != null)
|
||||
scannerView!!.startCamera()
|
||||
}
|
||||
|
||||
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
|
||||
when (requestCode) {
|
||||
1 -> {
|
||||
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED)
|
||||
runScanner()
|
||||
else {
|
||||
setResult(Activity.RESULT_CANCELED)
|
||||
finish()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun handleResult(result: Result) {
|
||||
val data = Intent()
|
||||
data.putExtra("QRCode", result.text)
|
||||
setResult(Activity.RESULT_OK, data)
|
||||
finish()
|
||||
}
|
||||
|
||||
private fun runScanner() {
|
||||
// programmatically initialize the scanner view
|
||||
scannerView = ZXingScannerView(this)
|
||||
setContentView(scannerView)
|
||||
// register ourselves as a handler for scan results.
|
||||
scannerView!!.setResultHandler(this)
|
||||
scannerView!!.startCamera()
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue