4

I'm working on a camera app on android. I'm currently taking my capture with the jpeg callback. 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.

So from the jpeg callback can I have access to raw data (succession of RGB pixels).

EDIT :

So from the jpeg callback can I have access to raw data (succession of YUV pixels).

TurnsCoffeeIntoScripts
  • 3,868
  • 8
  • 41
  • 46

1 Answers1

0

I was successfully able to get a "raw" (YUV422) picture with android 5.1 running on a rk3288.

3 steps to get yuv image

  1. init buffer
  2. call addRawImageCallbackBuffer by relfexion
  3. get the yuv picture in the dedicated callback

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 ( shutterCallback, rawCallback, jpegCallback );

This callback will return a null byteArray, unless I explicitly add a buffer for raw image first.
So, you're supposed to call camera.addRawImageCallbackBuffer for this purpose.

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.

wscourge
  • 10,657
  • 14
  • 59
  • 80
Xavier D
  • 3,364
  • 1
  • 9
  • 16