我想保存从预览到jpeg文件的视图帧.它看起来或多或少(代码简化 – 没有额外的逻辑,例外等)像这样……
public voID onPrevIEwFrame(byte[] data,Camera camera) { int wIDth = camera.getParameters().getPrevIEwSize().wIDth; int height = camera.getParameters().getPrevIEwSize().height; final int[] rgb = decodeYUV420SP(data,wIDth,height); Bitmap bmp = Bitmap.createBitmap(rgb,height,Bitmap.Config.ARGB_8888); String filename="/sdcard/file" + (index++)+ ".jpg"; fileOutputStream out; out = new fileOutputStream(filename); bmp.compress(Bitmap.CompressFormat.JPEG,90,out); out.flush(); out.close(); out=null; }
这是我尝试转换颜色的方法之一(我相信这个板)
public int[] decodeYUV420SP( byte[] yuv420sp,int wIDth,int height) { final int frameSize = wIDth * height; int rgb[]=new int[wIDth*height]; for (int j = 0,yp = 0; j < height; j++) { int uvp = frameSize + (j >> 1) * wIDth,u = 0,v = 0; for (int i = 0; i < wIDth; i++,yp++) { int y = (0xff & ((int) yuv420sp[yp])) - 16; if (y < 0) y = 0; if ((i & 1) == 0) { v = (0xff & yuv420sp[uvp++]) - 128; u = (0xff & yuv420sp[uvp++]) - 128; } int y1192 = 1192 * y; int r = (y1192 + 1634 * v); int g = (y1192 - 833 * v - 400 * u); int b = (y1192 + 2066 * u); if (r < 0) r = 0; else if (r > 262143) r = 262143; if (g < 0) g = 0; else if (g > 262143) g = 262143; if (b < 0) b = 0; else if (b > 262143) b = 262143; rgb[yp] = 0xff000000 | ((r << 6) & 0xff0000) | ((g >> 2) & 0xff00) | ((b >> 10) & 0xff); } } return rgb; }
问题是图片总是看起来像三个“奇怪的绿色图片”……
我是新用户所以我不能发布它:(
我不知道它是否与尺寸有什么关系或者我被卡住了…
你可以用它支持我吗?
@OverrIDe public voID onPrevIEwFrame(byte[] data,Camera camera) { Bitmap bitmap = Bitmap.createBitmap(r.wIDth(),r.height(),Bitmap.Config.ARGB_8888); Allocation bmData = renderScriptNV21ToRGBA8888( mContext,r.wIDth(),data); bmData.copyTo(bitmap);}public Allocation renderScriptNV21ToRGBA8888(Context context,int height,byte[] nv21) { RenderScript rs = RenderScript.create(context); ScriptIntrinsicYuvToRGB yuvToRgbIntrinsic = ScriptIntrinsicYuvToRGB.create(rs,Element.U8_4(rs)); 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); return out;}总结
以上是内存溢出为你收集整理的android – 将预览帧转换为位图全部内容,希望文章能够帮你解决android – 将预览帧转换为位图所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)