Android--Get camera frame data decoding

Source: Internet
Author: User

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                                                                      

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.