我正在开发一个应用程序,其中使用了Google移动视觉API的条形码扫描器。我应该只能在ScannerClassy.java中扫描一次并将结果保存在MainActivity中。问题是当我扫描条形码时,它会打开MainActivity.java 5-7次。我尝试使用camerasource.release(),但它没有起作用。
ScannerClass.java
import com.example.MashapeHello.R;
import com.google.android.gms.vision.Detector;
import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Looper;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.util.Log;
import android.util.SparseArray;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.TextView;
import com.google.android.gms.vision.CameraSource;
import com.google.android.gms.vision.barcode.Barcode;
import com.google.android.gms.vision.barcode.BarcodeDetector;
import java.io.IOException;
public class ScannerClass extends AppCompatActivity implements Detector.Processor {
private TextView textView;
private SurfaceView surfaceView;
private BarcodeDetector barcodeDetector;
private CameraSource cameraSource;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
surfaceView = (SurfaceView) this.findViewById(R.id.surfaceView);
barcodeDetector = new BarcodeDetector.Builder(this).setBarcodeFormats(Barcode.ALL_FORMATS).build();
barcodeDetector.setProcessor(this);
cameraSource = new CameraSource.Builder(getApplicationContext(), barcodeDetector).
setRequestedPreviewSize(1024,1024).setAutoFocusEnabled(true)
.build();
final Activity activity = this;
surfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
try{
if(ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.CAMERA},1024);
return;
}
cameraSource.start(surfaceView.getHolder());
} catch (IOException ie){
Log.e("Camera start problem", ie.getMessage());
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
cameraSource.stop();
}
});
}
@Override
public void release(){
}
@Override
public void receiveDetections(Detector.Detections detections) {
final SparseArray<Barcode> barcodes = detections.getDetectedItems();
if (barcodes.size() != 0) {
final StringBuilder sb = new StringBuilder();
for (int i = 0; i < barcodes.size(); ++i) {
sb.append(barcodes.valueAt(i).rawValue).append("\n");
}
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("barcodevalue", sb.toString());
startActivity(intent);
cameraSource.release();
}
}
}