Http://www.linuxidc.com/Linux/2014-12/110924.htm
Packagecom.klp.demo_025;ImportJava.io.ByteArrayInputStream;ImportJava.io.ByteArrayOutputStream;ImportJava.io.File;ImportJava.io.FileOutputStream;Importandroid.app.Activity;Importandroid.content.Intent;ImportAndroid.graphics.Bitmap;Importandroid.graphics.BitmapFactory;ImportAndroid.net.Uri;ImportAndroid.os.Bundle;ImportAndroid.provider.MediaStore;ImportAndroid.view.View;ImportAndroid.widget.Button;ImportAndroid.widget.ImageView; Public classMainactivityextendsActivity {PrivateImageView IV; Privatebutton button; Privatefile file; PrivateURI Uri; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); IV=(ImageView) Findviewbyid (R.ID.IMAGEVIEW1); Button=(Button) Findviewbyid (R.id.button1); File=NewFile ( This. Getexternalfilesdir (NULL), "Image.jpg"); URI=uri.fromfile (file); Button.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {//TODO auto-generated Method StubIntent Intent =NewIntent (mediastore.action_image_capture); Intent.putextra (MediaStore.Images.Media.ORIENTATION,0); Intent.putextra (Mediastore.extra_output, URI); Startactivityforresult (Intent,2); } }); } @Overrideprotected voidOnactivityresult (intRequestcode,intResultCode, Intent data) { //TODO auto-generated Method Stub Super. Onactivityresult (Requestcode, ResultCode, data); if(ResultCode = =RESULT_OK) { if(Requestcode = = 2) {startphotozoom (URI); } Else { Try{bitmapfactory.options Options=Newbitmapfactory.options (); Options.insamplesize= 2; Bitmap Bitmap=Bitmapfactory.decodefile (File.getpath (), options); //Compress Pictures//bitmap = Compressimage (bitmap,500); if(Bitmap! =NULL) { //Show PicturesIv.setimagebitmap (bitmap); //Save PictureFileOutputStream fos =NULL; FOS=Newfileoutputstream (file); Bitmap.compress (Bitmap.CompressFormat.JPEG,100, FOS); Fos.flush (); Fos.close (); } } Catch(Exception e) {//Todo:handle Exception } } } } /*** Crop image * *@paramURI*/ Public voidstartphotozoom (Uri uri) {Intent Intent=NewIntent ("Com.android.camera.action.CROP"); Intent.setdataandtype (URI,"Image/*"); Intent.putextra ("Crop", "true");//Crop=true has this sentence to get out of the final clipping page.Intent.putextra ("Aspectx", 1);//The two items are the proportions of the cropping box.Intent.putextra ("Aspecty", 1);//x:y=1:1Intent.putextra ("Outputx", 200);//Image Output SizeIntent.putextra ("Outputy", 200); Intent.putextra ("Output", URI); Intent.putextra ("OutputFormat", "JPEG");//return FormatStartactivityforresult (Intent, 3); } /*** Compress picture image into a picture sized size (size for picture, in kilobytes) * *@paramImage * Image resource *@paramSize * Picture sizes *@returnBitmap*/ PrivateBitmap Compressimage (Bitmap image,intsize) {Bytearrayoutputstream BAOs=NewBytearrayoutputstream (); //mass compression method, where 100 means no compression, the compressed data stored in the BAOsImage.compress (Bitmap.CompressFormat.JPEG, 100, BAOs); intoptions = 100; //loop to determine if the image is larger than 100kb after compression while(Baos.tobytearray (). length/1024 >size) { //Reset BAOs is empty BAOsBaos.reset (); //each time, it's reduced by tenOptions-= 10; //this compresses the options% and stores the compressed data in the BAOs .image.compress (Bitmap.CompressFormat.JPEG, Options, BAOs); } //Store the compressed data BAOs in the BytearrayinputstreamBytearrayinputstream ISBM =NewBytearrayinputstream (Baos.tobytearray ()); //generate images from the Bytearrayinputstream dataBitmap Bitmap = Bitmapfactory.decodestream (ISBM,NULL,NULL); returnbitmap; } }
Android Camera Photo Compression