Skip to content

Android CameraApi2 / CameraX Image to ByteBuffer converter.

License

Notifications You must be signed in to change notification settings

gordinmitya/yuv2buf

Repository files navigation

Android Camera Api2 / CameraX YUV to ByteBuffer

Motivation:

When you're attempting to get an Image from ImageReader or ImageAnalysis.Analyzer you actually get 3 separate ByteBuffers which you can't pass to further processing. You have to merge them but that also is not easy because they are full of row and pixel strides.

Solution:

This library carefully merges 3 buffers into one with respect to all strides. As a result, you receive YUV type (NV21 or YUV_420) and ByteBuffer to pass into OpenCV or a neural network framework.

The whole library is a single file, you can just copy Yuv.java into your project.

Alternatives

  1. (For OpenCV users) Copy private method from OpenCV camera implementation: JavaCamera2View, Mat rgba().
  2. Capture image from camera directly to RenderScript Allocation.getSurface();
  3. Switch back to CameraApi 1 (some trade offs);
  4. Manipulate pixels manually as it has done in TFLite demo ImageUtils. However, even with C++ implementation it's ridiculously slow. ~50ms for image about 1280x720 on Snapdragon 855;

TODO

  • make the library;
  • write unit tests;
  • add RenderScript example;
  • add OpenCV example;
  • publish to jcenter;
  • add MNN example;
  • add TFLite example.

About

Android CameraApi2 / CameraX Image to ByteBuffer converter.

Topics

Resources

License

Stars

Watchers

Forks