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; }

谢谢。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值