Android 使用libyuv-缩放

封装一个 JNI 方法来调用 libyuv 的 I420Scale 函数

  1. 创建 JNI 接口

    external fun yuvScale(yuv: ByteArray, srcWidth: Int, srcHeight: Int,dstWith:Int,dstHeight:Int): ByteArray
    
  2. 编写一个 C++ 文件,使用 libyuv 的 I420Scale 方法

    extern "C"
    JNIEXPORT jbyteArray JNICALL
    Java_com_libyuv_YuvUtil_yuvScale(JNIEnv *env, jobject thiz, jbyteArray inputYUV, jint src_width,
                                                     jint src_height, jint dst_with, jint dst_height) {
        // 获取输入数据
        jbyte* inputPtr = env->GetByteArrayElements(inputYUV, nullptr);
        if (inputPtr == nullptr) {
            return nullptr; // Out of memory
        }
    
        // 创建输出数据
        int outputSize = dst_with * dst_height * 3 / 2;
        jbyteArray outputYUV = env->NewByteArray(outputSize);
        jbyte* outputPtr = env->GetByteArrayElements(outputYUV, nullptr);
        if (outputPtr == nullptr) {
            env->ReleaseByteArrayElements(inputYUV, inputPtr, JNI_ABORT);
            return nullptr; // Out of memory
        }
    
        // 进行 YUV420 缩放
        const uint8_t* src_y = reinterpret_cast<const uint8_t*>(inputPtr);
        const uint8_t* src_u = src_y + src_width * src_height;
        const uint8_t* src_v = src_u + (src_width * src_height) / 4;
    
        libyuv::I420Scale(src_y, src_width,
                          src_u, src_width / 2,
                          src_v, src_width / 2,
                          src_width, src_height,
                          reinterpret_cast<uint8_t*>(outputPtr), dst_with,
                          reinterpret_cast<uint8_t*>(outputPtr) + dst_with * dst_height, dst_with / 2,
                          reinterpret_cast<uint8_t*>(outputPtr) + dst_with * dst_height + (dst_with * dst_height) / 4, dst_with / 2,
                          dst_with, dst_height,
                          libyuv::kFilterBilinear);
    
        // 释放输入数据
        env->ReleaseByteArrayElements(inputYUV, inputPtr, JNI_ABORT);
        // 释放输出数据
        env->ReleaseByteArrayElements(outputYUV, outputPtr, 0);
    
        return outputYUV; // 返回输出数据
    }
    
  3. 在 Java 中调用 scaleI420 方法,传入原始 YUV 数据和目标宽高

     var outImage = yuvUtil.yuvScale(it,1920,1080,960,540)
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值