android camera nv21轉(zhuǎn)I420,預(yù)覽旋轉(zhuǎn)

眾所周知android camera據(jù)絕大多數(shù)情況都是使用nv21格式的數(shù)據(jù),有時(shí)需要將yuv轉(zhuǎn)化成i420格式的數(shù)據(jù)方便我們處理,使用google開源的libyuv效率要比常規(guī)C算法高很多。


編譯動(dòng)態(tài)庫

使用libyuv需要提前將源碼編譯成android需要的動(dòng)態(tài)庫,編譯的方法比較簡單,如果沒有提前編譯好,可以參考下面這個(gè)文章。

創(chuàng)建native module

為了更好的維護(hù),將yuv相關(guān)功能封裝在一個(gè)module中,對(duì)外暴露java api即可,當(dāng)然直接添加到你的app nodule中也是可以的。

不熟悉native工程的,可以用android studio創(chuàng)建一個(gè)native C++模板工程,對(duì)比一下默認(rèn)的配置。

src/main下創(chuàng)建cpp目錄,并在目錄下創(chuàng)建CMakeLists.txt文件。

cpp目錄

其中CMakeLists.txt是配置編譯的腳本文件。

創(chuàng)建CMakeLists.txt

cmake_minimum_required(VERSION 3.10.2)

enable_language(ASM)

