Reference: http://www.bangchui.org/read.php? Tid = 4203
Camera. Parameters. setpreviewformat (pixelformat. JPEG); this method must be supported by the mobile phone Camara; otherwise, it will have no effect.
You can use the list = camera. Parameters. getsupportedpreviewformats () method to determine which type of pixelformat is supported by the mobile phone. Generally, the value is nv21, that is, 17. Bitmapfactory. decodebytearray this method cannot directly parse byte [] In nv21 format and returns NULL. Therefore, you need to convert byte [] In nv21 format.
Finally: SDK 2.2 provides the yuvimage class for conversion through the compresstojpeg method, but sdk2.1 does not have a ready-made API for this operation (or if I did not find it, I can find it and tell it ), therefore, you need to convert the format yourself.
Conversion functions:
public 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);}}}
Convert to bitmap:
if(format == ImageFormat.NV21 || format == ImageFormat.YUY2){ if((version >= 8)){ YuvImage yuv_image = new YuvImage(data, format, 320, 240, null); // Convert YuV to Jpeg Rect rect = new Rect(0, 0, 320, 240); ByteArrayOutputStream output_stream = new ByteArrayOutputStream(); yuv_image.compressToJpeg(rect, 100, output_stream); // Convert from Jpeg to Bitmap bitmap = BitmapFactory.decodeByteArray(output_stream.toByteArray(), 0, output_stream.size()); }else{ decodeYUV420SP(bufInt, data, 320, 240); bitmap = Bitmap.createBitmap(bufInt, 320, 240, Bitmap.Config.RGB_565); }}else if(format == PixelFormat.RGB_565 || format == ImageFormat.JPEG){bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);}
PS: bufint is an array of int type, length and callback function:
Onpreviewframe (byte [] data, camera)
The data array is the same.