Сканирование QR-кодов с помощью BarcodeDetector устройством OPPO CPH1923 - PullRequest
1 голос
/ 05 августа 2020

У меня есть проект, который сканирует QR Codes. Он отлично работает с Samsung mobiles , но я не могу сканировать QR Codes с помощью OPPO CPH1923 device. Я сделал отладку и обнаружил, что отладчик не попал в функцию переопределения receiveDetections. Я не знаю, почему это устройство дозирует это, и я зря трачу свое время.

Я использую Google mobile vision API

Примечание: у меня в мобильном нет сим-карты

Вот мой код

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();

                            }
                        });

                    }
                }
            }
        });

1 Ответ

0 голосов
/ 10 августа 2020

Наконец, я нашел решение в этой части этого ответа Здесь

На основе примеров 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.
...