YUV420转RGBA之使用opencv

前面在 《YUV_420_888介绍及YUV420转RGBA》 一文中介绍了YUV420的转换,并提供了自己写的转换代码。但是实际项目中一般不会自己手写代码逐个像素去转换,因为这样转换的速度比较慢。通常我们可以使用opencv或者libyuv来进行转换。本文就介绍使用opencv进行转换。

YUV420转RGBA系列共三篇:

  • YUV_420_888介绍及YUV420转RGBA
  • YUV420转RGBA之使用opencv
  • YUV420转RGBA之使用libyuv
  • 本文是其中的第二篇。

    1. 下载opencv

    https://github.com/opencv/opencv/releases
    可以下载opencv源码自行编译,也可以选择编译好的opencv-xxx-android-sdk。这里只是简单说明opencv的用法,就直接使用了opencv-3.4.10-android-sdk。

    2. 导入opencv到Android Studio

    导入opencv有两种方式:

  • 直接使用opencv提供的Java版本 API
  • 导入头文件和库文件,自己编写JNI调用opencv C/C++版本 API
    这里选择第二种,自己编写JNI调用opencv C/C++版本 API。
  • 2.1 导入头文件

    在项目的app/src/main/cpp/下新建include文件夹。解压opencv-3.4.10-android-sdk压缩包,将~/opencv-3.4.10-android-sdk/OpenCV-android-sdk/sdk/native/jni/include/下面的头文件拷贝到项目的app/src/main/cpp/include下:

    2.3 修改CMakeLists.txt

    include_directories(${CMAKE_SOURCE_DIR}/include)
    find_library(log-lib log)
    add_library(opencv3 SHARED IMPORTED)
    set_target_properties(
            opencv3
            PROPERTIES
            IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/../jniLibs/${ANDROID_ABI}/libopencv_java3.so
    add_library(
            OpencvUtils
            SHARED
            opencv.cpp
            opencv_utils.cpp
    target_link_libraries(OpencvUtils opencv3 ${log-lib})
    

    修改完成后同步一下项目,接下来我们只要在opencv.cpp和 opencv_utils.cpp中编码调用opencv 的API就可以了。

    3. 使用opencv将YUV420转换成RGBA

    opencv.cpp:

    #include <jni.h>
    #include <string>
    #include "opencv_utils.h"
    extern "C"
    JNIEXPORT void JNICALL
    Java_com_qxt_yuv420_OpencvUtils_I420ToRGBA(JNIEnv *env, jclass clazz,
                                               jbyteArray src, jbyteArray dst,
                                               jint width, jint height) {
        jbyte *_src = env->GetByteArrayElements(src, nullptr);
        jbyte *_dst = env->GetByteArrayElements(dst, nullptr);
        opencvI420ToRGBA(reinterpret_cast<unsigned char *>(_src),
                         reinterpret_cast<unsigned char *>(_dst), width, height);
        env->ReleaseByteArrayElements(src, _src, JNI_ABORT);
        env->ReleaseByteArrayElements(dst, _dst, 0);
    extern "C"
    JNIEXPORT void JNICALL
    Java_com_qxt_yuv420_OpencvUtils_YV12ToRGBA(JNIEnv *env, jclass clazz,
                                               jbyteArray src, jbyteArray dst,
                                               jint width, jint height) {
        jbyte *_src = env->GetByteArrayElements(src, nullptr);
        jbyte *_dst = env->GetByteArrayElements(dst, nullptr);
        opencvYV12ToRGBA(reinterpret_cast<unsigned char *>(_src),
                         reinterpret_cast<unsigned char *>(_dst), width, height);
        env->ReleaseByteArrayElements(src, _src, JNI_ABORT);
        env->ReleaseByteArrayElements(dst, _dst, 0);
    extern "C"
    JNIEXPORT void JNICALL
    Java_com_qxt_yuv420_OpencvUtils_NV12ToRGBA(JNIEnv *env, jclass clazz,
                                               jbyteArray src, jbyteArray dst,
                                               jint width, jint height) {
        jbyte *_src = env->GetByteArrayElements(src, nullptr);
        jbyte *_dst = env->GetByteArrayElements(dst, nullptr);
        opencvNV12ToRGBA(reinterpret_cast<unsigned char *>(_src),
                         reinterpret_cast<unsigned char *>(_dst), width, height);
        env->ReleaseByteArrayElements(src, _src, JNI_ABORT);
        env->ReleaseByteArrayElements(dst, _dst, 0);
    extern "C"
    JNIEXPORT void JNICALL
    Java_com_qxt_yuv420_OpencvUtils_NV21ToRGBA(JNIEnv *env, jclass clazz,
                                               jbyteArray src, jbyteArray dst,
                                               jint width, jint height) {
        jbyte *_src = env->GetByteArrayElements(src, nullptr);
        jbyte *_dst = env->GetByteArrayElements(dst, nullptr);
        opencvNV21ToRGBA(reinterpret_cast<unsigned char *>(_src),
                         reinterpret_cast<unsigned char *>(_dst), width, height);
        env->ReleaseByteArrayElements(src, _src, JNI_ABORT);
        env->ReleaseByteArrayElements(dst, _dst, 0);
    

    opencv_utils.h:

    #ifndef OPENCV_UTILS_H
    #define OPENCV_UTILS_H
    #ifdef __cplusplus
    extern "C" {
    #endif
    void opencvI420ToRGBA(unsigned char *src, unsigned char *dst, int width, int height);
    void opencvYV12ToRGBA(unsigned char *src, unsigned char *dst, int width, int height);
    void opencvNV12ToRGBA(unsigned char *src, unsigned char *dst, int width, int height);
    void opencvNV21ToRGBA(unsigned char *src, unsigned char *dst, int width, int height);
    #ifdef __cplusplus
    #endif
    #endif //OPENCV_UTILS_H
    

    opencv_utils.cpp:

    #include <stdint.h>
    #include <opencv2/opencv.hpp>
    #include <opencv2/core/core.hpp>
    #include "logger.h"
    #include "opencv_utils.h"
    using namespace std;
    using namespace cv;
    void opencvI420ToRGBA(unsigned char *src, unsigned char *dst, int width, int height) {
        Mat srcImg(height * 3 / 2, width, CV_8UC1, src);
        Mat dstImg(height, width, CV_8UC4, dst);
        cvtColor(srcImg, dstImg, CV_YUV2RGBA_I420);
    void opencvYV12ToRGBA(unsigned char *src, unsigned char *dst, int width, int height) {
        Mat srcImg(height * 3 / 2, width, CV_8UC1, src);
        Mat dstImg(height, width, CV_8UC4, dst);
        cvtColor(srcImg, dstImg, CV_YUV2RGBA_YV12);
    void opencvNV12ToRGBA(unsigned char *src, unsigned char *dst, int width, int height) {
        Mat srcImg(height * 3 / 2, width, CV_8UC1, src);
        Mat dstImg(height, width, CV_8UC4, dst);
        cvtColor(srcImg, dstImg, CV_YUV2RGBA_NV12);
    void opencvNV21ToRGBA(unsigned char *src, unsigned char *dst, int width, int height) {
        Mat srcImg(height * 3 / 2, width, CV_8UC1, src);
        Mat dstImg(height, width, CV_8UC4, dst);
        cvtColor(srcImg, dstImg, CV_YUV2RGBA_NV21);
    

    4. 使用opencv旋转RGBA和YUV420P

    opencv.cpp:

    extern "C"
    JNIEXPORT void JNICALL
    Java_com_qxt_yuv420_OpencvUtils_rotateRGB(JNIEnv *env, jclass clazz, jbyteArray src,
                                              jint width, jint height, jfloat degree) {
        jbyte *_src = env->GetByteArrayElements(src, nullptr);
        opencvRotateRGB(reinterpret_cast<unsigned char *>(_src), width, height, degree);
        env->ReleaseByteArrayElements(src, _src, 0);
    extern "C"
    JNIEXPORT void JNICALL
    Java_com_qxt_yuv420_OpencvUtils_rotateRGBA(JNIEnv *env, jclass clazz, jbyteArray src,
                                               jint width, jint height, jfloat degree) {
        jbyte *_src = env->GetByteArrayElements(src, nullptr);
        opencvRotateRGBA(reinterpret_cast<unsigned char *>(_src), width, height, degree);
        env->ReleaseByteArrayElements(src, _src, 0);
    extern "C"
    JNIEXPORT void JNICALL
    Java_com_qxt_yuv420_OpencvUtils_rotateYUV420P(JNIEnv *env, jclass clazz, jbyteArray src,
                                                  jint width, jint height, jfloat degree) {
        jbyte *_src = env->GetByteArrayElements(src, nullptr);
        opencvRotateYUV420P(reinterpret_cast<unsigned char *>(_src), width, height, degree);
        env->ReleaseByteArrayElements(src, _src, 0);
    

    opencv_utils.h:

    void opencvRotateRGB(unsigned char *src, int width, int height, float degree);
    void opencvRotateRGBA(unsigned char *src, int width, int height, float degree);
    void opencvRotateYUV420P(unsigned char *src, int width, int height, float degree);
    

    opencv_utils.cpp:

    static inline void rotate(Mat srcImg, float degree, int stride) {
        Mat tmpImg;
        Mat finalImg;
        if (degree == 90.0f /*ROTATE_90_CLOCKWISE*/) {
            transpose(srcImg, tmpImg);
            flip(tmpImg, finalImg, 1);
        } else if (degree == 180.0f /*ROTATE_180*/) {
            flip(srcImg, finalImg, -1);
        } else if (degree == 270.0f /*ROTATE_90_COUNTERCLOCKWISE*/) {
            transpose(srcImg, tmpImg);
            flip(tmpImg, finalImg, 0);
        } else {
            return;
        memcpy(srcImg.data, finalImg.data,
               static_cast<size_t>(finalImg.rows * finalImg.cols * stride));
        if (!tmpImg.empty()) {
            tmpImg.release();
        if (!finalImg.empty()) {
            finalImg.release();
    void opencvRotateRGB(unsigned char *src, int width, int height, float degree) {
        Mat srcImg(height, width, CV_8UC3, src);
        rotate(srcImg, degree, 3);
    void opencvRotateRGBA(unsigned char *src, int width, int height, float degree) {
        Mat srcImg(height, width, CV_8UC4, src);
        rotate(srcImg, degree, 4);
    void opencvRotateYUV420P(unsigned char *src, int width, int height, float degree) {
        int uvWidth = width >> 1;
        int uvHeight = height >> 1;
        unsigned char *pY = src;
        unsigned char *pU = src + width * height;
        unsigned char *pV = src + width * height * 5 / 4;
        Mat y(height, width, CV_8UC1, pY);
        rotate(y, degree, 1);
        Mat u(uvHeight, uvWidth, CV_8UC1, pU);
        rotate(u, degree, 1);