All Projects → gordinmitya → yuv2buf

gordinmitya / yuv2buf

Licence: MIT License
Android CameraApi2 / CameraX Image to ByteBuffer converter.

Programming Languages

C++
36643 projects - #6 most used programming language
java
68154 projects - #9 most used programming language
kotlin
9241 projects
c
50402 projects - #5 most used programming language
CMake
9771 projects

Projects that are alternatives of or similar to yuv2buf

QRCodeCameraX
QRcode decoder based on CameraX & zxing-core & ML kit, in less than 50 lines
Stars: ✭ 111 (+200%)
Mutual labels:  camera2-api, camerax
CameraXDemo
CameraXDemo - Demonstrate CameraX APIs like Preview, Capture and Analysis.
Stars: ✭ 17 (-54.05%)
Mutual labels:  camera2-api, camerax
Udacity-CarND-Vehicle-Detection-and-Tracking
Term 1, Project 5 - Udacity Self Driving Car Nanodegree
Stars: ✭ 20 (-45.95%)
Mutual labels:  yuv
CameraX
CameraX demo, but written in java
Stars: ✭ 42 (+13.51%)
Mutual labels:  camerax
Camera
Android camera app to get manual control over focus, shuttertime, iso and white balance
Stars: ✭ 27 (-27.03%)
Mutual labels:  camera2-api
Cameraview
📸 A well documented, high-level Android interface that makes capturing pictures and videos easy, addressing all of the common issues and needs. Real-time filters, gestures, watermarks, frame processing, RAW, output of any size.
Stars: ✭ 4,137 (+11081.08%)
Mutual labels:  camera2-api
ColorHelper
No description or website provided.
Stars: ✭ 34 (-8.11%)
Mutual labels:  yuv
scannerX
ScannerX is a showcase app for demonstrating QR/Barcode scanning with CameraX and scanner libraries.
Stars: ✭ 54 (+45.95%)
Mutual labels:  camerax
MLKit
🌝 MLKit是一个强大易用的工具包。通过ML Kit您可以很轻松的实现文字识别、条码识别、图像标记、人脸检测、对象检测等功能。
Stars: ✭ 294 (+694.59%)
Mutual labels:  camerax
zAnalysis
zAnalysis是基于Pascal语言编写的大型统计学开源库
Stars: ✭ 52 (+40.54%)
Mutual labels:  yuv
qml-ar
Seamless Augmented Reality module for QML using UchiyaMarkers
Stars: ✭ 32 (-13.51%)
Mutual labels:  yuv
Photonoter
📓Material Design风格的开源照片笔记。(MVP+Dagger2+RxJava+AspectJ+Dex处理)
Stars: ✭ 1,592 (+4202.7%)
Mutual labels:  camera2-api
CreditCardScanner
Android Credit Card Scanner using CameraX and ML Kit
Stars: ✭ 24 (-35.14%)
Mutual labels:  camerax
hella-renderscript
Hella-parallel processing with RenderScript on Android
Stars: ✭ 27 (-27.03%)
Mutual labels:  camera2-api
RxCamera2
Rx Java 2 wrapper for Camera2 google API
Stars: ✭ 27 (-27.03%)
Mutual labels:  camera2-api
Zxinglite
🔥 ZXing的精简版,优化扫码和生成二维码/条形码,内置闪光灯等功能。扫描风格支持:微信的线条样式,支付宝的网格样式。几句代码轻松拥有扫码功能 ,ZXingLite让集成更简单。(扫码识别速度快如微信)
Stars: ✭ 2,117 (+5621.62%)
Mutual labels:  camerax
CameraX-Samples
No description or website provided.
Stars: ✭ 23 (-37.84%)
Mutual labels:  camerax
Image-Detection-Samples
This sample app supports "Building a MVP with Face recognition and AR" and "Quest of a Hero part 2" presentations as well as it has two different possibilities to build face detection mechanism. The first one is OpenCV based and the second one is by means of Camera 2 API
Stars: ✭ 36 (-2.7%)
Mutual labels:  camera2-api
ProPicker
ProPicker is a file picker (image, video, file) library for Android. It helps you to pick any file and return the result in a convenient way
Stars: ✭ 25 (-32.43%)
Mutual labels:  camerax
MacGyver
🤖 A simple application using Google's MLkit library and cameraX api.
Stars: ✭ 73 (+97.3%)
Mutual labels:  camerax

Android Camera Api2 / CameraX YUV to ByteBuffer

OUTDATED!

7 years passed since CameraApi 2 was introduced in Android 5. So Google decided that it's time to make convenient way to get RGB images from it.

ImageAnalysis from CaemraX now supports setOutputImageFormat(ImageAnalysis.OUTPUT_IMAGE_FORMAT_RGBA_8888) developer.android.com

Hovewer if you do not use CameraX for some reason - welcome!

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 I420) and ByteBuffer to pass into OpenCV or a neural network framework.

The solution was tested with CameraX, CameraApi 2, and MediaCodec (for video files).

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

Usage

private var reuseBuffer: ByteBuffer? = null

fun convert(image: ImageProxy): Pair<Bitmap, Long> {

    // val converted = Yuv.toBuffer(image)
    // OR pass existing DirectBuffer for reuse
    val converted = Yuv.toBuffer(image, reuseBuffer)
    reuseBuffer = converted.buffer

    val format = when (converted.type) {
        ImageFormat.YUV_420_888 -> Imgproc.COLOR_YUV2RGB_I420
        ImageFormat.NV21 -> Imgproc.COLOR_YUV2RGB_NV21
        else -> throw IllegalArgumentException()
    }

    // process converted.buffer ByteBuffer with one of converters
}

About android image formats

In android documentation ImageFormat.YUV_420_888 is stated as 'generic YCbCr format', so the actual format depends on u and v order and are these planes interleaved or not (pixelStride=1 or 2).

Most of the time you will get NV21 format (just as in Camera1 api).

And you even may start wondering is there a phone with I420 format (u and v pixelStride==1) – Yes! There is one. Blackview BV6000S Android 7.0. Screenshot. It looks like it's the only one. Other models of Blackview I was able to find in local sellers used NV21.

Converters

  1. RenderScriptConverter.kt - built-in, no additional libraries required. Uses Bitmap for rotation.
  2. OpenCVRoteterter.kt - preform rotation on yuv image and then converts color. The fastest in total time.
  3. OpenCVConverter.kt - the fastest color conversion without rotation. Rotation of a rgb image is slightly harder.
  4. MNNConverter.kt - if your goal is futher processing with neural network. Conversion and rotation performed in single operation.

PS: If your goal is to get Mat you may consider this method from OpenCV.

Benchmark

Snapdragon 855 (Xiaomi Mi 9T Pro). Image resolution 480x640.

OpenCV (YUV rotate) OpenCV (RGB rotate) RenderScript MNN
color ~1ms ~1.6ms ~2.2ms ~21ms
rotate ~3.5ms ~2.8ms ~16.3ms NA (included in color)

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. RenderScript implementation in android/camera-samples repo.
  4. Switch back to CameraApi 1 (some trade-offs);
  5. 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;
  • add MNN example;
  • publish to GooglePlay;
  • publish to mavenCentral();
  • add TFLite example.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].