这篇文章主要介绍Android如何实现裁剪照片功能,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
从相册选择照片进行裁剪
从相册选择照片并裁剪:
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, "imagepublic 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); }}
以上是“Android如何实现裁剪照片功能”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注编程网行业资讯频道!