【问题标题】:Android Image to Bitmap for use in Google Vision Barcode apiAndroid Image to Bitmap 用于 Google Vision Barcode api
【发布时间】:2017-01-20 03:53:18
【问题描述】:

我需要将android.media.Image 转换为BitMap,以便可以在Google Vision Barcode API 中使用它。我已经尝试了以下...

    byte[] imageData = new byte[image.getPlanes()[0].getBuffer().remaining()];
    Bitmap bmp = BitmapFactory.decodeByteArray(imageData, 0, imageData.length);
    Frame frame = new Frame.Builder().setBitmap(bmp).build();
    SparseArray<Barcode> barcodes = detector.detect(frame);
    System.out.println(barcodes.valueAt(0));

...但我收到以下错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference.

Google 的示例代码引用了已弃用的 Camera API,但我使用的是 camera2,这就是我无法使用它们寻求帮助的原因。

【问题讨论】:

    标签: java android bitmap camera2 google-vision


    【解决方案1】:

    修复是确保imageReaderJPEG 格式而不是YUV

     mImageReader = ImageReader.newInstance(width, height, ImageFormat.JPEG, 3);
    

    【讨论】:

      猜你喜欢
      • 2016-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-24
      • 2017-04-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多