android平台提供的RenderScript可以帮助应用快速完成yuv到rgb的数据互转。
首先创建实例:
rs = RenderScript.create(this); yuvToRgbIntrinsic = ScriptIntrinsicYuvToRGB.create(rs, Element.U8_4(rs));
接着创建builder和allocator:
yuvType = new Type.Builder(rs, Element.U8(rs)).setX(data.length); in = Allocation.createTyped(rs, yuvType.create(), Allocation.USAGE_SCRIPT); rgbaType = new Type.Builder(rs, Element.RGBA_8888(rs)).setX(SIZE_WIDTH).setY(SIZE_HEIGHT); out = Allocation.createTyped(rs, rgbaType.create(), Allocation.USAGE_SCRIPT);
输入数据:
in.copyFrom(data);
转换:
yuvToRgbIntrinsic.setInput(in); yuvToRgbIntrinsic.forEach(out);
创建位图并输出:
Bitmap bm = Bitmap.createBitmap(SIZE_WIDTH, SIZE_HEIGHT, Bitmap.Config.ARGB_8888); out.copyTo(bm);
以上过程可以用于将camera预览数据转储为bitmap。