Android裁剪照片功能
CrazyCodeBoy 人气:01. 从相册选择照片进行裁剪
从相册选择照片并裁剪:
/** * 从相册选择照片进行裁剪 */ 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); }
将裁减好的照片显示在显示在ImagaView上:
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;
程序运行效果图:
2. 从相机拍取照片进行裁剪
控制相机拍照并将照片保存到指定位置:
/** * 从相机拍取照片进行裁剪 */ 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;
/** * 裁剪指定uri对应的照片 * @param imageUri:uri对应的照片 * @param outputX:裁剪宽 * @param outputY:裁剪高 * @param requestCode:请求码 */ private void cropImageUri(Uri imageUri, int outputX, int outputY, int requestCode){ Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(imageUri, "image/*"); intent.putExtra("crop", "true"); intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); intent.putExtra("outputX", outputX); intent.putExtra("outputY", outputY); intent.putExtra("scale", true); intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); intent.putExtra("return-data", false); intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString()); intent.putExtra("noFaceDetection", true); // no face detection startActivityForResult(intent, requestCode); }
程序运行效果图:
3.完整项目代码:
package com.jph.cp; import java.io.File; import java.io.FileNotFoundException; import android.support.v7.app.ActionBarActivity; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.provider.MediaStore; import android.view.View; import android.widget.ImageView; /** * 从相册选择照片进行裁剪,从相机拍取照片进行裁剪 * @author JPH * Date:2014.10.09 */ 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); } /** * 裁剪指定uri对应的照片 * @param imageUri:uri对应的照片 * @param outputX:裁剪宽 * @param outputY:裁剪高 * @param requestCode:请求码 */ private void cropImageUri(Uri imageUri, int outputX, int outputY, int requestCode){ Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(imageUri, "image/*"); intent.putExtra("crop", "true"); intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); intent.putExtra("outputX", outputX); intent.putExtra("outputY", outputY); intent.putExtra("scale", true); intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); intent.putExtra("return-data", false); intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString()); intent.putExtra("noFaceDetection", true); // no face detection startActivityForResult(intent, requestCode); } public void cropPic(View view) { switch (view.getId()) { case R.id.btnCropFromGallery://从相册选择照片进行裁剪 cropFromGallery(); break; case R.id.btnCropFromTake://从相机拍取照片进行裁剪 cropFromTake(); break; default: break; } } /** * 从相机拍取照片进行裁剪 */ 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); } }
加载全部内容