OPPO CPH1923 设备使用 barcodeDetector 扫描二维码

Scanning QR Codes using barcodeDetector by OPPO CPH1923 device

我有一个扫描 QR Codes 的项目。使用 Samsung mobiles 效果很好,但我无法使用 OPPO CPH1923 device 扫描 QR Codes 。我进行了调试,发现调试器没有进入 receiveDetections 覆盖功能。我不知道为什么这个设备会这样,我在浪费时间。

我用Google mobile vision API

Note : I don't have a sim card in the mobile

这是我的代码

barcodeDetector = new BarcodeDetector.Builder(MainActivity.this)
                .setBarcodeFormats(Barcode.ALL_FORMATS)
                .build();
        cameraSource = new CameraSource.Builder(MainActivity.this,barcodeDetector)
                .setRequestedPreviewSize(640,480)
                .setAutoFocusEnabled(true)
                .build();


        surface.getHolder().addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                try {
                    if(ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CAMERA)!= PackageManager.PERMISSION_GRANTED){
                        ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest.permission.CAMERA},REQUEST_CAMERA_PERMISSION);
                    }
                    else {

                        cameraSource.start(holder);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

            }

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
                scannerDilaog.dismiss();
                cameraSource.stop();
            }
        });

        barcodeDetector.setProcessor(new Detector.Processor<Barcode>() {
            @Override
            public void release() {

            }

            @Override
            public void receiveDetections(Detector.Detections<Barcode> detections) {
                final SparseArray<Barcode> barcodeSparseArray = detections.getDetectedItems();



                if(barcodeSparseArray!=null&&barcodeSparseArray.size()>0){
                    if(!flag) {
                        final Handler handler = new Handler(Looper.getMainLooper());
                        handler.post(new Runnable() {

                            @Override
                            public void run() {
                                int times =0;
                               
                                //flag = true;
                                cameraSource.stop();
                                Intent intent = new Intent(MainActivity.this, HomeActivity.class);
                                intent.putExtra("result", barcodeSparseArray.valueAt(0).displayValue);
                                Log.e("result"," "+barcodeSparseArray.valueAt(0).displayValue);

                                if(!flag) {
                                    times++;
                                    Log.d("times"," "+times);
                                    startActivity(intent);
                                }
                                flag=true;
                                cameraSource.stop();

                            }
                        });

                    }
                }
            }
        });

最后我在这个答案的这一部分找到了解决方案

基于 Google 示例(来源中的评论):

// Note: The first time that an app using the barcode or face API is installed on a
    // device, GMS will download a native libraries to the device in order to do detection.
    // Usually this completes before the app is run for the first time.  But if that
    // download has not yet completed, then the above call will not detect any barcodes
    // and/or faces.