使用谷歌视觉QR码阅读器时无法连接到摄像头服务

时间:2018-09-17 06:37:38

标签: android qr-code google-vision android-vision

我在我的应用中使用了Google视觉QR码扫描仪,但出现以下错误

09-17 11:02:22.419 5022-5022/com.gms E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.gms, PID: 5022
java.lang.RuntimeException: Fail to connect to camera service
    at android.hardware.Camera.<init>(Camera.java:519)
    at android.hardware.Camera.open(Camera.java:364)
    at com.google.android.gms.vision.CameraSource.zza(Unknown Source)
    at com.google.android.gms.vision.CameraSource.start(Unknown Source)
    at com.easym.webrtc.GoogleQrCodeActivity$1.surfaceCreated(GoogleQrCodeActivity.java:98)
    at android.view.SurfaceView.updateWindow(SurfaceView.java:656)
    at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:172)
    at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:1013)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2542)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1537)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7183)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:959)
    at android.view.Choreographer.doCallbacks(Choreographer.java:734)
    at android.view.Choreographer.doFrame(Choreographer.java:670)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:945)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6776)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1496)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386)

下面是我的QrcodeActivity,它执行所有与二维码扫描相关的工作

public class GoogleQrCodeActivity extends AppCompatActivity {


private BarcodeDetector barcodeDetector;
private CameraSource cameraSource;

Button btnAction;
String intentData = "";
boolean isEmail = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_google_qr_code);

    surfaceView = findViewById(R.id.surfaceView_qr_code_scanner);

}

private void initialiseDetectorsAndSources() {

    Toast.makeText(getApplicationContext(), "Barcode scanner started", Toast.LENGTH_SHORT).show();

    barcodeDetector = new BarcodeDetector.Builder(this)
            .setBarcodeFormats(Barcode.QR_CODE)
            .build();

    cameraSource = new CameraSource.Builder(this, barcodeDetector)
            .setRequestedPreviewSize(1920, 1080)
            .setAutoFocusEnabled(true) //you should add this feature
            .build();

    surfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            try {
                if (ActivityCompat.checkSelfPermission(GoogleQrCodeActivity.this, android.Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
                        cameraSource.start(surfaceView.getHolder());
                }
                else {
                    ActivityCompat.requestPermissions(GoogleQrCodeActivity.this, new
                            String[]{Manifest.permission.CAMERA}, ConstantsApp.REQUEST_CAMERA_PERMISSION);
                }

            } catch (IOException e) {
                e.printStackTrace();
            }


        }

        @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() {
  Toast.makeText(getApplicationContext(), "To prevent memory leaks barcode scanner has been stopped", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void receiveDetections(Detector.Detections<Barcode> detections) {
            final SparseArray<Barcode> barcodes = detections.getDetectedItems();
            if (barcodes.size() != 0) {


                Intent mIntent =new Intent();
                mIntent.putExtra("barcode",barcodes.valueAt(0));
                setResult(RESULT_OK,mIntent);
                finish();

            }
        }
    });
}


@Override
protected void onPause() {
    super.onPause();
    cameraSource.release();
}

@Override
protected void onResume() {
    super.onResume();
    initialiseDetectorsAndSources();
}

}

我在清单中设置了以下权限

<uses-permission android:name="android.permission.CAMERA"  />
<uses-feature
    android:name="android.hardware.camera"
    android:required="true" />
<uses-feature
    android:name="android.hardware.camera.autofocus"
    android:required="false" /> 

在Samsung设备中扫描时,我主要遇到此错误。 我能够第一次扫描QR码,从第二次开始得到上述错误

我无法弄清楚我的代码到底出了什么问题

0 个答案:

没有答案