日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

android获取图片的rgb,使用ImageReader读取RGB图像

發布時間:2023/12/31 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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图像的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。