Android Camera onPreview中byte[]快速转换为Bitmap<耗时仅需3~4ms>
2016-12-02 09:59
1396 查看
Android Camera onPreview中byte[]快速转换为Bitmap
在以前对帧率要求不高时,一直使用BitmapFactory.decodeByteArray来进行处理,耗时非常可观,在只开前摄的情况下处理图像,耗时达到了260ms,下面是以前的处理方式:YuvImage yuvimage = new YuvImage( yuvData, ImageFormat.NV21, size.width, size.height, null);//data是onPreviewFrame参数提供 ByteArrayOutputStream baos = new ByteArrayOutputStream(); yuvimage.compressToJpeg( new Rect(0, 0,yuvimage.getWidth(), yuvimage.getHeight()), 100, baos);// 80--JPG图片的质量[0-100],100最高 byte[] rawImage =baos.toByteArray(); BitmapFactory.Options options = new BitmapFactory.Options(); //options.inPreferredConfig = Bitmap.Config.RGB_565; //默认8888 //options.inSampleSize = 8; SoftReference<Bitmap> softRef = new SoftReference<Bitmap>(BitmapFactory.decodeByteArray(rawImage, 0,rawImage.length,options));//方便回收 Bitmap bitmap = (Bitmap) softRef.get();
在新方法下,仅仅3~4ms就可完成对图像的处理,需要使用Renderscript内联函数,可以更快的转换为YUV图像:
使用到的变量:
private RenderScript rs; private ScriptIntrinsicYuvToRGB yuvToRgbIntrinsic; private Type.Builder yuvType, rgbaType; private Allocation in, out;
在onCreate方法中创建:
rs = RenderScript.create(this); yuvToRgbIntrinsic = ScriptIntrinsicYuvToRGB.create(rs, Element.U8_4(rs));
在onPreviewFrame方法中调用以下方法:
if (yuvType == null) { yuvType = new Type.Builder(rs, Element.U8(rs)).setX(dataLength); in = Allocation.createTyped(rs, yuvType.create(), Allocation.USAGE_SCRIPT); rgbaType = new Type.Builder(rs, Element.RGBA_8888(rs)).setX(prevSizeW).setY(prevSizeH); out = Allocation.createTyped(rs, rgbaType.create(), Allocation.USAGE_SCRIPT); } in.copyFrom(data); yuvToRgbIntrinsic.setInput(in); yuvToRgbIntrinsic.forEach(out); Bitmap bmpout = Bitmap.createBitmap(prevSizeW, prevSizeH, Bitmap.Config.ARGB_8888); out.copyTo(bmpout);
个人联系方式:15010399702@163.com
相关文章推荐
- 从public void onPreviewFrame(byte[] data, Camera arg1)拿到Bitmap(收集)
- 从public void onPreviewFrame(byte[] data, Camera arg1)拿到Bitmap
- [Android] bitmap/drawable/byte的转换和获取圆角/倒影图片
- Android中Bitmap、Drawable、byte[]转换
- android 将图片内容解析成字节数组,将字节数组转换为ImageView可调用的Bitmap对象,图片缩放,把字节数组保存为一个文件,把Bitmap转Byte
- Android进阶2之Bitmap、Drawable、byte[]转换
- <项目日志1001>ipcamera for android项目准备更新
- Drawable、Bitmap、byte[]之间的转换 (android转)
- android 将图片内容解析成字节数组,将字节数组转换为ImageView可调用的Bitmap对象,图片缩放,把字节数组保存为一个文件,把Bitmap转Byte
- VB 字节数组和字符串的转换问题 (String<>Byte)
- VB 长整型和字节数组的转换问题 (Long<>Byte)
- android 将图片内容解析成字节数组,将字节数组转换为ImageView可调用的Bitmap对象,图片缩放,把字节数组保存为一个文件,把Bitmap转Byte
- android 将图片内容解析成字节数组,将字节数组转换为ImageView可调用的Bitmap对象,图片缩放,把字节数组保存为一个文件,把Bitmap转Byte
- android.graphics.Bitmap.Config<ALPHA_8, ARGB_4444,ARGB_8888,RGB_565>
- How to resolve Android Camera Preview on Emulator show white and black square only?
- EmguCV中 Image<'TColor, TDepth>与 Bitmap之间的转换
- android draw arrow on camera preview
- Android学习 之 Bitmap Drawable byte[] 三者之间的转换以及把数组存入数据库及提取数据重新组合成所需对象,如图像
- Android Drawable、Bitmap、byte[]之间的转换
- android Bitmap、Drawable、byte[]相互转换