android调用系统的相机拍照 、裁剪,然后保存

上一篇说了用surfaceView来拍照,现在说一下调用系统的拍照、相册和裁剪。

由于调用系统的拍照可能返回的data非常大,很容易导致OOM。所以在项目中都是给它传URI,然后让它把图片保存到URI,我们再跟进URI来读取图片。

首先我们是不是得准备一个File ,让系统把结果存在FIle里。

File tempFile = new File(Environment.getExternalStorageDirectory(),"lv.png");

调用系统拍照的Intent:

 Intent cameraintent = new Intent(
                    MediaStore.ACTION_IMAGE_CAPTURE);
            // 指定调用相机拍照后照片的储存路径
            cameraintent.putExtra(MediaStore.EXTRA_OUTPUT,
                    Uri.fromFile(tempFile));
            cameraintent.putExtra("return-data", false);//不返回数据
            cameraintent.putExtra("outputFormat", Bitmap.CompressFormat.PNG.toString());
            cameraintent.putExtra("noFaceDetection", true);//开启人脸识别
            startActivityForResult(cameraintent,
                    AccountInfoPresenter.PHOTO_REQUEST_TAKEPHOTO);


拍完后就是onActivityResult的处理了,由于系统帮我们保存的到URI的图片可能会被旋转了,所以我们要把它转回来,
 /**
     * 读取图片属性:旋转的角度
     * @param path 图片绝对路径
     * @return degree旋转的角度
     */
    public static int readPictureDegree(String path) {
        int degree  = 0;
        try {
            ExifInterface exifInterface = new ExifInterface(path);
            int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
            switch (orientation) {
                case ExifInterface.ORIENTATION_ROTATE_90:
                    degree = 90;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    degree = 180;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_270:
                    degree = 270;
                    break;
            }

            if (0==degree) {
                return degree;
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            Bitmap bitmap=BitmapFactory.decodeFile(path);

            Matrix m = new Matrix();
            int width = bitmap.getWidth();
            int height = bitmap.getHeight();
            m.setRotate(degree); // 旋转angle度
            bitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, m, true);// 从新生成
            FileOutputStream out = new FileOutputStream(path);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
            out.flush();
            out.close();
            bitmap.recycle();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return degree;
    }
图片也旋转完了,我们是不是得调用到系统的裁剪呢,下面是跳转到裁剪:

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(MediaStore.EXTRA_OUTPUT, Uri.fromFile(tempFile));

        // aspectX aspectY 是宽高的比例
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);

        // outputX,outputY 是剪裁图片的宽高
        intent.putExtra("outputX", 300);
        intent.putExtra("outputY", 300);
        intent.putExtra("return-data", false);
        intent.putExtra("noFaceDetection", true);
        AccountInformationFragment.this.startActivityForResult(intent, AccountInfoPresenter.PHOTO_REQUEST_CUT);

    }
系统裁剪后也是把结果保存到指定的URI中,我们就在onActivityResult中处理它:

if (data != null) {

                    try {
                        Bitmap bitmap = BitmapFactory.decodeStream(new FileInputStream(tempFile));
                        imgAccountIcon.setImageBitmap(bitmap);
                        imgAccountBg.setBackgroundDrawable((new BitmapDrawable(FastBlur.doBlur(bitmap, 10, false))));
                        isEditAviator = true;
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }

接下来是调用系统相册的:
Intent getAlbum = new Intent(Intent.ACTION_GET_CONTENT);
            getAlbum.setType("image/*");
            startActivityForResult(getAlbum, AccountInfoPresenter.PHOTO_REQUEST_GALLERY);
在相册选择图片后,一般会在onActivityResult回调的返回的Intent返回URI,注意,我说的是一般,有些特别的机子是不会返回的,所以我们得判断是否空再跳转到裁剪界面

 // 做非空判断,当我们觉得不满意想重新剪裁的时候便不会报异常,下同
                if (data != null) {
                    System.out.println("11================");
                    startPhotoZoom(data.getData());
                } else {
                    System.out.println("================");
                

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值