Android and androidadt

Source: Internet
Author: User

Android and androidadt


// Save a photo 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 (); // if the 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 as YuvImage // compress the image. compressToJpeg (new Rect (0, 0, image. getWidth (), image. getHeight (), 70, filecon); // compress an NV21 image with a quality of 70 to Jpeg and obtain the JPEG data stream} catch (IOException e) {e. printStackTrace ();}}

This method is often used in Camera.PreviewCallbackUsed in:

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



Compress NV21 data into JPEG, obtain JPEG byte data, and decompress JPEG byte data into a Bitmap

@ Overridepublic void onPreviewFrame (byte [] bytes, Camera camera) {YuvImage image = new YuvImage (bytes, ImageFormat. NV21, width, height, null); // ImageFormat. NV21 640 480 ByteArrayOutputStream outputSteam = new ByteArrayOutputStream (); image. compressToJpeg (new Rect (0, 0, image. getWidth (), image. getHeight (), 70, outputSteam); // compress an NV21 image with a quality of 70 to Jpeg, and obtain the JPEG data stream byte [] bytes DATA = outputSteam. toByteArray (); // get byte data Options = new BitmapFactory from outputSteam. options (); options. inSampleSize = 1; Bitmap bmp = BitmapFactory. decodeStream (writable data, null, options );}


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

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.