NV21转bitmap的高效方式

最近在做人脸检测这块,需要把USB摄像头的byte[]数组转成bitmap显示到屏幕上,这就需要用到

 

public static Bitmap convertByte2Bitmap(byte[] data, int width, int height, int quality) {
        Bitmap bitmap;
        YuvImage yuvImage = new YuvImage(data, ImageFormat.NV21, width, height, null);
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        yuvImage.compressToJpeg(new Rect(0, 0, width, height), quality, stream);

        bitmap = BitmapFactory.decodeByteArray(stream.toByteArray(), 0, stream.size());

        try {
            stream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return bitmap;
    }

此方法转换,但这个方法比较耗时,大概需要30-50毫秒 。

通过google查到了另一个方法转换,使用Renderscript内联函数,效率更快,大概几毫秒就可以转换完成。

public class NV21ToBitmap {

    private RenderScript rs;
    private ScriptIntrinsicYuvToRGB yuvToRgbIntrinsic;

    public NV21ToBitmap(Context context) {
        rs = RenderScript.create(context);
        yuvToRgbIntrinsic = ScriptIntrinsicYuvToRGB.create(rs, Element.U8_4(rs));
    }

    public Bitmap nv21ToBitmap(byte[] nv21, int width, int height) {
        Type.Builder yuvType = new Type.Builder(rs, Element.U8(rs)).setX(nv21.length);
        Allocation in = Allocation.createTyped(rs, yuvType.create(), Allocation.USAGE_SCRIPT);
        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);

        in.copyFrom(nv21);

        yuvToRgbIntrinsic.setInput(in);
        yuvToRgbIntrinsic.forEach(out);

        Bitmap bmpout = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        out.copyTo(bmpout);

        return bmpout;

    }
}

 

转载于:https://my.oschina.net/ldhy/blog/3082533

你可能感兴趣的:(NV21转bitmap的高效方式)