Android calls camera to capture and crop pictures, call albums in your phone and crop pictures

Source: Internet
Author: User

In the Android app, very often we need to implement upload images, or directly call the camera on the phone to take photos processing and then directly display and upload the function, the following will be called the camera to take pictures of the image processing and then display and call the photo processing in the mobile phone register and then display the function, to achieve the upload function. It is generally uploaded to the database, and the pictures in the ImageView are taken out and stored in the database.

The steps to implement are described below:

1. Call the picture in the album to crop and then display.

1.1 Use intent to get photos selected from the albums.

1.2 Crop the captured picture. Crop processing is also implemented using the intent call of Android's own cropping feature.

1.3 The processed data is displayed in the ImageView.

2. Call the camera function to get the picture cropped and then displayed.

2.1 Use Intent to get photos taken from a photo.

2.2 Crop processing of the acquired image is also implemented using the intent call of Android's own cropping feature.

2.3 The processed data is displayed in the ImageView.

Note: The two implementations above are essentially learning how to use intent to invoke the features of Android and the various files on the phone.

Here's the code for the implementation:

1. Layout files

<?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 ">    <imageview android:id=" @+id/imageview "        android:adjustviewbounds=" true "        Android: layout_gravity= "center"        android:minwidth= "150dip"        android:minheight= "150dip"        android:layout_width= "Wrap_content"        android:layout_height= "wrap_content"/>    <button android:id= "@+id/btnphone"        Android:layout_width= "Fill_parent"        android:layout_height= "wrap_content"        android:text= "albums"/>    <button android:id= "@+id/btntakepicture"        android:layout_height= "wrap_content"        android:layout_width = "Fill_parent"        android:text= "take pictures"/></linearlayout>


2.activity file

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.widget.button;import    Android.widget.imageview;public class Mainactivity extends Activity {private static final int NONE = 0; private static final int photo_graph = 1;//camera private static final int photo_zoom = 2;    Scale private static final int photo_resoult = 3;//result private static final String image_unspecified = "image/*";    Private ImageView ImageView = null;    Private Button btnphone = null;    Private Button btntakepicture = null;        @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);                Setcontentview (R.layout.activity_main);        ImageView = (ImageView) Findviewbyid (R.id.imageview); Btnphone =(Button) Findviewbyid (R.id.btnphone);        Btnphone.setonclicklistener (Onclicklistener);        Btntakepicture = (Button) Findviewbyid (r.id.btntakepicture);    Btntakepicture.setonclicklistener (Onclicklistener); } private Final View.onclicklistener Onclicklistener = new View.onclicklistener () {@Override public  void OnClick (View v) {if (V==btnphone) {//Get picture from album Intent Intent = new Intent (Intent.action_pick,                NULL);                Intent.setdataandtype (MediaStore.Images.Media.EXTERNAL_CONTENT_URI, image_unspecified);            Startactivityforresult (Intent, photo_zoom);                }else if (v==btntakepicture) {//capture picture from photo Intent Intent = new Intent (mediastore.action_image_capture); Intent.putextra (Mediastore.extra_output, Uri.fromfile (new File (environment. Getexternalstorage                Directory (), "temp.jpg")));            Startactivityforresult (Intent, photo_graph);   }        } }; @Override protected void Onactivityresult (int requestcode, int resultcode, Intent data) {if (ResultCode = = NONE        ) return; Photo if (Requestcode = = photo_graph) {//Set file save path Files picture = new File (Environment.getex            Ternalstoragedirectory () + "/temp.jpg");        Startphotozoom (Uri.fromfile (picture));        } if (data = = null) return;        Read the brochure zoom picture if (Requestcode = = photo_zoom) {startphotozoom (Data.getdata ());            }//Processing result if (Requestcode = = photo_resoult) {Bundle extras = Data.getextras ();                if (extras! = null) {Bitmap photo = extras.getparcelable ("Data");                Bytearrayoutputstream stream = new Bytearrayoutputstream (); Photo.compress (Bitmap.CompressFormat.JPEG, (), stream);//(0-100) Compress file//here can save Bitmap to SD card, see more: http://www. Cnblogs.com/linjiqin/archive/2011/12/28/2304940.html imageview.setimagebitmap (photo);    Display the picture on the ImageView control}} super.onactivityresult (Requestcode, ResultCode, data); }/** * Shrink picture * * @param uri */public void Startphotozoom (Uri uri) {Intent Intent = new Int        ENT ("Com.android.camera.action.CROP");//Call the Android system comes with a picture clipping page, Intent.setdataandtype (URI, image_unspecified);        Intent.putextra ("Crop", "true");//trimming//Aspectx aspecty is a proportional intent.putextra of width ("Aspectx", 1);        Intent.putextra ("Aspecty", 1);        Outputx Outputy is a cropped picture with a wide height of Intent.putextra ("Outputx", 300);        Intent.putextra ("Outputy", 500);        Intent.putextra ("Return-data", true);    Startactivityforresult (Intent, Photo_resoult); }}


Android calls camera to capture and crop pictures, call albums in your phone and crop 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.