android获取图片的rgb,使用ImageReader读取RGB图像
我正在嘗試使用ImageReader從相機中獲取RGB圖像。 我在運行開發者預覽版的Nexus 5上使用Android 5.0“L”的Camera2 API。
我已經為RGB圖像配置了SurfaceView ,它工作正常,我知道相機硬件會生成RGB數據(因為Android上的所有色調映射和顏色增益設置都指定為在RGB通道上運行)。
我可以通過以下方式創建ImageReader從ImageReader獲取YUV_420_888圖像:
imageReader = ImageReader.newInstance(W, H, ImageFormat.YUV_420_888, 4);
然后將YUV圖像轉換為RGB。 然而,這引入了不希望的量化誤差(因為我的應用需要RGB圖像)和不必要的處理時間。
但是,當我嘗試以這種方式創建圖像閱讀器時:
imageReader = ImageReader.newInstance(W, H, PixelFormat.RGB_888, 4);
圖像捕獲失敗,但出現以下exception:
java.lang.UnsupportedOperationException: The producer output buffer format 0x22 doesn't match the ImageReader's configured buffer format 0x3. at android.media.ImageReader.nativeImageSetup(Native Method) at android.media.ImageReader.acquireNextSurfaceImage(ImageReader.java:293) at android.media.ImageReader.acquireNextImage(ImageReader.java:339) at android.media.ImageReader.acquireLatestImage(ImageReader.java:243) at
我在兩條戰線上感到困惑。 首先,提到的輸出格式0x22不在PixelFormat或ImageFormat中。 它似乎是某種未記錄的原始模式,但我不能使用ImageReader.newInstance(W, H, 0x22, 4)來捕獲它(我得到java.lang.UnsupportedOperationException: Invalid format specified 34 )。 我希望以原始格式捕獲,但我不能說服ImageFormat接受它(并且其他原始格式ImageFormat.RAW_SENSOR由于某種原因非常慢)。
其次, SurfaceView已經很樂意使用RGB_888圖像(據我所知),并將它們直接放在屏幕上。 那么為什么ImageReader不能正確接受RGB圖像呢? 我做錯了什么?
試試這個從圖像中獲取RGB,它對我有用:
private byte[] rgbValuesFromBitmap(Bitmap bitmap) { ColorMatrix colorMatrix = new ColorMatrix(); ColorFilter colorFilter = new ColorMatrixColorFilter( colorMatrix); Bitmap argbBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(argbBitmap); Paint paint = new Paint(); paint.setColorFilter(colorFilter); canvas.drawBitmap(bitmap, 0, 0, paint); int width = bitmap.getWidth(); int height = bitmap.getHeight(); int componentsPerPixel = 3; int totalPixels = width * height; int totalBytes = totalPixels * componentsPerPixel; byte[] rgbValues = new byte[totalBytes]; @ColorInt int[] argbPixels = new int[totalPixels]; argbBitmap.getPixels(argbPixels, 0, width, 0, 0, width, height); for (int i = 0; i < totalPixels; i++) { @ColorInt int argbPixel = argbPixels[i]; int red = Color.red(argbPixel); int green = Color.green(argbPixel); int blue = Color.blue(argbPixel); rgbValues[i * componentsPerPixel + 0] = (byte) red; rgbValues[i * componentsPerPixel + 1] = (byte) green; rgbValues[i * componentsPerPixel + 2] = (byte) blue; } return rgbValues; }
謝謝。
總結
以上是生活随笔為你收集整理的android获取图片的rgb,使用ImageReader读取RGB图像的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: vxe-table踩坑,表格操作列按钮不
- 下一篇: 跟着团子学SAP PS:通过项目挣值分析