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.PreviewCallback
Used 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