#設(shè)置變量值并賦值
set(-DCMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -Wall -s -Wdeprecated-declarations")
set(-DCMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -Wall -s -Wdeprecated-declarations")
set(PATH_TO_MEDIACORE ${CMAKE_SOURCE_DIR})

#源文件文件夾,根據(jù)你c++項(xiàng)目結(jié)構(gòu)修改
include_directories(${CMAKE_SOURCE_DIR})
include_directories(${PATH_TO_MEDIACORE}/)
include_directories(${PATH_TO_MEDIACORE}/util/)

#定義libyuv頭文件位置,從當(dāng)前位置開始,具體位置根據(jù)你自己的放置
set(YUV_HEADER ${CMAKE_CURRENT_LIST_DIR}/../../../../extra/libyuv/include)
#包含上面定義libyuv頭文件
include_directories(${YUV_HEADER})

#定義需要參與編譯的源文件,根據(jù)實(shí)際情況修改
file(GLOB FILES "${PATH_TO_MEDIACORE}/*.cc")
file(GLOB FILES_C "${PATH_TO_MEDIACORE}/*.c")
file(GLOB FILES_UTIL "${PATH_TO_MEDIACORE}/util/*.cc")

#將源文件生產(chǎn)鏈接文件
add_library(
        #生成的名字
        fc_plugin
        #庫的類型為動(dòng)態(tài)庫
        SHARED
        #下面描述的都是分別有哪些文件參與編譯
        fc_plugin_jni.cc
        ${FILES}
        ${FILES_C}
        ${FILES_UTIL})

#定義yuv動(dòng)態(tài)庫的位置
set(YUV_LIB ${CMAKE_CURRENT_LIST_DIR}/../../../../extra/libyuv/libs/${ANDROID_ABI}/libyuv_fc.so)
#定義一個(gè)動(dòng)態(tài)庫yuv
add_library(yuv SHARED IMPORTED)
#將上面定定義的yuv庫和它的位置進(jìn)行綁定,yuv庫定義完成
set_target_properties(yuv PROPERTIES IMPORTED_LOCATION ${YUV_LIB})

#將目標(biāo)文件與庫文件進(jìn)行鏈接
target_link_libraries(
        #目標(biāo)文件,這個(gè)是我們自己生成的連接文件
        fc_plugin
        #上面定義的第三方庫
        yuv)

YUV_HEADERlibyuv頭文件的位置,頭文件在下載的libyuv源碼中的include ,YUV_LIB是我們前面編譯的libyuv動(dòng)態(tài)庫,本文中放在工程根目錄下的extra中,可以根據(jù)情況修改。

路徑參考

修改.gradle

defaultConfig節(jié)點(diǎn)下

externalNativeBuild {
            cmake {
                arguments "-DANDROID_ARM_NEON=TRUE", "-DANDROID_TOOLCHAIN=clang", "-DANDROID_STL=c++_shared"
                cppFlags "-std=c++11"
                abiFilters "armeabi-v7a", "arm64-v8a"
            }
        }

android節(jié)點(diǎn)下

externalNativeBuild {
        cmake {
            version "3.10.2"
            path "src/main/cpp/CMakeLists.txt"
        }
    }

工程配置基本配置完成,上面有些源文件沒有可以暫時(shí)屏蔽掉。

實(shí)現(xiàn)功能

在cpp下創(chuàng)建util包,并創(chuàng)建yuv_util.h

//
// Created by ingxin on 12/27/20.
//

#ifndef FC_YUV_UTIL_H
#define FC_YUV_UTIL_H

#include "libyuv.h"
#include "stdint.h"

/**
 * 將nv21(yuv420sp)類型的數(shù)據(jù)轉(zhuǎn)化成yuv210類型數(shù)據(jù)
 * @param src_nv21_data nv21數(shù)據(jù)
 * @param width 圖像的寬
 * @param height 圖像的高
 * @param src_i420_data 轉(zhuǎn)化后的數(shù)據(jù)
 */
static inline void
nv21ToI420(uint8_t *src_nv21_data, int width, int height, uint8_t *dst_i420_data) {
    int src_y_size = width * height;
    int src_u_size = (width >> 1) * (height >> 1);

    uint8_t *src_nv21_y_data = src_nv21_data;
    uint8_t *src_nv21_vu_data = src_nv21_data + src_y_size;

    uint8_t *src_i420_y_data = dst_i420_data;
    uint8_t *src_i420_u_data = dst_i420_data + src_y_size;
    uint8_t *src_i420_v_data = dst_i420_data + src_y_size + src_u_size;

    libyuv::NV21ToI420(src_nv21_y_data, width,
                       src_nv21_vu_data, width,
                       src_i420_y_data, width,
                       src_i420_u_data, width >> 1,
                       src_i420_v_data, width >> 1,
                       width, height);

}

/**
 * 旋轉(zhuǎn)yuv420數(shù)據(jù).
 * @param src_i420_data yuv420原始數(shù)據(jù)
 * @param width 寬度
 * @param height 高度
 * @param dst_i420_data 轉(zhuǎn)化后的數(shù)據(jù)
 * @param degree 旋轉(zhuǎn)角度
 */
static inline void
rotateI420(uint8_t *src_i420_data, int width, int height, uint8_t *dst_i420_data, int degree) {
    int src_i420_y_size = width * height;
    int src_i420_u_size = (width >> 1) * (height >> 1);

    uint8_t *src_i420_y_data = src_i420_data;
    uint8_t *src_i420_u_data = src_i420_data + src_i420_y_size;
    uint8_t *src_i420_v_data = src_i420_data + src_i420_y_size + src_i420_u_size;

    uint8_t *dst_i420_y_data = dst_i420_data;
    uint8_t *dst_i420_u_data = dst_i420_data + src_i420_y_size;
    uint8_t *dst_i420_v_data = dst_i420_data + src_i420_y_size + src_i420_u_size;

    //要注意這里的width和height在旋轉(zhuǎn)之后是相反的
    if (degree == libyuv::kRotate90 || degree == libyuv::kRotate270) {
        libyuv::I420Rotate(src_i420_y_data, width,
                           src_i420_u_data, width >> 1,
                           src_i420_v_data, width >> 1,
                           dst_i420_y_data, height,
                           dst_i420_u_data, height >> 1,
                           dst_i420_v_data, height >> 1,
                           width, height,
                           (libyuv::RotationMode) degree);
    }
}

/**
 * 縮放I420
 * @param src_i420_data
 * @param width
 * @param height
 * @param dst_i420_data
 * @param dst_width
 * @param dst_height
 * @param mode
 */
static inline void
scaleI420(uint8_t *src_i420_data, int width, int height, uint8_t *dst_i420_data, int dst_width,
          int dst_height, int mode) {

    int src_i420_y_size = width * height;
    int src_i420_u_size = (width >> 1) * (height >> 1);
    uint8_t *src_i420_y_data = src_i420_data;
    uint8_t *src_i420_u_data = src_i420_data + src_i420_y_size;
    uint8_t *src_i420_v_data = src_i420_data + src_i420_y_size + src_i420_u_size;

    int dst_i420_y_size = dst_width * dst_height;
    int dst_i420_u_size = (dst_width >> 1) * (dst_height >> 1);
    uint8_t *dst_i420_y_data = dst_i420_data;
    uint8_t *dst_i420_u_data = dst_i420_data + dst_i420_y_size;
    uint8_t *dst_i420_v_data = dst_i420_data + dst_i420_y_size + dst_i420_u_size;

    libyuv::I420Scale(src_i420_y_data, width,
                      src_i420_u_data, width >> 1,
                      src_i420_v_data, width >> 1,
                      width, height,
                      dst_i420_y_data, dst_width,
                      dst_i420_u_data, dst_width >> 1,
                      dst_i420_v_data, dst_width >> 1,
                      dst_width, dst_height,
                      (libyuv::FilterMode) mode);
}

#endif

java接口

  1. 暴露java api,創(chuàng)建YUVUtil.java。
/**
 * java層接口.
 *
 * @author ingxin
 * @date 2021/1/25
 */
public class YUVUtil {

    /**
     * 將 NV21 轉(zhuǎn) I420
     *
     * @param src    原始數(shù)據(jù)
     * @param dst    轉(zhuǎn)化后的數(shù)據(jù)
     * @param width  寬
     * @param height 高
     */
    public native void convertNV21ToI420(byte[] src, byte[] dst, int width, int height);

    /**
     * 旋轉(zhuǎn)I420數(shù)據(jù)
     *
     * @param input    輸入的I420數(shù)據(jù)
     * @param output   輸入的I420數(shù)據(jù)
     * @param width    寬
     * @param height   高
     * @param rotation 旋轉(zhuǎn)的角度 0,90,180和270
     */
    public native void rotateI420(byte[] input, byte[] output, int width, int height, int rotation);

    /**
     * 縮放I420數(shù)據(jù)
     *
     * @param src       原始數(shù)據(jù)
     * @param srcWidth  原始寬
     * @param srcHeight 原始高
     * @param dst       輸出數(shù)據(jù)
     * @param dstWidth  輸出寬度
     * @param dstHeight 輸出高度
     */
    public native void scaleI420(byte[] src, int srcWidth, int srcHeight, byte[] dst, int dstWidth, int dstHeight);
}

  1. cpp目錄下創(chuàng)建jni文件
//
// Created by ingxin on 2020/12/21.
//

#include <jni.h>
#include "yuv_util.h"

#ifdef __cplusplus
extern "C" {
#endif

#ifndef NELEM
#define NELEM(x) ((int) (sizeof(x) / sizeof((x)[0])))
#endif

//此處為第一步中java api文件包名路徑
#define YUV_UTIL "com/xxx/xxx/xxxx/xxxx/YUVUtil"

static void Android_JNI_NV21_TO_I420(JNIEnv *env,
                                     jobject object,
                                     jbyteArray src_nv21_data,
                                     jbyteArray dst_i420_data,
                                     jint width,
                                     jint height) {

    jbyte *srcData = env->GetByteArrayElements(src_nv21_data, NULL);
    jbyte *dstData = env->GetByteArrayElements(dst_i420_data, NULL);
    //420sp轉(zhuǎn)化成420
    nv21ToI420(reinterpret_cast<uint8_t *>(srcData),
               width,
               height,
               reinterpret_cast<uint8_t *>(dstData));
    // 釋放資源
    env->ReleaseByteArrayElements(src_nv21_data, srcData, 0);
    env->ReleaseByteArrayElements(dst_i420_data, dstData, 0);
}

static void Android_JNI_rotate_I420(JNIEnv *env,
                                    jobject object,
                                    jbyteArray input,
                                    jbyteArray output,
                                    jint width,
                                    jint height,
                                    jint degree) {

    jbyte *srcData = env->GetByteArrayElements(input, NULL);
    jbyte *dstData = env->GetByteArrayElements(output, NULL);

    rotateI420(reinterpret_cast<uint8_t *>(srcData),
               width,
               height,
               reinterpret_cast<uint8_t *>(dstData),
               degree);
    // 釋放資源
    env->ReleaseByteArrayElements(input, srcData, 0);
    env->ReleaseByteArrayElements(output, dstData, 0);
}

static void Android_JNI_scale_I420(JNIEnv *env,
                                   jobject object,
                                   jbyteArray src,
                                   jint src_width,
                                   jint src_height,
                                   jbyteArray dst,
                                   jint dst_width,
                                   jint dst_height) {

    jbyte *srcData = env->GetByteArrayElements(src, NULL);
    jbyte *dstData = env->GetByteArrayElements(dst, NULL);

    scaleI420(reinterpret_cast<uint8_t *>(srcData),
              src_width,
              src_height,
              reinterpret_cast<uint8_t *>(dstData),
              dst_width,
              dst_height,
              0);
    // 釋放資源
    env->ReleaseByteArrayElements(src, srcData, 0);
    env->ReleaseByteArrayElements(dst, dstData, 0);
}

static JNINativeMethod fcYUVUtilMethods[] = {
        {"convertNV21ToI420", "([B[BII)V",   (void **) Android_JNI_NV21_TO_I420},
        {"rotateI420",        "([B[BIII)V",  (void **) Android_JNI_rotate_I420},
        {"scaleI420",         "([BII[BII)V", (void **) Android_JNI_scale_I420}
};

JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) {
    JNIEnv *env = NULL;
    if ((vm)->GetEnv((void **) &env, JNI_VERSION_1_6) != JNI_OK) {
        return JNI_ERR;
    }

    jclass yuvUtil = env->FindClass(YUV_UTIL);
    env->RegisterNatives(yuvUtil, fcYUVUtilMethods, NELEM(fcYUVUtilMethods));
    env->DeleteLocalRef(yuvUtil);

    return JNI_VERSION_1_6;
}

JNIEXPORT void JNI_OnUnload(JavaVM *vm, void *reserved) {
    //卸載so
}

#ifdef __cplusplus
}
#endif

到這里工作已經(jīng)完成,按照這個(gè)步驟慢慢調(diào)試修改總會(huì)成功的。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容