Call system camera to take pictures and clip
In the Android development process, many times we need to call the camera to take pictures, especially when it comes to posting tweets.
Inheriting the camera class to write a photo function is obviously not the best solution because we can't consider it very comprehensively.
At this point, calling the system's camera is certainly a good solution.
Next, we'll write a call to the system camera to take a picture and clip the photo.
| The code is as follows |
Copy Code |
|
Intent Intent = new Intent (mediastore.action_image_capture);
Intent.putextra (Mediastore.extra_output, Uri.fromfile) (New File (environment.getexternalstoragedirectory () + "/", " Temp.jpg "));
Startactivityforresult (Intent, 1); |
The code above is to call the system camera to take a picture and save it to the SDcard directory temp.jpg
But this time is not enough, because the camera is usually photographed in large size, obviously can not be used for uploading, we need to edit the photos.
| The code is as follows |
Copy Code |
|
public void Startphotozoom (Uri uri) {
Intent Intent = new Intent ("Com.android.camera.action.CROP");
Intent.setdataandtype (URI, image_unspecified);
Intent.putextra ("Crop", "true");
Aspectx Aspecty is the ratio of wide to high
Intent.putextra ("Aspectx", 1);
Intent.putextra ("Aspecty", 1);
OUTPUTX, Outputy is cropped picture width high
Intent.putextra ("Outputx", 250);
Intent.putextra ("Outputy", 250);
Intent.putextra ("Return-data", true);
Startactivityforresult (Intent, Photoresoult);
}
|
The above code implements the clip of the picture, we just need to pass in the corresponding URI, then we pass in the URI.
| The code is as follows |
Copy Code |
|
File picture = new file (environment.getexternalstoragedirectory () + "/" + "temp.jpg");
Startphotozoom (Uri.fromfile (picture)); |
Of course, the clips are over, and we're sure to get the clips after the photos.
| The code is as follows |
Copy Code |
|
if (Requestcode = = Photoresoult) {
Bundle extras = Data.getextras ();
if (extras!= null) {
Bitmap photo = extras.getparcelable ("Data");
Bytearrayoutputstream stream = new Bytearrayoutputstream ();
if (photo!=null) {
Photo.compress (Bitmap.CompressFormat.JPEG, N, stream);//(0–100) compressed file
Pic.setimagebitmap (photo);
}
}
}
|
Android Call system camera take photos and clip
In this way, we can do the function of photographing and editing pictures by calling the system camera.
1. Call the system's camera program
Intent Intent = new Intent (mediastore.action_image_capture);
Startactivityforresult (Intent, take_photo_with_data);
2. Process the returned data in Onactivityresult
Final Bitmap photo = Data.getparcelableextra ("Data");
If you do not need to do shearing processing can be used directly to the picture, such as output to the ImageView
Imageview.setimagebitmap (photo);
If you need to do a cut process, continue down execution
3. Do the shearing processing
| The code is as follows |
Copy Code |
Intent Intent = new Intent ("Com.android.camera.action.CROP");
Intent.settype ("image/*");
Intent.putextra ("Data", data);
Intent.putextra ("Crop", "true");
Intent.putextra ("Aspectx", 1);
Intent.putextra ("Aspecty", 1);
Intent.putextra ("Outputx", 128);
Intent.putextra ("Outputy", 128);
Intent.putextra ("Return-data", true);
Startactivityforresult (Intent, photo_picked_with_data); |
4. Handling in Onactivityresult
Same as the second step.
Complete code:
| The code is as follows |
Copy Code |
|
public class Sdcarduritestactivity extends activity {
/** called the activity is a. */
Private Button btntake = null;
Private ImageView lblimage = null;
private static final int photo_picked_with_data = 3021;
private static final int camera_with_data = 3023;
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Btntake = (Button) Findviewbyid (R.id.btntake);
Lblimage = (ImageView) Findviewbyid (r.id.lblimage);
Btntake.setonclicklistener (New View.onclicklistener () {
@Override
public void OnClick (View v) {
TODO auto-generated Method Stub
Intent Intent = new Intent (mediastore.action_image_capture);
Startactivityforresult (Intent, camera_with_data);
}
});
}
@Override
protected void Onactivityresult (int requestcode, int resultcode, Intent data) {
TODO auto-generated Method Stub
if (RESULTCODE!=RESULT_OK)
Return
Switch (requestcode) {
Case Camera_with_data:
Final Bitmap photo = Data.getparcelableextra ("Data");
if (photo!=null) {
Docropphoto (photo);
}
Case Photo_picked_with_data:
Bitmap photo1 = Data.getparcelableextra ("Data");
if (photo1!=null) {
Lblimage.setimagebitmap (PHOTO1);
}
}
}
protected void Docropphoto (Bitmap data) {
Intent Intent = getcropimageintent (data);
Startactivityforresult (Intent, photo_picked_with_data);
}
public static Intent getcropimageintent (Bitmap data) {
Intent Intent = new Intent ("Com.android.camera.action.CROP");
Intent.settype ("image/*");
Intent.putextra ("Data", data);
Intent.putextra ("Crop", "true");
Intent.putextra ("Aspectx", 1);
Intent.putextra ("Aspecty", 1);
Intent.putextra ("Outputx", 128);
Intent.putextra ("Outputy", 128);
Intent.putextra ("Return-data", true);
return intent;
}
}
Layout file slightly
|