问题描述
嘿,我正在创建小型相机应用程序,我已经实现了所有功能,但是我遇到了一个问题,即将NV21字节数组转换为jpeg格式
我发现了很多方法,但是所有方法甚至都无法正常工作或在某些设备上正常工作
首先,我尝试了此代码段,该代码段适用于Xperia z2 5.2,但适用于银河s4 4.4.4
Hey I'm creating small camera app I have implemented every things but I have one problem which is converting NV21 byte array into jpeg format
I have found many way but all of them even not working or work on some devices
firstly i tried this snippet and it work on Xperia z2 5.2 but on galaxy s4 4.4.4
bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
这种方法也可以在同一设备上运行,而在另一设备上失败
also this way works on same device and fail on the other
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;
}
}
第二次我尝试了许多解决方案来转换解码NV21第一个renderscript
代码
secondly I tried many solutions to convert decode NV21first one renderscript
code
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;
}
还有此代码
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);
}
}
}
最后我试图将图像保存在sd卡上,然后重新打开但也失败
and finally I tried to save the image on sd card and then reopen itbut it also fail
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();
这是我遵循的最后3种方法的结果
and this is the result with last 3 approach I had follow
推荐答案
有几种方法可以保存来自摄像机的NV21帧,最简单的方法是将其转换为YuvImage然后保存至Jpeg文件:
There are a few ways to save an NV21 frame coming from the camera, the easiest way being converting it to a YuvImage then saving it to a Jpeg file:
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();
或者,您也可以将其转换为Android Bitmap对象,并将其另存为PNG或其他格式:
Alternatively, you can also convert it to Android Bitmap object and save it as a PNG or other format:
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();
请注意,最后一个选项执行NV21 -> JPEG -> Bitmap Object -> PNG file
处理,因此请记住,如果需要高性能,这不是从相机保存预览图像的非常有效的方法.
Note that the last option does a NV21 -> JPEG -> Bitmap Object -> PNG file
process so keep in mind that this is not a very efficient way to save preview images from the camera if you need high performance.
更新:我已经厌倦了这种转换需要花费多长时间,所以我写了一个库( easyRS )围绕RenderScript轻松地在一行代码中做到这一点:
UPDATE: I got tired of how long it takes for this conversion to work so I wrote a library (easyRS) around RenderScript to easily do this in one line of code:
Bitmap outputBitmap = Nv21Image.nv21ToBitmap(rs, nv21ByteArray, width, height);
在Moto G 2nd上的2000x2000图像中,它比JPEG处理快大约五倍.
It is around five times faster than the JPEG process in a 2000x2000 image on a Moto G 2nd.
这篇关于将NV21字节数组转换为位图可读格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!