这里有新鲜出炉的精品教程,程序狗速度看过来!
Android是一种基于Linux的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由Google公司和开放手机联盟领导及开发。尚未有统一中文名称,中国大陆地区较多人使用“安卓”或“安致”。
这篇文章主要为大家详细介绍了Android调用系统拍照裁剪图片模糊的解决方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
在Android中,调用系统相机拍照时,将会接收到返回的图像数据,但是这些图片并不是全尺寸的图像,而是系统给的缩略图,当对拍照的图片进行裁切后显示时,得到的却是模糊的图片。下面针对这个问题提出解决的方法。
首先,我们知道调用系统的裁切是通过Intent intent = new Intent(“com.android.camera.action.CROP”);
但是intent到底能够携带哪些数据呢,都有什么含义呢,我们可以看到如下:
上面包含了所有可选的操作,其中有一些非常重要的参数。
intent.putExtra(“return-data”, true):表示裁剪后返回的数据为Bitmap,是存在内存中的缩略图,效果模糊。获取的方式为,在Activity中的onActivityResult方法中:
- Bundle bundle = data.getExtras();
- Bitmap bitmap = bundle.getParcelable("data");
为了获取到裁切后的原图,我们选择将剪切的图片保存在本地,然后调用本地的图片,并不直接返回Bitmap.
- intent.putExtra("return-data", false);
- intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
- intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
下面代码实现拍照/剪切并进行显示的
- public static int TAKE_PHOTO_REQUEST_CODE = 1; //拍照
- public static int PHOTO_REQUEST_CUT = 2; //裁切
- public static int PHOTO_REQUEST_GALLERY = 3; //相册
- public Uri imageUri;
- /**
- * 打开相机拍照
- */
- private void takePhotos() {
- imageUri = Uri.fromFile(getImageStoragePath(this));
- Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
- //指定照片存储路径
- intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
- startActivityForResult(intent, TAKE_PHOTO_REQUEST_CODE);
- }
- /**
- * 打开相册选择图片
- */
- private void choicePicFromAlbum() {
- Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
- intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
- startActivityForResult(intent, PHOTO_REQUEST_GALLERY);
- }
- /**
- * 设置图片保存路径
- * @return
- */
- private File getImageStoragePath(Context context) {
- if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
- File file = new File(context.getExternalFilesDir(Environment.DIRECTORY_PICTURES), "temp.jpg");
- return file;
- }
- return null;
- }
- @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- super.onActivityResult(requestCode, resultCode, data);
- if (requestCode == TAKE_PHOTO_REQUEST_CODE) {
- if (imageUri != null) {
- startPhotoZoom(imageUri);
- }
- } else if (requestCode == PHOTO_REQUEST_CUT) {
- if (imageUri != null) {
- Bitmap bitmap = decodeUriBitmap(imageUri);
- imageView.setImageBitmap(bitmap);
- }
- } else if (requestCode == PHOTO_REQUEST_GALLERY) {
- if (data != null) {
- imageUri = data.getData();
- Bitmap bitmap = decodeUriBitmap(imageUri);
- imageView.setImageBitmap(bitmap);
- }
- }
- }
- private Bitmap decodeUriBitmap(Uri uri) {
- Bitmap bitmap = null;
- try {
- bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));
- } catch(FileNotFoundException e) {
- e.printStackTrace();
- return null;
- }
- return bitmap;
- }
- /**
- * 调用系统裁剪
- *
- * @param uri
- */
- public void startPhotoZoom(Uri uri) {
- Intent intent = new Intent("com.android.camera.action.CROP");
- intent.setDataAndType(uri, "image/*");
- // crop为true是设置在开启的intent中设置显示的view可以剪裁
- intent.putExtra("crop", "true");
- intent.putExtra("scale", true);
- // aspectX aspectY 是宽高的比例
- intent.putExtra("aspectX", 1);
- intent.putExtra("aspectY", 1);
- // outputX,outputY 是剪裁图片的宽高
- intent.putExtra("outputX", 800);
- intent.putExtra("outputY", 800);
- //设置了true的话直接返回bitmap,可能会很占内存
- intent.putExtra("return-data", false);
- //设置输出的格式
- intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
- //设置输出的地址
- intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
- //不启用人脸识别
- intent.putExtra("noFaceDetection", true);
- startActivityForResult(intent, PHOTO_REQUEST_CUT);
- }
来源: http://www.phperz.com/article/17/1011/347202.html