Http://blog.csdn.net/xys289187120/article/details/6575830
In Android, if you want to process images, you can call the system controls to achieve very good effects of scaling and cutting images. Today, I wrote a demo to share with you.
[Java]
View plaincopyprint?
- Package CN. m15.test;
- Import java. Io. bytearrayoutputstream;
- Import java. Io. file;
- Import Android. App. activity;
- Import Android. content. intent;
- Import Android. Graphics. Bitmap;
- Import android.net. Uri;
- Import Android. OS. Bundle;
- Import Android. OS. environment;
- Import Android. provider. mediastore;
- Import Android. View. view;
- Import Android. View. View. onclicklistener;
- Import Android. widget. Button;
- Import Android. widget. imageview;
- Public class testactivity
Extends activity {
- Public static
Final int none =
0;
- Public static
Final int photohraph = 1; // photograph
- Public static
Final int photozoom =
2; // zoom
- Public static
Final int photoresoult =
3; // result
- Public static
Final string image_unspecified = "image /*";
- Imageview = NULL;
- Button button0 = NULL;
- Button button1 = NULL;
- @ Override
- Public void oncreate (bundle savedinstancestate ){
- Super. oncreate (savedinstancestate );
- Setcontentview (R. layout. Main );
- Imageview = (imageview) findviewbyid (R. Id. imageid );
- Button0 = (button) findviewbyid (R. Id. btn_01 );
- Button1 = (button) findviewbyid (R. Id. btn_02 );
- Button0.setonclicklistener (New onclicklistener (){
- @ Override
- Public void onclick (view v ){
- Intent intent = new intent (intent. action_pick,
Null );
- Intent. setdataandtype (mediastore. Images. Media. external_content_uri, image_unspecified );
- Startactivityforresult (intent, photozoom );
- }
- });
- Button1.setonclicklistener (New onclicklistener (){
- @ Override
- Public void onclick (view v ){
- Intent intent = new intent (mediastore. action_image_capture );
- Intent. putextra (mediastore. extra_output, Uri. fromfile (new file (environment. getexternalstoragedirectory (),
"Temp.jpg ")));
- Startactivityforresult (intent, photohraph );
- }
- });
- }
- @ Override
- Protected void onactivityresult (INT requestcode,
Int resultcode, intent data ){
- If (resultcode = none)
- Return;
- // Take a photo
- If (requestcode = photohraph ){
- // Set the file storage path, which is stored in the following directory.
- File picture = new file (environment. getexternalstoragedirectory () +
"/Temp.jpg ");
- Startphotozoom (URI. fromfile (picture ));
- }
- If (Data = NULL)
- Return;
- // Read the scaled image of the album
- If (requestcode = photozoom ){
- Startphotozoom (data. getdata ());
- }
- // Processing result
- If (requestcode = photoresoult ){
- Bundle extras = data. getextras ();
- If (extras! = NULL ){
- Bitmap photo = extras. getparcelable ("data ");
- Bytearrayoutputstream stream = new bytearrayoutputstream ();
- Photo. Compress (bitmap. compressformat. JPEG, 75, stream); // (0-100) compressed file
- Imageview. setimagebitmap (photo );
- }
- }
- Super. onactivityresult (requestcode, resultcode, data );
- }
- Public void startphotozoom (URI ){
- Intent intent = new intent ("com. Android. Camera. Action. Crop ");
- Intent. setdataandtype (Uri, image_unspecified );
- Intent. putextra ("crop ",
"True ");
- // Aspectx aspecty is the aspect ratio
- Intent. putextra ("aspectx ",
1 );
- Intent. putextra ("aspecty", 1 );
- // Outputx outputy indicates the width and height of the cropped image.
- Intent. putextra ("outputx", 64 );
- Intent. putextra ("outputy ",
64 );
- Intent. putextra ("Return-Data ",
True );
- Startactivityforresult (intent, photoresoult );
- }
- }
Package CN. m15.test; </P> <p> Import Java. io. bytearrayoutputstream; <br/> Import Java. io. file; <br/> Import android. app. activity; <br/> Import android. content. intent; <br/> Import android. graphics. bitmap; <br/> Import android.net. uri; <br/> Import android. OS. bundle; <br/> Import android. OS. environment; <br/> Import android. provider. mediastore; <br/> Import android. view. view; <br/> Import android. view. view. oncli Cklistener; <br/> Import android. widget. button; <br/> Import android. widget. imageview; </P> <p> public class testactivity extends activity {</P> <p> Public static final int none = 0; <br/> Public static final int photohraph = 1; // photograph <br/> Public static final int photozoom = 2; // scale <br/> Public static final int photoresoult = 3; // result </P> <p> Public static final string image_unspecified = "image /*"; <br/> IMA Geview imageview = NULL; <br/> button button0 = NULL; <br/> button button1 = NULL; </P> <p> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); <br/> imageview = (imageview) findviewbyid (R. id. imageid); <br/> button0 = (button) findviewbyid (R. id. btn_01); <br/> button1 = (button) findviewbyid (R. id. btn_02); </P> <p> button 0. setonclicklistener (New onclicklistener () {<br/> @ override <br/> Public void onclick (view v) {<br/> intent = new intent (intent. action_pick, null); <br/> intent. setdataandtype (mediastore. images. media. external_content_uri, image_unspecified); <br/> startactivityforresult (intent, photozoom); <br/>}< br/> }); </P> <p> button1.setonclicklistener (New onclicklistener () {</P> <p> @ override <br/> Public void Onclick (view v) {<br/> intent = new intent (mediastore. action_image_capture); <br/> intent. putextra (mediastore. extra_output, Uri. fromfile (new file (environment. getexternalstoragedirectory (), "temp.jpg"); <br/> startactivityforresult (intent, photohraph); <br/>}< br/> }); <br/>}</P> <p> @ override <br/> protected void onactivityresult (INT requestcode, int resultcode, intent data) {<br/> If (resultc Ode = none) <br/> return; <br/> // photograph <br/> If (requestcode = photohraph) {<br/> // set the file storage path, which is stored in the following directory <br/> file picture = new file (environment. getexternalstoragedirectory () + "/temp.jpg"); <br/> startphotozoom (URI. fromfile (picture); <br/>}</P> <p> If (Data = NULL) <br/> return; </P> <p> // read the scaled image of the album. <br/> If (requestcode = photozoom) {<br/> startphotozoom (data. getdata (); <br/>}< br/> // processing result <br/> If (reque Stcode = photoresoult) {<br/> bundle extras = data. getextras (); <br/> If (extras! = NULL) {<br/> bitmap photo = extras. getparcelable ("data"); <br/> bytearrayoutputstream stream = new bytearrayoutputstream (); <br/> photo. compress (bitmap. compressformat. JPEG, 75, stream); // (0-100) compressed file <br/> imageview. setimagebitmap (photo); <br/>}</P> <p> super. onactivityresult (requestcode, resultcode, data); <br/>}</P> <p> Public void startphotozoom (URI) {<br/> intent = new intent ("com. android. camera. action. crop "); <br/> intent. setdataandtype (Uri, image_unspecified); <br/> intent. putextra ("crop", "true"); <br/> // aspectx aspecty is the ratio of width to height <br/> intent. putextra ("aspectx", 1); <br/> intent. putextra ("aspecty", 1); <br/> // outputx outputy is the width and height of the cropped image. <br/> intent. putextra ("outputx", 64); <br/> intent. putextra ("outputy", 64); <br/> intent. putextra ("Return-Data", true); <br/> startactivityforresult (intent, photoresoult); <br/>}< br/>}[HTML]
View plaincopyprint?
- <? XML
Version = "1.0"
Encoding = "UTF-8"?>
- <Linearlayout
Xmlns: Android = "http://schemas.android.com/apk/res/android"
- Android: Orientation = "vertical"
Android: layout_width = "fill_parent"
- Android: layout_height = "fill_parent">
- <Textview
Android: layout_width = "fill_parent"
- Android: layout_height = "wrap_content"
Android: text = "@ string/hello"
/>
- <Imageview
Android: Id = "@ + ID/imageid"
- Android: adjustviewbounds = "true"
Android: maxwidth = "50dip"
- Android: maxheight = "50dip"
Android: layout_width = "wrap_content"
- Android: layout_height = "wrap_content"
/>
- <Button
Android: Id = "@ + ID/btn_01"
Android: layout_height = "50dip"
- Android: text = "album"
Android: layout_width = "150dip"/>
- <Button
Android: Id = "@ + ID/btn_02"
Android: layout_height = "50dip"
- Android: text = "photo"
Android: layout_width = "150dip"/>
- </Linearlayout>