簡體   English   中英

Android Image to Bitmap可在Google Vision條形碼API中使用

[英]Android Image to Bitmap for use in Google Vision Barcode api

我需要將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 ,這就是為什么我不能使用它們來獲取幫助的原因。

解決方法是確保imageReaderJPEG格式而不是YUV

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM