public class FastYUVtoRGB { private RenderScript rs; private ScriptIntrinsicYuvToRGB yuvToRgbIntrinsic; private Type.Builder yuvType, rgbaType; private Allocation in, out; public FastYUVtoRGB(Context context) { rs = RenderScript.create(context); yuvToRgbIntrinsic = ScriptIntrinsicYuvToRGB.create(rs, Element.U8_4(rs)); } public Bitmap convertYUVtoRGB(byte[] yuvData, int width, int height) { if (yuvType == null) { yuvType = new Type.Builder(rs, Element.U8(rs)).setX(yuvData.length); in = Allocation.createTyped(rs, yuvType.create(), Allocation.USAGE_SCRIPT); rgbaType = new Type.Builder(rs, Element.RGBA_8888(rs)).setX(width).setY(height); out = Allocation.createTyped(rs, rgbaType.create(), Allocation.USAGE_SCRIPT); } in.copyFrom(yuvData); yuvToRgbIntrinsic.setInput(in); yuvToRgbIntrinsic.forEach(out); Bitmap bmpout = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); out.copyTo(bmpout); return bmpout; } //存在native級別的內存泄漏,修改系統源碼。參照下面的鏈接修改 //https://blog.csdn.net/q979713444/article/details/80446404 private static Bitmap nv21ToBitmap(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), 80, stream); bitmap = BitmapFactory.decodeByteArray(stream.toByteArray(), 0, stream.size()); stream.close(); } catch (IOException e) { e.printStackTrace(); } return bitmap; } }
高效的把攝像頭data數據轉換爲bitmap
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.