我正在使用以下代码扫描二维码并显示它。
主要活动
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bg1);
scan =(Button) findViewById(R.id.scanner);
scan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean isGooglePlayUpdated=isGooglePlayServicesAvailable(BG1.this);
if (isGooglePlayUpdated){
ActivityCompat.requestPermissions(BG1.this, new
String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_WRITE_PERMISSION);
}
else
Toast.makeText(BG1.this,"Please update Google Play Services",Toast.LENGTH_LONG).show();
}
});
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case REQUEST_WRITE_PERMISSION:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
takePicture();
} else {
Toast.makeText(BG1.this, "Permission Denied!", Toast.LENGTH_SHORT).show();
}
}
}
private void takePicture() {
Intent intent = new Intent(BG1.this,scanActivity.class);
startActivityForResult(intent, PHOTO_REQUEST);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PHOTO_REQUEST) {
if (resultCode == RESULT_OK){
if (data!=null){
String qrCode = data.getParcelableExtra(scanActivity.BarcodeObject);
Toast.makeText(BG1.this,qrCode,Toast.LENGTH_LONG).show();
Log.d(TAG, "Barcode read: " + qrCode);
}else {
Toast.makeText(BG1.this,"intent data is null",Toast.LENGTH_LONG).show();
Log.d(TAG, "No barcode captured, intent data is null");
}
}
else {
Toast.makeText(BG1.this,"Result code error ",Toast.LENGTH_LONG).show();
Log.d(TAG, "Result code error");
}
}
else {
}
}
扫描活动.java
public class scanActivity extends AppCompatActivity {
private static String TAG="scan Activity";
SurfaceView cameraView;
BarcodeDetector barcodeDetector;
CameraSource cameraSource;
SurfaceHolder holder;
public static final String BarcodeObject = "Barcode";
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.scan_activity);
cameraView=(SurfaceView) findViewById(R.id.camera_view);
cameraView.setZOrderMediaOverlay(true);
holder=cameraView.getHolder();
barcodeDetector=new BarcodeDetector.Builder(this)
.setBarcodeFormats(Barcode.QR_CODE)
.build();
if (!barcodeDetector.isOperational()){
Toast.makeText(this,"Sorry Can't setup",Toast.LENGTH_LONG).show();
// Check for low storage. If there is low storage, the native library will not be
// downloaded, so detection will not become operational.
IntentFilter lowstorageFilter = new IntentFilter(Intent.ACTION_DEVICE_STORAGE_LOW);
boolean hasLowStorage = registerReceiver(null, lowstorageFilter) != null;
if (hasLowStorage) {
Toast.makeText(this, "Low Storage", Toast.LENGTH_LONG).show();
Log.w(TAG, "Low Storage");
}
this.finish();
}
try {
cameraSource.release();
}catch (NullPointerException e){
Log.e("Camera Realease", "Yes");
Toast.makeText(scanActivity.this,"Camerea Release ",Toast.LENGTH_LONG).show();
e.printStackTrace();
}
cameraSource=new CameraSource.Builder(this,barcodeDetector)
.setFacing(CameraSource.CAMERA_FACING_BACK)
.setRequestedFps(24)
.setAutoFocusEnabled(true)
.setRequestedPreviewSize(1920,1024)
.build();
cameraView.getHolder().addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
try
{
cameraSource.start(cameraView.getHolder());
}catch (Exception i){
Log.e("cameraSource start()", "Yes");
i.printStackTrace();
Toast.makeText(scanActivity.this,"Error while opening the camera",Toast.LENGTH_LONG).show();
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
cameraSource.stop();
}
});
barcodeDetector.setProcessor(new Detector.Processor<Barcode>() {
@Override
public void release() {
}
@Override
public void receiveDetections(Detector.Detections<Barcode> detections) {
SparseArray<Barcode> barcodes = detections.getDetectedItems();
if (barcodes.size()>0){
System.out.println("Barcode "+barcodes.valueAt(0).displayValue);
//Toast.makeText(scanActivity.this,barcodes.valueAt(0).toString(),Toast.LENGTH_LONG);
Intent intent=new Intent();
//storing into global variable
Constants.qrCode=barcodes.valueAt(0).displayValue;
intent.putExtra(BarcodeObject,barcodes.valueAt(0).displayValue);
// //stop the camera
setResult(RESULT_OK,intent);
finish();
}
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
cameraSource=null;
}
}
所以我面临以下错误:
我在扫描活动中获取扫描的二维码,但没有调用 mainActivity.java 中的 onActivityResult() 方法。