Skip to content
Android Camera2 API ImageReader ConvertYUV2RGB
C++ Java Makefile C
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
jni
libs Add files via upload Aug 6, 2018
src/cn/mclover/util Add files via upload Aug 6, 2018
LICENSE
README.md

README.md

Camera2-Yuv2Rgb in NDK

Android5.0 Camera2 ImageReader 读取YUV格式转为RGB格式的方法

本工程提取于tzutalin/dlib-android

高效:

在双Cortex-A72大核+四Cortex-A53小核(RK3399)下处理640x480图片平均耗时22ms

不同方法性能比较见 http://www.mclover.cn/blog/index.php/archives/206.html

使用方法:

1.将so库放到jniLibs文件夹下

2.将src中代码放入工程

3.在Application或入口Activity中加入

    static{
        System.loadLibrary("ImageConvert");
    }

示例方法:

    static{
        System.loadLibrary("ImageConvert");
    }
    public void onImageAvailable(ImageReader reader) {
        Image image = reader.acquireLatestImage();
        Image.Plane[] plane = image.getPlanes();
        byte[][] mYUVBytes = new byte[plane.length][];
        for (int i = 0; i < plane.length; ++i) {
            mYUVBytes[i] = new byte[plane[i].getBuffer().capacity()];
        }
        int[] mRGBBytes = new int[640 * 480];

        for (int i = 0; i < plane.length; ++i) {
            plane[i].getBuffer().get(mYUVBytes[i]);
        }

        final int yRowStride = plane[0].getRowStride();
        final int uvRowStride = plane[1].getRowStride();
        final int uvPixelStride = plane[1].getPixelStride();

        ImageConvert.convertYUV420ToARGB8888(
                mYUVBytes[0],
                mYUVBytes[1],
                mYUVBytes[2],
                mRGBBytes,
                image.getWidth(),
                image.getHeight(),
                yRowStride,
                uvRowStride,
                uvPixelStride,
                false);

        Bitmap mRGBframeBitmap = Bitmap.createBitmap(image.getWidth(), image.getHeight(), Bitmap.Config.ARGB_8888);
        mRGBframeBitmap.setPixels(mRGBBytes, 0, image.getWidth(), 0, 0, image.getWidth(), image.getHeight());
        image.close();
    }

You can’t perform that action at this time.