Android Photo + square cut--with code and effect diagram

Source: Internet
Author: User

This article links http://blog.csdn.net/xiaodongrush/article/details/29173567

Reference link Http://stackoverflow.com/questions/12758425/how-to-set-the-output-image-use-com-android-camera-action-crop

1. Origins

To develop an Avatar upload module, the avatar upload process is divided into two steps. The first step is to take a camera or pick a photo from the gallery, create a photo, and a second step with the avatar cut, usually cut to square. The third step, upload avatar, delete unnecessary cache files.
Photo and Gallery Select photos can be used for system scenarios. Homemade camera can make filter, this development cost is bigger, general app also does not need to support. Gallery Select Photos This can be done by yourself, access to the SD card, relatively simple. The problem is on the picture clipping. Some technical solutions on the net, after the migration, the effect is not good, such as the sensitivity of scaling, scaling after the problem of inaccurate tailoring, scaling is not smooth. Later found that using Com.android.camera.action.CROP can call the system clipping page, but this page is not the official public page, so some vendors may not support this. t_t.
Look at the next few apps, QQ, and easy to believe that they do, the effect is not very good, 360 mobile phone Assistant is the system of call. System interception is relatively smooth, the effect is better, in addition to the previous hidden trouble, there is a problem is, in some custom phone above, the overall clipping of the page is darker, or there is a layer of shadow, of course, the picture after the cut is normal.
Considering that 360 of mobile phone assistants have such a large user-size app, the system tailoring is also used, so we'll also consider using system tailoring, if we find that system clipping is not available, then call your own tailoring.

2. Photo Code

Here with the mediastore.extra_output, the photo image will not be returned through intnet, to be read by the URI, so that a smaller image can be read in. If this is not the case, the system will return a compressed image in the intent, how large the compressed images can not be controlled, so may be larger.

Intent newintent = new Intent (mediastore.action_image_capture); Newintent.putextra (Mediastore.extra_output, URI); Startactivityforresult (Newintent, Request_code_take_photo);
3. Square Cut

The code here is similar to taking pictures, and mediastore.extra_output is used, and the results are read by path.

Intent Intent = new Intent ("Com.android.camera.action.CROP"); Intent.setdataandtype (URI, "image/*"); Intent.putextra ( "Crop", "true"); Open Trim Intent.putextra ("Aspectx", 1); Wide-height ratio intent.putextra ("Aspecty", 1), Intent.putextra ("Outputx", 150); Intent.putextra ("Outputy"), Intent.putextra (Mediastore.extra_output, Uri.fromfile (new File) ( Moutputfile.getabsolutefile () + "tmp")); Startactivityforresult (Intent, Request_code_clip_photo);
4. A relatively complete code

Code download link containing apk http://download.csdn.net/detail/u011267546/7460367

public class Mainactivity extends Activity {private static final int request_code_take_photo = 0;    private static final int request_code_clip_photo = 1;    Private File Moutputfile;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);    Setcontentview (r.layout. Activity_main); } @Override protected void Onactivityresult (int requestcode, int resultcode, Intent data) {Super.onactivityr        Esult (Requestcode, ResultCode, data);        if (Requestcode = = Request_code_take_photo) {ontakephotofinished (ResultCode, data);        } else if (Requestcode = = Request_code_clip_photo) {onclipphotofinished (ResultCode, data);                }} public void OnClick (View v) {if (V.getid () = = R.id.take_photo) {if (Hascarema () = = False) {            Return        } takephoto (); }} private Boolean Hascarema () {Packagemanager pm = GetpackagemaNager (); if (!pm.hassystemfeature (Packagemanager.feature_camera) &&!pm.hassystemfeature (Packagemanager.fea            Ture_camera_front) {Toast. Maketext (This, "No CAMERA found", Toast.length_short). Show ();        return false;    } return true; private void Takephoto () {String Sdpath = Environment.getexternalstoragedirectory (). Getabsolu        Tepath ();        Moutputfile = new File (Sdpath, System.currenttimemillis () + ". tmp"); Uri uri = URI.        FromFile (Moutputfile);        Intent newintent = new Intent (mediastore.action_image_capture); Newintent.putextra (Mediastore.        Extra_output, URI);    Startactivityforresult (Newintent, Request_code_take_photo);            } private void ontakephotofinished (int resultcode, Intent data) {if (ResultCode = = result_canceled) { Toast.            Maketext (This, "Take photo Canceled", Toast.length_short). Show ();        Return } ELSE if (ResultCode! = RESULT_OK) {Toast. Maketext (This, "Take photo Failed", Toast.length_short)        . Show ();        } else {Clipphoto (Uri. FromFile (Moutputfile)); }}//http://www.xuanyusong.com/archives/1743 private void Clipphoto (Uri uri) {Intent Intent = new Inten        T ("Com.android.camera.action.CROP");        Intent.setdataandtype (URI, "image/*");        The following crop=true is set to set the display in the intent of the open view can be cropped intent.putextra ("crop", "true");        Aspectx Aspecty is a proportional Intent.putextra ("Aspectx", 1) of the wide height;        Intent.putextra ("Aspecty", 1);        Outputx Outputy is a cropped picture with a wide height of Intent.putextra ("Outputx", 150);        Intent.putextra ("Outputy", 150); Intent.putextra (Mediastore. Extra_output, Uri.        FromFile (New File (Moutputfile.getabsolutefile () + "tmp"));    Startactivityforresult (Intent, Request_code_clip_photo); } private void onclipphotofinished (int resultcode, Intent data){if (ResultCode = = result_canceled) {Toast. Maketext (This, "clip photo CANCELED", Toast.length_short)            . Show ();        Return                    } else if (resultcode! = RESULT_OK) {Toast. Maketext (This, "Take photo Failed", Toast.length_short)        . Show ();        } Bitmap BM = Bitmapfactory.decodefile (Moutputfile.getabsolutepath () + "tmp");        ImageView Photoiv = (ImageView) Findviewbyid (R.id.photo);    Photoiv.setimagebitmap (BM); }}
5.

Give a lovely bear, only the upper body of the Big Bear.


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.