Reproduced: Android call album, photo zoom, cut pictures

Source: Internet
Author: User
Tags crop image

For several days did not write a blog, feel a little slack. The author participated in the second Software design contest for college students, these days

I've been working on the big game and didn't spend a lot of time organizing blogs. Fortunately, after a few days, the game has been pretty much the same.

The next step is to take this time to learn something useful to do some summary.

What you do today is the area clipping function that implements the image. Due to the need of the project function, I need to implement PDF document

Read, and "picture" is clipped on a page (a page is understood as a picture). The author is not familiar with the aspect

So I have to go online to check the information. After I found the Android can call the system album, take pictures to implement the picture

Crop, zoom function.

This process is like uploading an avatar in an app and adjusting the avatar. Now make a record and share.

First of all, let's take a look at the actual!

The first interface:

Next:

See how the area is actually cropped? It is important to note that this is the system's own function to implement.

How is it implemented? The source code is as follows:

directly see the activity of the code, layout files are not given here (relatively simple, just a button)

 PackageCom.xiaoma.piccut.demo;ImportJava.io.File;Importandroid.app.Activity;ImportAndroid.app.AlertDialog;ImportAndroid.content.DialogInterface;Importandroid.content.Intent;ImportAndroid.graphics.Bitmap;Importandroid.graphics.drawable.BitmapDrawable;Importandroid.graphics.drawable.Drawable;ImportAndroid.net.Uri;ImportAndroid.os.Bundle;Importandroid.os.Environment;ImportAndroid.provider.MediaStore;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button;ImportAndroid.widget.ImageButton;ImportAndroid.widget.ImageView;/*** Call the system album or take photos to achieve the image clipping, zooming *@authorKiritor **/ Public classPiccutdemoactivityextendsActivity {PrivateButton BTN =NULL; /**Called when the activity is first created.*/@Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.main); //Initializeinit (); }        /*** Initialization method implementation*/    Private voidinit () {btn=(Button) Findviewbyid (R.id.button1); Btn.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {showpickdialog ();    }        }); }        /*** Control Click event Implementation * * Because a friend asked how the background of the different controls is cropped, * I've used three controls in this place, just for my own records * people feel useless can skip*/        /*** Select Prompt dialog box*/    Private voidShowpickdialog () {NewAlertdialog.builder ( This). Settitle ("Set Avatar ..."). Setnegativebutton ("Albums",NewDialoginterface.onclicklistener () { Public voidOnClick (Dialoginterface Dialog,intwhich)                                                {Dialog.dismiss (); Intent Intent=NewIntent (Intent.action_pick,NULL); /*** The following sentence, written in other ways is the same effect, if: * Intent.setdata (MediaStore.Images.Media.EXT                         Ernal_content_uri);                         * Intent.settype ("image/*"); Set data type * If your friends want to limit the types of images uploaded to the server, you can write them directly such as: "Image/jpeg, image/png, etc." */Intent.setdataandtype (MediaStore.Images.Media.EXTERNAL_CONTENT _uri,"Image/*"); Startactivityforresult (Intent,1); }}). Setpositivebutton ("Take pictures",NewDialoginterface.onclicklistener () { Public voidOnClick (Dialoginterface Dialog,intWhichbutton)                        {Dialog.dismiss (); Intent Intent=NewIntent (mediastore.action_image_capture); //The following sentence specifies the path of the photo store after the camera is calledIntent.putextra (Mediastore.extra_output, Uri. FromFile (NewFile (Environment. getExternalStorageDirectory (), "Xiaoma.jpg"))); Startactivityforresult (Intent,2);    }}). Show (); } @Overrideprotected voidOnactivityresult (intRequestcode,intResultCode, Intent data) {        Switch(requestcode) {//If you get it directly from the album         Case1: Startphotozoom (Data.getdata ());  Break; //If you are calling the camera to take a picture         Case2: File Temp=NewFile (environment.getexternalstoragedirectory ()+ "/test.jpg");            Startphotozoom (Uri.fromfile (temp));  Break; //get the cropped picture         Case3:            if(Data! =NULL) {Setpictoview (data); }             Break; default:             Break; }        Super. Onactivityresult (Requestcode, ResultCode, data); }        /*** Crop Image method Implementation *@paramURI*/     Public voidstartphotozoom (Uri uri) {Intent Intent=NewIntent ("Com.android.camera.action.CROP"); Intent.setdataandtype (URI,"Image/*"); //The following crop=true is set to set the display in the open intent view can be croppedIntent.putextra ("Crop", "true"); //Aspectx Aspecty is the ratio of width to heightIntent.putextra ("Aspectx", 1); Intent.putextra ("Aspecty", 1); //Outputx Outputy is the cropped picture width highIntent.putextra ("Outputx", 150); Intent.putextra ("Outputy", 150); Intent.putextra ("Return-data",true); Startactivityforresult (Intent,3); }        /*** Save image data after cropping *@paramPicdata*/    Private voidSetpictoview (Intent picdata) {Bundle Extras=Picdata.getextras (); if(Extras! =NULL) {Bitmap photo= extras.getparcelable ("Data"); Drawable drawable=Newbitmapdrawable (photo); }    }}

Reproduced: Android call album, photo zoom, cut pictures

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.