Android-Save NV21 image as jpeg

Source: Internet
Author: User


Save a picture String FileName = "Img_" + string.valueof (Index) + ". jpg";  JPEG file name definition file Sdroot = environment.getexternalstoragedirectory ();    System path String dir = "/jpeg/";   Folder name File MkDir = new file (Sdroot, dir);     if (!mkdir.exists ()) {   mkdir.mkdirs ();   Directory does not exist, create}file picturefile = new File (sdroot, dir + fileName), if (!picturefile.exists ()) {   try {       Picturefile.createnewfile ();       FileOutputStream Filecon = new FileOutputStream (picturefile);       Yuvimage image = new Yuvimage (data, imageformat.nv21, width, height, null);   Save NV21 data to Yuvimage       //image compression       image.compresstojpeg (               new Rect (0, 0, image.getwidth (), Image.getheight ()),               Filecon);   Compress the NV21 format picture to a JPEG with a mass of 70 and get the JPEG data stream   }catch (IOException e)   {       e.printstacktrace ();   }}

This approach, often in Camera.PreviewCallbackUsed in:

@Overridepublic void Onpreviewframe (byte[] data, Camera camera) {}



Compress the NV21 data into JPEG and get the JPEG byte data, unzip the JPEG byte data into a single bitmap

@Overridepublic void Onpreviewframe (byte[] bytes, camera camera) {    yuvimage image = New Yuvimage (bytes, IMAGEFORMAT.N V21, width, height, null);            IMAGEFORMAT.NV21  640 480    bytearrayoutputstream outputsteam = new Bytearrayoutputstream ();    Image.compresstojpeg (New Rect (0, 0, image.getwidth (), Image.getheight ()),, Outputsteam); The NV21 format picture, compressed to a quality of 70 jpeg, and get JPEG data stream    byte[] jpegdata = Outputsteam.tobytearray ();                                                Get byte data from outputsteam options    options = new Bitmapfactory.options ();    Options.insamplesize = 1;    Bitmap bmp = Bitmapfactory.decodestream (Jpegdata, NULL, options);}


http://my.oschina.net/eclipse88/blog/80115

Android-Save NV21 image as jpeg

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.