In C++ how to capture preview frames and save as JPEG withn Android NDK Camera2?

48 Views Asked by At

What I want to do is to use NDK Camera2 to capture preview frames from camera, and encode them as JPEG images in a binary buffer.

For this I searched some examples of displaying preview frames on SurfaceView or TextureView with NDK Camera2 and C++, including this popular sample. but all these examples use (or rely on?) JNIEnv and SurfaceView to display preview frames, and in my use case, my C++ code is not expected to use or rely on Android Views. I don't need to display preview, I just need to save the preview frames as JPEG in a binary buffer. Can you explain how to do this? Thanks!

1

There are 1 best solutions below

0
Eddy Talvala On

You can capture JPEGs directly with the camera2 NDK API, but since those are usually intended for high-quality snapshots, you probably can't do so at preview rate (30fps).

Alternatively, if you're receiving YCbCr_420_888-format buffers from the camera via an NDK ImageReader, you can use a library like libjpeg-turbo to compress to a JPEG in-memory. You may need to adjust the layout of the YUV data in between, depending on what your compression library accepts. (for example, by using libyuv )

Or, you can do a conversion to RGB using something like libyuv and then use the NDK Bitmap compression methods to save a JPEG. Though those are relatively new, so may not work at the API levels you need.