简体   繁体   English

获取Android摄像头的原始RGB数据

[英]Getting the raw RGB data of the android camera

I'm working on a camera app on android. 我正在研究Android上的相机应用程序。 I'm currently taking my capture with the jpeg callback. 我正在使用jpeg回调进行捕获。 I'd like to know if there's a way to get to raw data of the capture. 我想知道是否有办法获取捕获的原始数据。 I know there is a raw callback for the capture but it always return null. 我知道捕获有一个原始回调,但它总是返回null。

So from the jpeg callback can I have access to raw data (succession of RGB pixels). 所以从jpeg回调我可以访问原始数据(RGB像素的连续)。

EDIT : 编辑:

So from the jpeg callback can I have access to raw data (succession of YUV pixels). 所以从jpeg回调我可以访问原始数据(YUV像素的连续)。

I was successfully able to get a "raw" (YUV422) picture with android 5.1 running on a rk3288. 我成功地在rk3288上运行了android 5.1的“原始”(YUV422)图片。

3 steps to get yuv image 获得yuv图像的3个步骤

  1. init buffer init缓冲区
  2. call addRawImageCallbackBuffer by relfexion 调用addRawImageCallbackBuffer通过relfexion
  3. get the yuv picture in the dedicated callback 在专用回调中获取yuv图片

code sample 代码示例

val weight = size.width * size.height * ImageFormat.getBitsPerPixel(ImageFormat.NV21) / 8
val bytes = ByteArray(weight);
val camera = android.hardware.Camera.open();

try {
    val addRawImageCallbackBuffer = camera.javaClass
            .getDeclaredMethod("addRawImageCallbackBuffer", bytes.javaClass)
    addRawImageCallbackBuffer.invoke(camera, bytes)
} catch (e: Exception) {
    Log.e("RNG", "Error", e);
}
...
camera.takePicture(null, { data, camera ->
    val file = File("/sdcard/output.jpg")
    file.createNewFile()
    val yuv = YuvImage(data, ImageFormat.NV21, size.width, size.height, null)
    yuv.compressToJpeg(Rect(0, 0, size.width, size.height), 80, file.outputStream())
}, null)


Explanation 说明

The Camera.takePicture() method takes a callback for raw as second parameter. Camera.takePicture()方法将raw作为第二个参数进行回调。

camera.takePicture ( shutterCallback, rawCallback, jpegCallback );

This callback will return a null byteArray, unless I explicitly add a buffer for raw image first. 除非我首先为raw图像显式添加缓冲区,否则此回调将返回null byteArray。
So, you're supposed to call camera.addRawImageCallbackBuffer for this purpose. 所以,你应该为此目的调用camera.addRawImageCallbackBuffer

Nevertheless, this the method is not available (public but not exported, so you cannot call it directly). 然而,这个方法不可用(公共但不导出,所以你不能直接调用它)。

Fortunately, the code sample demonstrates how to force call this method by reflection. 幸运的是,代码示例演示了如何通过反射强制调用此方法。 This will make the raw buffer to push a consistent yuv picture as a parameter. 这将使原始缓冲区将一致的yuv图片作为参数推送。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM