Android to achieve photo capture function _android

Source: Internet
Author: User
Tags crop image

This article will show you how to take a screenshot of the photo.

First look at the effect chart:

The screenshot is a little bit special, but now the Android smartphone's camera is millions of pixels and the pictures are very large. Therefore, we can not use the bitmap as the photo album screenshot, regardless of the large map of the map are unified use of the URI operation.

First, prepare the URI you need to use:

Private

static 
final 
String image_file_location = "file:///sdcard/temp.jpg";//temp
 FILE

Uri
 Imageuri = Uri.parse (image_file_location);//the
 Uri to store the big bitmap


Second, the use of mediastore.action_image_capture can easily invoke the camera program to take photos:

Intent
 Intent = new

Intent (mediastore.action_image_capture);//action is
 CAPTURE

Intent.putextra (Mediastore.extra_output,
 Imageuri);

Startactivityforresult (Intent,
 take_big_picture);//or
 Take_small_picture

Third, you can then get the returned data (URI) in the Onactivityresult, and pass the URI to the screenshot of the program.

Switch

(requestcode) {

case

take_big_picture:

  log.d (TAG,
"take_big_picture:
 data =" 
+ data);//it
 seems to being null

  //todo
 sent to crop

  Cropimageuri (Imageuri,
 Crop_big_picture);

   

  break;

Case

Take_small_picture:

  log.i (TAG,
"take_small_picture:
 data =" 
+ data);

  TODO
 sent to crop 

  Cropimageuri (Imageuri,
 crop_small_picture);

   

  break;

Default: Break

  ;

}

As you can see, whether you're shooting a big picture or a small picture, you're using a URI, just a different size. We encapsulate this operation in one method

Private

void 
Cropimageuri (URI uri, int

outputx, int

outputy, int

requestcode) {

  Intent
 Intent = new

Intent ("Com.android.camera.action.CROP");

  Intent.setdataandtype (URI,
"image/*");

  Intent.putextra ("Crop",
"true");

  Intent.putextra ("Aspectx",
2);

  Intent.putextra ("Aspecty",
1);

  Intent.putextra ("Outputx",
 outputx);

  Intent.putextra ("Outputy",
 outputy);

  Intent.putextra ("scale",
true);

  Intent.putextra (mediastore.extra_output,
 URI);

  Intent.putextra ("Return-data",
false);

  Intent.putextra ("OutputFormat",
 Bitmap.CompressFormat.JPEG.toString ());

  Intent.putextra ("Nofacedetection",
true);
 no Face Detection

  startactivityforresult (Intent,
 requestcode);

}


Four, the last step, we have passed the data into the crop image program, the next thing to do is to process the returned data:

Switch

(requestcode) {

case

crop_big_picture://from
 crop_big_picture

  log.d (TAG,
"Crop_ Big_picture:
 data = " 
+ data";//it
 seems to is null

  if (Imageuri
 != null) {

    Bitmap
 Bitmap = Decodeuriasbitmap (Imageuri);

    Imageview.setimagebitmap (bitmap);

  }

  break;

Case

Crop_small_picture:

  if (Imageuri
 != null) {

    Bitmap
 Bitmap = Decodeuriasbitmap ( Imageuri);

    Imageview.setimagebitmap (bitmap);

  } else{

    log.e (TAG,
"crop_small_picture:
 data =" 
+ data);

  }

  break;

Default: Break

  ;

}

The above is the Android to achieve the camera screenshot function method, the hope is helpful to everybody's study.

Related Article

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.