Ant*_*wan 6 android bitmap yuv android-camera
嘿我正在创建小型相机应用程序我已经实现了所有的东西,但我有一个问题,即将NV21字节数组转换为jpeg格式
我发现很多方法,但所有这些甚至不工作或在某些设备上工作
首先 我尝试了这个片段和它适用于Xperia z2 5.2但适用于galaxy s4 4.4.4
bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
Run Code Online (Sandbox Code Playgroud)
这种方式也可以在同一台设备上运行而另一台则失败
int pich = camera.getParameters().getPreviewSize().height;
int picw = camera.getParameters().getPreviewSize().width;
int[] pix = new int[picw * pich];
bitmap.getPixels(pix, 0, picw, 0, 0, picw, pich);
// int R, G, B, Y;
for (int y = 0; y < pich; y++) {
for (int x = 0; x < picw; x++) {
int index = y * picw + x;
int R = (pix[index] >> 16) & 0xff;
int G = (pix[index] >> 8) & 0xff;
int B = pix[index] & 0xff;
pix[index] = 0xff000000 | (R << 16) | (G << 8) | B;
}
}
Run Code Online (Sandbox Code Playgroud)
其次我尝试了许多解决方案来转换解码NV21第一个renderscript代码
public Bitmap convertYUV420_NV21toRGB8888_RenderScript(byte [] data,int W, int H, Fragment fragment) {
// http://stackoverflow.com/questions/20358803/how-to-use-scriptintrinsicyuvtorgb-converting-byte-yuv-to-byte-rgba
RenderScript rs;
ScriptIntrinsicYuvToRGB yuvToRgbIntrinsic;
rs = RenderScript.create(fragment.getActivity());
yuvToRgbIntrinsic = ScriptIntrinsicYuvToRGB.create(rs, Element.U8_4(rs)); //Create an intrinsic for converting YUV to RGB.
Type.Builder yuvType = new Type.Builder(rs, Element.U8(rs)).setX(data.length);
Allocation in = Allocation.createTyped(rs, yuvType.create(), Allocation.USAGE_SCRIPT); //an Allocation will be populated with empty data when it is first created
Type.Builder rgbaType = new Type.Builder(rs, Element.RGBA_8888(rs)).setX(W).setY(H);
Allocation out = Allocation.createTyped(rs, rgbaType.create(), Allocation.USAGE_SCRIPT); //an Allocation will be populated with empty data when it is first created
in.copyFrom(data);//Populate Allocations with data.
yuvToRgbIntrinsic.setInput(in); //Set the input yuv allocation, must be U8(RenderScript).
yuvToRgbIntrinsic.forEach(out); //Launch the appropriate kernels,Convert the image to RGB.
Bitmap bmpout = Bitmap.createBitmap(W, H, Bitmap.Config.ARGB_8888);
out.copyTo(bmpout); //Copy data out of Allocation objects.
return bmpout;
}
Run Code Online (Sandbox Code Playgroud)
还有这段代码
void decodeYUV420SP(int[] rgb, byte[] yuv420sp, int width, int height) {
final int frameSize = 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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
最后我试图将图像保存在SD卡上,然后重新打开它,但它也失败了
File pictureFile = new File(filename);
int pich = camera.getParameters().getPreviewSize().height;
int picw = camera.getParameters().getPreviewSize().width;
Rect rect = new Rect(0, 0,picw, pich);
YuvImage img = new YuvImage(data, ImageFormat.NV21, picw, picw, null);
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
img.compressToJpeg(rect, 100, fos);
fos.write(data);
fos.close();
Run Code Online (Sandbox Code Playgroud)
sil*_*ren 19
有几种方法可以保存来自相机的NV21帧,最简单的方法是将其转换为YuvImage,然后将其保存为Jpeg文件:
FileOutputStream fos = new FileOutputStream(Environment.getExternalStorageDirectory() + "/imagename.jpg");
YuvImage yuvImage = new YuvImage(nv21bytearray, ImageFormat.NV21, width, height, null);
yuvImage.compressToJpeg(new Rect(0, 0, width, height), 100, fos);
fos.close();
Run Code Online (Sandbox Code Playgroud)
或者,您也可以将其转换为Android Bitmap对象并将其另存为PNG或其他格式:
YuvImage yuvImage = new YuvImage(nv21bytearray, ImageFormat.NV21, width, height, null);
ByteArrayOutputStream os = new ByteArrayOutputStream();
yuvImage.compressToJpeg(new Rect(0, 0, width, height), 100, os);
byte[] jpegByteArray = os.toByteArray();
Bitmap bitmap = BitmapFactory.decodeByteArray(jpegByteArray, 0, jpegByteArray.length);
FileOutputStream fos = new FileOutputStream(Environment.getExternalStorageDirectory() + "/imagename.png");
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
Run Code Online (Sandbox Code Playgroud)
请注意,最后一个选项会执行一个NV21 -> JPEG -> Bitmap Object -> PNG file过程,因此请记住,如果您需要高性能,这不是从相机保存预览图像的有效方法.
更新:我厌倦了这个转换工作需要多长时间,所以我在RenderScript周围编写了一个库(easyRS),可以在一行代码中轻松完成:
Bitmap outputBitmap = Nv21Image.nv21ToBitmap(rs, nv21ByteArray, width, height);
Run Code Online (Sandbox Code Playgroud)
它在Moto G 2nd上的2000x2000图像中比JPEG处理快约五倍.
您的第二次尝试(使用 ScriptIntrinsicYuvToRGB)看起来很有希望。使用 JellyBean 4.3 (API18) 或更高版本执行以下操作(相机预览格式必须为 NV21):
首先,不要在将执行脚本的方法或循环中创建 rs、yuvToRgbIntrinsic 和分配。这将极大地减慢您的应用程序的速度,并可能导致内存不足错误。将这些放在 onCreate().. 方法中:
rs = RenderScript.create(this); // create rs object only once and use it as long as possible
yuvToRgbIntrinsic = ScriptIntrinsicYuvToRGB.create(rs, Element.U8_4(rs)); // ditto.
Run Code Online (Sandbox Code Playgroud)
使用 Api18+,分配更容易(您不需要 Type.Builder 对象!)。使用您的 cameraPreviewWidth 和 cameraPreviewHeight 创建分配 aIn:
int yuvDatalength = cameraPreviewWidth*cameraPreviewHeight*3/2; // this is 12 bit per pixel
aIn = Allocation.createSized(rs, Element.U8(rs), yuvDatalength);
Run Code Online (Sandbox Code Playgroud)
你需要一个位图来输出:
bmpout = Bitmap.createBitmap(cameraPreviewWidth, cameraPreviewHeight, Bitmap.Config.ARGB_8888);
Run Code Online (Sandbox Code Playgroud)
并简单地从此位图创建 aOut 分配:
aOut = Allocation.createFromBitmap(rs, bmpout);
Run Code Online (Sandbox Code Playgroud)
设置脚本的分配内(仅一次,在循环外):
yuvToRgbIntrinsic.setInput(aIn); //Set the input yuv allocation, must be U8(RenderScript).
Run Code Online (Sandbox Code Playgroud)
在“相机循环”中使用 byte[] 数据:
aIn.copyFrom(data); // or aIn.copyFromUnchecked(data); // which is faster and safe with camera data
yuvToRgbIntrinsic.forEach(aOut); //Launch the appropriate kernels,Convert the image to RGB.
aOut.copyTo(bmpout); // copy data from Allocation aOut to Bitmap bmpout
Run Code Online (Sandbox Code Playgroud)
例如,在 Nexus 7(2013,JellyBean 4.3)上,全高清(1920x1080 像素)相机预览转换大约需要 7 毫秒。
| 归档时间: |
|
| 查看次数: |
10695 次 |
| 最近记录: |