捕获图像条形码后,FirebaseVisionBarcode列表为空

时间:2019-10-25 23:16:11

标签: android firebase-mlkit

当前使用Nexus 5设备进行测试。
捕获包含UPC-A条形码的图像后,将调用FirebaseVisionBarcodeDetector的onSuccess方法。但是,列表为空。不知道什么会导致此问题。



      CameraManager camera_manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);

      try{

          CameraCharacteristics camera_characteristics = camera_manager.getCameraCharacteristics(camera_device.getId());

          Size [] yuvSizes = null;
          if (camera_characteristics != null) {
              yuvSizes = camera_characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP).getOutputSizes(ImageFormat.YUV_420_888);
          }
          int width = 640;
          int height = 480;
          if (yuvSizes != null && 0 < yuvSizes.length) {
              width = yuvSizes[0].getWidth();
              height = yuvSizes[0].getHeight();
          }

          image_reader = ImageReader.newInstance(width, height, ImageFormat.YUV_420_888, 1);
          List<Surface> output_surface = new ArrayList<Surface>(2);
          output_surface.add(image_reader.getSurface());
          output_surface.add(new Surface(texture_view.getSurfaceTexture()));

          final CaptureRequest.Builder capture_builder = camera_device.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);
          capture_builder.addTarget(image_reader.getSurface());
          capture_builder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
          capture_builder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON);
          ImageReader.OnImageAvailableListener reader_listener = new ImageReader.OnImageAvailableListener() {
              @Override
              public void onImageAvailable(ImageReader reader) {

                  Image image = reader.acquireLatestImage();
                  upc_image = image;

                  if(image != null){

                      upc_image = image;

                      Scan scan = new Scan(Camera.this, Camera.this, upc_image);
                      scan.scanning();

                  }

              }
          };

          image_reader.setOnImageAvailableListener(reader_listener, background_handler);
          final CameraCaptureSession.CaptureCallback capture_callback = new CameraCaptureSession.CaptureCallback() {
              @Override
              public void onCaptureCompleted(@NonNull CameraCaptureSession session, @NonNull CaptureRequest request, @NonNull TotalCaptureResult result) {
                  super.onCaptureCompleted(session, request, result);
                  create_camera_preview();
              }
          };

          camera_device.createCaptureSession(output_surface, new CameraCaptureSession.StateCallback() {
              @Override
              public void onConfigured(@NonNull CameraCaptureSession session) {

                  try{
                      session.capture(capture_builder.build(), capture_callback, background_handler);

                  }catch (CameraAccessException camera_access_error){
                      camera_access_error.printStackTrace();
                  }

              }

              @Override
              public void onConfigureFailed(@NonNull CameraCaptureSession session) {

              }
          }, background_handler);

      }catch(CameraAccessException camera_access_error){
          camera_access_error.printStackTrace();
      }


      camera_open_close_lock.release();

  }


当捕获图像并调用onImageAvailable时,宽度和高度分别为3264和2448像素。

image_reader = ImageReader.newInstance(width, height, ImageFormat.YUV_420_888, 1); 

执行条形码检测的地方

public void scanning(){

        FirebaseVisionImage image = null;

        try {
            image = FirebaseVisionImage.fromMediaImage(upc_image, Rotation_Compenstation());
        }catch(CameraAccessException camera_access_error){
            camera_access_error.printStackTrace();
        }

        try {
            Task<List<FirebaseVisionBarcode>> result = firebase_barcode_detector.detectInImage(image)
                    .addOnSuccessListener(new OnSuccessListener<List<FirebaseVisionBarcode>>() {
                        @Override
                        public void onSuccess(List<FirebaseVisionBarcode> firebase_vision_barcodes) {
                            // Task was successful
                            for (FirebaseVisionBarcode firebase_vision_barcode : firebase_vision_barcodes) {


                            }


                        }
                    }).addOnFailureListener(new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception error) {
                            // Task was a failure

                            error.printStackTrace();


                        }
                    }).addOnCanceledListener(new OnCanceledListener() {
                        @Override
                        public void onCanceled() {

                        }
                    }).addOnCompleteListener(new OnCompleteListener<List<FirebaseVisionBarcode>>() {
                        @Override
                        public void onComplete(@NonNull Task<List<FirebaseVisionBarcode>> task) {

                            Log.d(TAG, "Result: " + task.getResult().toString());
                        }
                    });
        }catch (Exception error){
            error.printStackTrace();
            Log.d(TAG, "Error, read fail");
        }


  }

创建FirebaseVisionImage对象时,必须计算旋转补偿。

public int Rotation_Compenstation(){

        int current_device_rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
        // current_device_rotation is 0
        int rotation_compensation = ORIENTATION.get(current_device_rotation);
        // rotation_compensation is 90
        CameraManager camera_manager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
        String cameraId = camera_manager.getCameraIdList()[0];

        int device_sensor_orientation = camera_manager.getCameraCharacteristics(cameraId).get(CameraCharacteristics.SENSOR_ORIENTATION);
        // device_sensor_oreintation is 90

        rotation_compensation = (rotation_compensation + device_sensor_orientation + 270) % 360;
        // rotation_compensation here is 90

        int result_rotation;
        switch(rotation_compensation){

            case 0:
                result_rotation = FirebaseVisionImageMetadata.ROTATION_0;
                break;

            case 90:
                result_rotation = FirebaseVisionImageMetadata.ROTATION_90;
                break;

            case 180:
                result_rotation = FirebaseVisionImageMetadata.ROTATION_180;
                break;

            case 270:
                result_rotation = FirebaseVisionImageMetadata.ROTATION_270;
                break;

                default:
                    result_rotation = FirebaseVisionImageMetadata.ROTATION_0;
                    Log.d(TAG, "Bad Rotation Value: " + result_rotation);
                    break;

        }

        return result_rotation;
        // result_rotation is 1


}

运行时的旋转补偿信息

current_device_rotation: 0
device_sensor_orientation: 90
rotation_compensation: 90
result_rotation: 1

电话设备始终保持竖立状态(人像)。条形码是竖直的(能够从左到右读取)。不知道可能是什么问题。最初我以为图片就像我打电话

一样大
upc_image.getWidth() and upc_image.getHeight() 

该值分别是3264和2448像素。我试图将upc与电话设备分开,但这也没有产生影响。列表仍然为空。

有人知道什么可能导致此问题。

0 个答案:

没有答案