Since the Android camera preview data can only be IMAGEFORMAT.NV21 format, so the decoding to go through a turn.
Camera Mcamera == mcamera.getparameters ();p. Setpreviewformat (IMAGEFORMAT.NV21); /* This is a unique value or can not be set. Some students may be set to PixelFormat below a value, in fact, is not right, the specific can see the official document */mcamera.setparameters (P); Mcamera.startpreview ( );
The following is the decoding core section:
@Override Public voidOnpreviewframe (byte[] data, camera camera) {Size size=camera.getparameters (). Getpreviewsize (); Try{yuvimage image=NewYuvimage (data, imageformat.nv21, Size.width, Size.Height,NULL); if(image!=NULL) {Bytearrayoutputstream stream=NewBytearrayoutputstream (); Image.compresstojpeg (NewRect (0, 0, Size.width, size.height), 80, stream); Bitmap BMP= Bitmapfactory.decodebytearray (Stream.tobytearray (), 0, Stream.size ()); Stream.Close (); } }Catch(Exception ex) {LOG.E ("Sys", "Error:" +ex.getmessage ()); } }
The code is simple. is to turn YUV data into Bitmap, the system provides Yuvimage class.
YUV420SP Turn RGB
/*** Decoding * *@paramRGB *@paramYUV420SP *@paramWidth *@paramHeight*/ Static Public voidDECODEYUV420SP (int[] RGB,byte[] yuv420sp,intWidthintheight) { Final intFramesize = width *height; for(intj = 0, YP = 0; J < height; J + +) { intUVP = Framesize + (j >> 1) * width, u = 0, v = 0; for(inti = 0; I < width; i++, yp++) { inty = (0xFF & (int) YUV420SP[YP])-16; if(Y < 0) y= 0; if((i & 1) = = 0) {v= (0xFF & yuv420sp[uvp++])-128; U= (0xFF & yuv420sp[uvp++])-128; } inty1192 = 1192 *y; intR = (y1192 + 1634 *v); intg = (y1192-833 * v-400 *u); intB = (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 >> Ten) & 0xFF); } } }
I am the split line of the king of the Land Tiger