本文实例为大家分享了Android实现裁剪照片功能的具体代码,供大家参考,具体内容如下
1. 从相册选择照片进行裁剪
从相册选择照片并裁剪:
private void cropFromGallery() {
// TODO Auto-generated method stub
Intent intent=new Intent();
intent.setAction(Intent.ACTION_PICK);//Pick an item from the data
intent.setType("image
private void cropFromTake() {
// TODO Auto-generated method stub
Intent intent=new Intent();
intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);//设置Action为拍照
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);//将拍取的照片保存到指定URI
startActivityForResult(intent, TAKE_PIC);
}
裁剪已经排好的照片并显示在ImageView上:
case TAKE_PIC:
if (resultCode==RESULT_OK) {
cropImageUri(imageUri, 600, 600, CROP_PIC);
}
break;
private void cropImageUri(Uri imageUri, int outputX, int outputY, int requestCode){
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(imageUri, "image
public class MainActivity extends ActionBarActivity {
private final static int SELECT_PIC=0x123;
private final static int TAKE_PIC=0x124;
private final static int CROP_PIC=0x125;
private Uri imageUri;
private ImageView imgShow;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化imageUri
imageUri=Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "test.jpg"));
imgShow=(ImageView)findViewById(R.id.imgShow);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
switch (requestCode) {
case SELECT_PIC:
if (resultCode==RESULT_OK) {
try {
Bitmap bitmap=BitmapFactory.decodeStream(getContentResolver().
openInputStream(imageUri));//将imageUri对象的图片加载到内存
imgShow.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
break;
case TAKE_PIC:
if (resultCode==RESULT_OK) {
cropImageUri(imageUri, 600, 600, CROP_PIC);
}
break;
case CROP_PIC:
if (resultCode==RESULT_OK) {
try {
Bitmap bitmap=BitmapFactory.decodeStream(getContentResolver().
openInputStream(imageUri));//将imageUri对象的图片加载到内存
imgShow.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
break;
default:
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
private void cropImageUri(Uri imageUri, int outputX, int outputY, int requestCode){
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(imageUri, "image
private void cropFromTake() {
// TODO Auto-generated method stub
Intent intent=new Intent();
intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);//设置Action为拍照
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);//将拍取的照片保存到指定URI
startActivityForResult(intent, TAKE_PIC);
}
private void cropFromGallery() {
// TODO Auto-generated method stub
Intent intent=new Intent();
intent.setAction(Intent.ACTION_PICK);//Pick an item from the data
intent.setType("image/*");//从所有图片中进行选择
intent.putExtra("crop", "true");//设置为裁切
intent.putExtra("aspectX", 1);//裁切的宽比例
intent.putExtra("aspectY", 1);//裁切的高比例
intent.putExtra("outputX", 600);//裁切的宽度
intent.putExtra("outputY", 600);//裁切的高度
intent.putExtra("scale", true);//支持缩放
intent.putExtra("return-data", false);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);//将裁切的结果输出到指定的Uri
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());//裁切成的图片的格式
intent.putExtra("noFaceDetection", true); // no face detection
startActivityForResult(intent, SELECT_PIC);
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程网。