Share an Activity that can be directly inherited on the interface to be photographed, with detailed annotations and activity annotations.
You can directly inherit the Activity based on your project requirements on the page to take a photo. The Activity is accompanied by a detailed annotation. The photo cut topic will be explained in detail, and the finale will provide a complete summary,
Public abstract class RequestWithTakePhotoActivity extends BaseActivity {
Private static final String IMAGE_UNSPECIFIED = "image /*";
Private static final String IMAGE_TMP = "photo_tmp.png ";
Private static final String IMAGE_TMP_2 = "photo_tmp2.png ";
Private static final int TAKE_PHOTO = 0;
Private static final int PIC_PHOTO = 1;
// Private static final int CROP_PHOTO = 2;
Private static final int CROP_BIG_PICTURE = 3;
Private boolean enableCrop = true;
Private static final int MAX_LINE = 480;
Private Uri imageUri;
Protected PopDialog dialog = null;
Private ArrayList <Integer> save_images_arr_id = new ArrayList <Integer> ();
Private ArrayList <String> save_images_arr_paths = new ArrayList <String> ();
Public final static int GET_PHOTO_RESULT_ID = 6;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
File outFile = new File (getExternalCacheDir () + "/" + IMAGE_TMP );
ImageUri = Uri. fromFile (outFile );
// The onCreate method is not entered after the test. // incorrect. Although it is not a constructor, It is super when the subclass implements the onCrreate method. onCreate (savedInstanceState); indicates the code
Log. I ("ResquestWithTakePotoActivity", "ResquestWithTakePotoActivity ");
}
Protected void enableCrop (boolean flag) {// subclass is used to set whether to cut the flag.
EnableCrop = flag;
}
Public void imageChooseItem (boolean isShowSelectPhoto ){
LayoutInflater inflater = LayoutInflater. from (this );
View view = inflater. inflate (R. layout. take_photo_select, null );
View. findViewById (R. id. photo_delete). setVisibility (View. GONE );
View. findViewById (R. id. take_photo). setOnClickListener (
New OnClickListener (){
@ Override
Public void onClick (View v ){
StartActionCamera (); // after clicking the photo button, start the camera
If (dialog! = Null ){
Dialog. cancel ();
}
}
});
If (isShowSelectPhoto ){
View. findViewById (R. id. pic_photo). setVisibility (View. VISIBLE );
View. findViewById (R. id. take_photo_line). setVisibility (View. VISIBLE );
View. findViewById (R. id. pic_photo). setOnClickListener (
New OnClickListener (){
@ Override
Public void onClick (View v ){
// StartImagePick ();
SelectPhoto ();
If (dialog! = Null ){
Dialog. cancel ();
}
}
});
} Else {
View. findViewById (R. id. take_photo_line). setVisibility (View. GONE );
View. findViewById (R. id. pic_photo). setVisibility (View. GONE );
}
Dialog = DialogUtil. showInfo (this,
GetResources (). getString (R. string. title_select), view,
New DialogInterface. OnClickListener (){
@ Override
Public void onClick (DialogInterface dialog, int which ){
Dialog. cancel ();
}
}, False, false );
}
Public void imageChooseItemWithDelete (int type, final int index,
Boolean isSetBitmap ){
LayoutInflater inflater = LayoutInflater. from (this );
View view = inflater. inflate (R. layout. take_photo_select, null );
Log. e ("hf: issetbitamp", isSetBitmap + "");
If (isSetBitmap ){
View. findViewById (R. id. take_photo_line). setVisibility (View. VISIBLE );
View. findViewById (R. id. photo_delete). setVisibility (View. VISIBLE );
} Else {
View. findViewById (R. id. photo_delete). setVisibility (View. GONE );
View. findViewById (R. id. take_photo_line). setVisibility (View. GONE );
}
View. findViewById (R. id. pic_photo). setOnClickListener (
New OnClickListener (){
@ Override
Public void onClick (View v ){
// StartImagePick ();
SelectPhoto ();
If (dialog! = Null ){
Dialog. cancel ();
}
}
});
View. findViewById (R. id. take_photo). setOnClickListener (
New OnClickListener (){
@ Override
Public void onClick (View v ){
StartActionCamera ();
If (dialog! = Null ){
Dialog. cancel ();
}
}
});
View. findViewById (R. id. photo_delete). setOnClickListener (
New OnClickListener (){
@ Override
Public void onClick (View v ){
DeleteImage (index );
If (dialog! = Null ){
Dialog. cancel ();
}
}
});
Dialog = DialogUtil. showInfo (this,
GetResources (). getString (R. string. title_select), view,
New DialogInterface. OnClickListener (){
@ Override
Public void onClick (DialogInterface dialog, int which ){
Dialog. cancel ();
}
}, False, false );
}
Abstract protected void deleteImage (int index );
Private void startActionCamera () {// jump to the camera interface in startActivityForResult Mode
Intent intent = new Intent (MediaStore. ACTION_IMAGE_CAPTURE); // action is
// Capture
Intent. putExtra (MediaStore. EXTRA_OUTPUT, imageUri); // Save the image address
StartActivityForResult (intent, TAKE_PHOTO );
}
Private void startImagePick (Uri uri ){
Intent intent = new Intent ("com. android. camera. action. CROP ");
Intent. setDataAndType (uri, "image /*");
Intent. putExtra ("crop", "true ");
Intent. putExtra ("aspectX", getCropX ());
Intent. putExtra ("aspectY", getCropY ());
Intent. putExtra ("outputX", getCropWidth ());
Intent. putExtra ("outputY", getCropHeight ());
Intent. putExtra ("scale", true );
Intent. putExtra ("return-data", false );
Intent. putExtra ("scaleUpIfNeeded", true); // when the image size is smaller than the output size, some machines intercept the Black edge of the image
Intent. putExtra (MediaStore. EXTRA_OUTPUT, imageUri );
Intent. putExtra ("outputFormat", Bitmap. CompressFormat. JPEG. toString ());
Intent. putExtra ("noFaceDetection", false); // no face detection
StartActivityForResult (intent, PIC_PHOTO );
}
Private void SelectPhoto (){
Intent intent = new Intent ();
Intent. setClass (this, SelectFromPhotoAlbumActivity. class );
Intent. putExtra ("TYPE", "single_pic ");
Intent. putExtra ("SELECTED_IMAGES", save_images_arr_paths );
Intent. putExtra ("SELECTED_IMAGES_ID", save_images_arr_id );
StartActivityForResult (intent, GET_PHOTO_RESULT_ID );
}
@ Override
Protected void onActivityResult (int requestCode, int resultCode, Intent data ){
Switch (requestCode ){
Case TAKE_PHOTO:
Log. e ("hfdd:", imageUri + "");
If (resultCode! = Activity. RESULT_CANCELED ){
CropImageUri (imageUri, CROP_BIG_PICTURE );
}
Break;
Case CROP_BIG_PICTURE: // from crop_big_picture
Log. e ("hf:", imageUri + "");
If (imageUri! = Null ){
Bitmap bitmap = decodeUriAsBitmap (imageUri );
OnImageCroped (bitmap );
} Else {
Return;
}
Break;
Case PIC_PHOTO:
If (imageUri! = Null ){
Bitmap bitmap = decodeUriAsBitmap (imageUri );
OnImageCroped (bitmap );
} Else {
Return;
}
Break;
Case GET_PHOTO_RESULT_ID:
If (data! = Null ){
ArrayList <Integer> images_arr_id = data
. GetIntegerArrayListExtra ("select_photos ");
ArrayList <String> images_arr_paths = data
. GetStringArrayListExtra ("select_photos_paths ");
Uri uri = Uri. parse ("file: //" + images_arr_paths.get (0 ));
StartImagePick (uri );
}
Break;
}
Super. onActivityResult (requestCode, resultCode, data );
}
Private Bitmap decodeUriAsBitmap (Uri uri ){
Bitmap bitmap = null;
Try {
Bitmap = BitmapFactory. decodeStream (getContentResolver ()
. OpenInputStream (uri ));
} Catch (FileNotFoundException e ){
E. printStackTrace ();
Return null;
}
Return bitmap;
}
Private void cropImageUri (Uri uri, int requestCode ){
Intent intent = new Intent ("com. android. camera. action. CROP ");
Intent. setDataAndType (uri, "image /*");
Intent. putExtra ("crop", "true ");
Intent. putExtra ("aspectX", getCropX ());
Intent. putExtra ("aspectY", getCropY ());
Intent. putExtra ("outputX", getCropWidth ());
Intent. putExtra ("outputY", getCropHeight ());
Intent. putExtra ("scale", true );
Intent. putExtra ("scaleUpIfNeeded", true); // when the image size is smaller than the output size, some machines intercept the Black edge of the image
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 );
}
Protected boolean savePicture (Bitmap image, File picture ){
If (! Picture. exists ()){
Try {
File path = new File (picture. getParent ());
If (! Path. exists ()){
If (! Path. mkdirs ()){
Return false;
}
}
If (! Picture. createNewFile ()){
Return false;
}
} Catch (IOException e ){
E. printStackTrace ();
Return false;
}
}
BufferedOutputStream stream;
Try {
Stream = new BufferedOutputStream (new FileOutputStream (picture ));
Image. compress (Bitmap. CompressFormat. JPEG, 100, stream );
Stream. close ();
} Catch (Exception e ){
E. printStackTrace ();
Return false;
}
Return true;
}
Abstract protected int getCropWidth ();
Abstract protected int getCropHeight ();
Abstract protected int getCropX ();
Abstract protected int getCropY ();
Abstract protected void onImageCroped (Bitmap image );
Abstract protected void onImageSelected (Uri uri );
}