Android YUV转RGB YUV转Bitmap Bitmap转RGB

   /**
     * yuv转rgb
     * @param yuv yuv数据源
     * @param width 图片宽度
     * @param height 图片高度
     * @return
     */
    public static byte[] yuvToRgb(byte[] yuv, int width, int height){
        Bitmap bitmap = yuvToBitmap(yuv, width, height);
        if(bitmap != null){
            return bitmapToRgb(bitmap);
        }
        return null;
    }
   /**
     * yuv转bitmap
     * @param nv21 yuv数据源
     * @param width 图片宽度
     * @param height 图片高度
     * @return
     */
    public static Bitmap yuvToBitmap(byte[] nv21, int width, int height) {
        Bitmap bitmap = null;
        try {
            YuvImage image = new YuvImage(nv21, ImageFormat.NV21, width, height, null);
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            image.compressToJpeg(new Rect(0, 0, width, height), 100, stream);
            bitmap = BitmapFactory.decodeByteArray(stream.toByteArray(), 0, stream.size());
            stream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bitmap;
    }
    /**
     * bitmap转rgb
     * @param bitmap bitmap数据源
     * @return
     */
    public static byte[] bitmapToRgb(Bitmap bitmap) {
        int bytes = bitmap.getByteCount();
        ByteBuffer buffer = ByteBuffer.allocate(bytes);
        bitmap.copyPixelsToBuffer(buffer);

        byte[] rgba = buffer.array();
        byte[] pixels = new byte[(rgba.length / 4) * 3];

        int count = rgba.length / 4;

        for (int i = 0; i < count; i++) {

            pixels[i * 3] = rgba[i * 4];            //R
            pixels[i * 3 + 1] = rgba[i * 4 + 1];    //G
            pixels[i * 3 + 2] = rgba[i * 4 + 2];    //B

        }

        return pixels;
    }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android 中,使用 Camera API 获取的图像数据通常是以 YUV 格式传输的。要将 YUV 数据换为 RGB,可以使用以下方法: 1. 使用 Android 提供的 YUV 换工具类:YuvImage 和 BitmapFactory。这种方法的缺点是比较耗时,而且需要将 YUV 数据先换为 JPEG 或 PNG 格式,然后再换为 RGB。代码示例: ``` YuvImage yuvImage = new YuvImage(data, ImageFormat.NV21, width, height, null); ByteArrayOutputStream out = new ByteArrayOutputStream(); yuvImage.compressToJpeg(new Rect(0, 0, width, height), 50, out); byte[] imageBytes = out.toByteArray(); Bitmap bitmap = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length); ``` 2. 使用 RenderScript 进行 YUV 换。RenderScript 是 Android 提供的高性能计算引擎,可以在 GPU 上进行并行计算,比 CPU 要快很多。代码示例: ``` RenderScript rs = RenderScript.create(context); ScriptIntrinsicYuvToRGB yuvToRgb = ScriptIntrinsicYuvToRGB.create(rs, Element.U8_4(rs)); Type.Builder yuvType = new Type.Builder(rs, Element.U8(rs)).setX(data.length); Allocation in = Allocation.createTyped(rs, yuvType.create(), Allocation.USAGE_SCRIPT); in.copyFrom(data); Type.Builder rgbaType = new Type.Builder(rs, Element.RGBA_8888(rs)).setX(width).setY(height); Allocation out = Allocation.createTyped(rs, rgbaType.create(), Allocation.USAGE_SCRIPT); yuvToRgb.setInput(in); yuvToRgb.forEach(out); Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); out.copyTo(bitmap); rs.destroy(); ``` 注意:以上代码示例中的参数 data 是 byte[] 类型的 YUV 数据,width 和 height 分别是图像宽度和高度。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值