Android camera development: encoding of real-time camera video preview Frames

Source: Internet
Author: User
Tags types of functions

[Java] final YuvImage image = new YuvImage (mData, ImageFormat. NV21, w, h, null); ByteArrayOutputStream OS = new ByteArrayOutputStream (mData. length); if (! Image. compressToJpeg (new Rect (0, 0, w, h), 100, OS) {return null;} byte [] tmp = OS. toByteArray (); Bitmap bmp = BitmapFactory. decodeByteArray (tmp, 0, tmp. length); because this mData is in byte [] format, the conversion process is: byte [] --- YuvImage ---- ByteArrayOutputStream --- byte [] ----- Bitmap. At first glance, this conversion is really redundant. Let's take a look at goolge's api: [html] public abstract void onPreviewFrame (byte [] data, Camera camera) Added in API level 1 Called as preview frames are displayed. this callback is invoked on the event thread open (int) was called from. if using the YV12 format, refer to the equations in setPreviewFormat (int) for the arrangement of the pixel data in the preview callback buffers. parameters data the contents of the p Review frame in the format defined by ImageFormat, which can be queried with getPreviewFormat (). if setPreviewFormat (int) is never called, the default will be the YCbCr_420_SP (NV21) format. camera the Camera service object. you can use getPreviewFormat () to query supported preview frames. If setPreviewFormat (INT) is never called, The YCbCr_420_SP format (NV21) is used by default ). In setPreviewFormat, it also says: [html] public void setPreviewFormat (int pixel_format) Added in API level 1 Sets the image format for preview pictures. if this is never called, the default format will be NV21, which uses the NV21 encoding format. use getSupportedPreviewFormats () to get a list of the available preview formats. it is stronugly recommended that either NV21 or YV12 is used, since they are support Ed by all camera devices. for YV12, the image buffer that is stored Ed is not necessarily tightly packed, as there may be padding at the end of each row of pixel data, as described in YV12. For camera callback data, it can be assumed that the stride of the Y and UV data is the smallest possible that meets the alignment requirements. that is, if the preview size is width x height, then the following Equations describe the buffer index for the beginning of row y for the Y plane and row c for the U and V planes: yStride = (int) ceil (width/16.0) * 16; uvStride = (int) ceil (yStride/2)/16.0) * 16; ySize = yStride * height; uvSize = uvStride * height/2; yRowIndex = yStride * y; uRowIndex = ySize + uvSize + uvStride * c; vRowIndex = ySize + uvStride * c; size = ySize + uvSize * 2; www. 2ct O.com strongly recommends NV21 and YV21 formats, which are NV21 by default, that is, YUV420SP. Therefore, BitmapFactory resolution cannot be successful without conversion. The same is true. Parsing mData directly produces the following error: In addition, NV21 is common. GetSupportedPreviewFormats () Added in API level 5 Gets the supported preview formats. NV21 is always supported. YV12 is always supported since API level 12. if YuvImage is too slow to compress and parse, you can only write the Conversion Function by yourself. There are three common types of functions on the Internet: 1. Here is only a coding framework. Refer to here: android real-time video collection-Camera preview Collection [java] // [interface for getting video preview frames] m1_previewcallback = new Camera. previewCallback () {@ Override public void onPreviewFrame (byte [] data, Camera camera) {// transmitted data, the default value is YUV420SP // TODO Auto-generated method stub try {Log. I (TAG, "going into onPreviewFrame"); // mYUV420sp = data; // obtain native YUV420SP data YUVIMGLEN = data. length; // copy the native yuv420sp data mYuvBufferlock. acquire (); System. arraycopy (data, 0, mYUV420SPSendBuffer, 0, data. length); // System. arraycopy (data, 0, mWrtieBuffer, 0, data. length); mYuvBufferlock. release (); // enable the encoding thread. For example, enable the PEG encoding thread mSendThread1.start ();} catch (Exception e) {Log. v ("System. out ", e. toString ();} // endtry} // endonPriview}; 2. Convert yuv420sp to rgb. Refer to here: android video collection [html] private void updateIM () {try {// parse YUV into RGB format decodeYUV420SP (byteArray, yuv420sp, width, height); DataBuffer dataBuffer = new DataBufferByte (byteArray, numBands); WritableRaster wr = Raster. createWritableRaster (sampleModel, dataBuffer, new Point (0, 0); im = new BufferedImage (cm, wr, false, null);} catch (Exception ex) {ex. printStackTrace () ;}} private static void decodeYUV420SP (byte [] rgbBuf, byte [] yuv420sp, int width, int height) {final int frameSize = width * height; if (rgbBuf = null) throw new NullPointerException ("buffer 'rgbbuf' is null"); if (rgbBuf. length <frameSize * 3) throw new IllegalArgumentException ("buffer 'rgbbuf' size" + rgbBuf. length + "<minimum" + frameSize * 3); if (yuv420sp = null) throw new NullPointerException ("buffer 'yuv420sp 'is null"); if (yuv420sp. length <frameSize * 3/2) throw new IllegalArgumentException ("buffer 'uv420sp 'size" + yuv420sp. length + "<minimum" + frameSize * 3/2); int I = 0, y = 0; int uvp = 0, u = 0, v = 0; int y1192 = 0, r = 0, g = 0, B = 0; for (int j = 0, yp = 0; j

Related Article

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.