package com.basic.security.utils; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.ImageFormat; import android.graphics.Rect; import android.graphics.YuvImage; import android.os.Build; import android.support.v8.renderscript.Allocation; import android.support.v8.renderscript.Element; import android.support.v8.renderscript.RenderScript; import android.support.v8.renderscript.ScriptIntrinsicYuvToRGB; import android.support.v8.renderscript.Type; import com.basic.security.base.BaseApplication; import java.io.ByteArrayOutputStream; public class RenderScriptHelper { Bitmap bitmap; RenderScript rs; ScriptIntrinsicYuvToRGB yuvToRgbIntrinsic; public Bitmap getBitmapFromFrameData(byte[] data, int width, int height) { if (rs == null) { rs = RenderScript.create(BaseApplication.getApplication()); } if (yuvToRgbIntrinsic == null) { yuvToRgbIntrinsic = ScriptIntrinsicYuvToRGB.create(rs, Element.U8_4(rs)); } Type.Builder yuvType = null, rgbaType; Allocation in = null, out = null; try { final int w = width; //宽度 final int h = height; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { 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(w).setY(h); out = Allocation.createTyped(rs, rgbaType.create(), Allocation.USAGE_SCRIPT); } in.copyFrom(data); yuvToRgbIntrinsic.setInput(in); yuvToRgbIntrinsic.forEach(out); if (bitmap == null) { bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); } out.copyTo(bitmap); } else { ByteArrayOutputStream baos; byte[] rawImage; //处理data BitmapFactory.Options newOpts = new BitmapFactory.Options(); newOpts.inJustDecodeBounds = true; YuvImage yuvimage = new YuvImage( data, ImageFormat.NV21, w, h, null); baos = new ByteArrayOutputStream(); yuvimage.compressToJpeg(new Rect(0, 0, w, h), 100, baos);// 80--JPG图片的质量[0-100],100最高 rawImage = baos.toByteArray(); //将rawImage转换成bitmap BitmapFactory.Options options = new BitmapFactory.Options(); options.inPreferredConfig = Bitmap.Config.RGB_565; bitmap = BitmapFactory.decodeByteArray(rawImage, 0, rawImage.length, options); } in = null; out = null; return bitmap; } catch (Throwable e) { in = null; out = null; return null; } } }