需要拍照的界面可以根据自己项目需求直接继承该Activity,附带详细注解,拍照剪切专题会进行详细讲解,大结局篇给出一个完整的总结,
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);
//测试后会发现并没有进入到onCreate方法//不对,虽然不是构造方法,但是子类实现onCrreate方法的时候的super.onCreate(savedInstanceState);就会指向这里的代码
Log.i("ResquestWithTakePotoActivity", "拍照时进入到ResquestWithTakePotoActivity");
}
protected void enableCrop(boolean 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();//拍照按钮点击后,启动照相机
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() {//以startActivityForResult的方式跳转到照相机界面
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);// action is
// capture
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);//另存图片地址
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);// 解决当图片大小小于输出大小时,部分机器截取图片黑边
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);// 解决当图片大小小于输出大小时,部分机器截取图片黑边
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);
}