Camera onPreview中byte[] 转换为Bitmap 出现的内存溢出处理方法

Camera onPreview中byte[] 转换为Bitmap 出现的内存溢出处理方法
先看看有几种方法处理转换为Bitmapweb

第一种转换,但这一种最容易出现的就是图片过大而后致使内存不够分配

Bitmap bitmap = null;
        ByteArrayOutputStream baos=null;
        try {
            YuvImage yuvimage = new YuvImage(
                    data,
                    ImageFormat.NV21,
                    previewWidth,
                    previewHeight,
                    null);
            baos = new ByteArrayOutputStream();
            yuvimage.compressToJpeg(new Rect(0, 0, previewWidth, previewHeight), 100, baos);// 80--JPG图片的质量[0-100],100最高
            byte[] rawImage = baos.toByteArray();
            //将rawImage转换成bitmap
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inPreferredConfig = Bitmap.Config.RGB_565;
            options.inSampleSize = 0;
          // options.inJustDecodeBounds =true; //设置为 true 解码器去加载一个较小的图片到内存
            bitmap = BitmapFactory.decodeByteArray(rawImage, 0, rawImage.length, options);
            baos.close();

        } catch (Exception e) {
            try {
               if (baos!=null){
                   baos.close();
               }
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
        }

第二种 使用BitmapFactory.decodeByteArray来进行处理,耗时很是可观,在只开前摄的状况下处理图像,耗时达到了260ms,下面是之前的处理方式

//在这种处理下虽然不能够处理内存溢出,但耗时比较久   耗时达到了260ms
        YuvImage yuvimage = new YuvImage(
                data,
                ImageFormat.NV21,
                previewWidth,
                previewHeight,
                null);
        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();

最后一种方法,要在nCreate中初始化这两个对象 RenderScript 和 ScriptIntrinsicYuvToRGB 而后直接在onPreview中byte[]下调用如下代码便可

try {
            if (yuvType == null) {
                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(previewWidth).setY(previewHeight);
                out = Allocation.createTyped(rs, rgbaType.create(), Allocation.USAGE_SCRIPT);
            }
            in.copyFrom(data);
            yuvToRgbIntrinsic.setInput(in);
            yuvToRgbIntrinsic.forEach(out);
            Bitmap bmpout = Bitmap.createBitmap(previewWidth, previewHeight, Bitmap.Config.ARGB_8888);
            out.copyTo(bmpout);
            return bmpout;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

源码文件下载vim