YUV420_888转RGB 格式

        最近在看android camera源码,在DngCreator.java 文件中看到YUV420_888转换成RGB代码,摘抄下来方便使用。代码路径:Android/sdk/platforms/android-35/android.jar!/android/hardware/camera2/DngCreator.class

1. 将YUV420_888先转换成YUV的格式。


    /**
     * Generate a direct RGB {@link ByteBuffer} from a YUV420_888 {@link Image}.
     */
    private static ByteBuffer convertToRGB(Image yuvImage) {
        // TODO: Optimize this with renderscript intrinsic.
        int width = yuvImage.getWidth();
        int height = yuvImage.getHeight();
        ByteBuffer buf = ByteBuffer.allocateDirect(BYTES_PER_RGB_PIX * width * height);

        Image.Plane yPlane = yuvImage.getPlanes()[0];
        Image.Plane uPlane = yuvImage.getPlanes()[1];
        Image.Plane vPlane = yuvImage.getPlanes()[2];

        ByteBuffer yBuf = yPlane.getBuffer();
        ByteBuffer uBuf = uPlane.getBuffer();
        ByteBuffer vBuf = vPlane.getBuffer();

        yBuf.rewind();
        uBuf.rewind();
        vBuf.rewind();

        int yRowStride = yPlane.getRowStride();
        int vRowStride = vPlane.getRowStride();
        int uRowStride = uPlane.getRowStride();

        int yPixStride = yPlane.getPixelStride();
        int vPixStride = vPlane.getPixelStride();
        int uPixStride = uPlane.getPixelStride();

        byte[] yuvPixel = { 0, 0, 0 };
        byte[] yFullRow = new byte[yPixStride * (width - 1) + 1];
        byte[] uFullRow = new byte[uPixStride * (width / 2 - 1) + 1];
        byte[] vFullRow = new byte[vPixStride * (width / 2 - 1) + 1];
        byte[] finalRow = new byte[BYTES_PER_RGB_PIX * width];
        for (int i = 0; i < height; i++) {
            int halfH = i / 2;
            yBuf.position(yRowStride * i);
            yBuf.get(yFullRow);
            uBuf.position(uRowStride * halfH);
            uBuf.get(uFullRow);
            vBuf.position(vRowStride * halfH);
            vBuf.get(vFullRow);
            for (int j = 0; j < width; j++) {
                int halfW = j / 2;
                yuvPixel[0] = yFullRow[yPixStride * j];
                yuvPixel[1] = uFullRow[uPixStride * halfW];
                yuvPixel[2] = vFullRow[vPixStride * halfW];
                yuvToRgb(yuvPixel, j * BYTES_PER_RGB_PIX, /*out*/finalRow);
            }
            buf.put(finalRow);
        }

        yBuf.rewind();
        uBuf.rewind();
        vBuf.rewind();
        buf.rewind();
        return buf;
    }

2.将YUV 转换成为RGB。


    /**
     * Convert a single YUV pixel to RGB.
     */
    private static void yuvToRgb(byte[] yuvData, int outOffset, /*out*/byte[] rgbOut) {
        final int COLOR_MAX = 255;

        float y = yuvData[0] & 0xFF;  // Y channel
        float cb = yuvData[1] & 0xFF; // U channel
        float cr = yuvData[2] & 0xFF; // V channel

        // convert YUV -> RGB (from JFIF's "Conversion to and from RGB" section)
        float r = y + 1.402f * (cr - 128);
        float g = y - 0.34414f * (cb - 128) - 0.71414f * (cr - 128);
        float b = y + 1.772f * (cb - 128);

        // clamp to [0,255]
        rgbOut[outOffset] = (byte) Math.max(0, Math.min(COLOR_MAX, r));
        rgbOut[outOffset + 1] = (byte) Math.max(0, Math.min(COLOR_MAX, g));
        rgbOut[outOffset + 2] = (byte) Math.max(0, Math.min(COLOR_MAX, b));
    }

`PermissionError: [Errno 13] Permission denied` 这个错误信息表示你尝试访问或操作某个文件或目录时,没有得到足够的权限。在你提供的例子中,你试图对`'D:/kyj/paper_code/GLARE-main/pretrained_weights'`路径下的内容进行操作,但是被系统拒绝了,这通常是因为以下几种情况: 1. 文件或文件夹已经被其他程序打开或占用,尤其是某些杀毒软件可能会限制对文件的操作。 2. 当前用户没有足够的权限来访问或修改该目录。这可能是因为文件或文件夹的权限设置问题,比如设置了仅管理员或特定用户组可以访问。 3. 你可能尝试在Windows系统上以管理员身份运行一个需要普通用户权限的程序,Windows有时会限制这种操作。 为了解决这个问题,你可以尝试以下步骤: 1. 检查文件或文件夹是否已经被其他程序打开,如果是,请关闭相关程序。 2. 尝试以管理员权限运行你的应用程序,如果你正在使用命令行工具,可以尝试以管理员身份重新打开命令行。 3. 确认当前用户账户的权限设置,确保你的用户账户有权限访问该文件夹。 4. 如果是在编程环境中遇到这个问题,尝试更改文件操作的代码,使用适当的错误处理机制来捕获并处理权限问题。 需要注意的是,在处理权限问题时要小心,避免轻易给予程序或用户过多的权限,这可能会导致系统安全风险。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值