Android 拍照获取图片并剪切

       项目里相册或者拍照获取图片并剪切设置头像是很多app里面都有的,但是经常会出现一些问题,在这里,我主要解决的两个比较头疼的问题:

1,剪切完后是缩略图,很不清楚

2,三星手机拍照是横着的

相册选择设置在这就不仔细说了,我主要说一下拍照后的图片问题


/**
     * 调用系统相机拍照
     */
    protected void doTakePhoto() {
        try {
            File yunDir = new File(IMAGE_FILE);
            if(!yunDir.exists()){
                yunDir.mkdirs();
            }
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE, null);
            mAvatarUri = Uri.fromFile(new File(Environment
                    .getExternalStorageDirectory(), "/Yun/Images/avatar_test"
                    ));
            intent.putExtra(MediaStore.EXTRA_OUTPUT, mAvatarUri);
            startActivityForResult(intent, CAMERA_WITH_DATA);
        } catch (ActivityNotFoundException e) {
            Toast.makeText(this, R.string.photoPickerNotFoundText, Toast.LENGTH_LONG).show();
        }

    }

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            switch (requestCode) {
                case PHOTO_PICKED_WITH_DATA:
                    // TODO 相册选择图片后裁剪图片
                    startPhotoZoom(data.getData());
                    break;
                case CAMERA_WITH_DATA:
                    // TODO 相机拍照后裁剪图片
                    // 判断是否有旋转度
                    Bitmap photoBitmap = PhotoUtil.readBitmapFromPath(this,IMAGE_FILE_PHOTO);
                    int degree = PhotoUtil.getExifOrientation(IMAGE_FILE_PHOTO);
                    if(degree != 0){
                        photoBitmap = PhotoUtil.rotaingImageView(photoBitmap,degree);
                        if(mCurrentPhotoFile == null){
                            mCurrentPhotoFile = new File(IMAGE_FILE_LOCATION);
                        }
                        mCurrentPhotoFile = PhotoUtil.saveBitmaptoSdCard(photoBitmap,this,"/Yun/Images");
                        startPhotoZoom(Uri.fromFile(mCurrentPhotoFile));
                    }else{
                        startPhotoZoom(mAvatarUri);
                    }
                    break;
                case PHOTO_CROP_RESOULT:
                    try {
                        Bitmap bitmap = getBitmapFromUri(imageUri);
                        mImage.setImageBitmap(bitmap);
                    }catch (Exception e){
                        Toast.makeText(this,"失败,请重新设置",Toast.LENGTH_SHORT).show();
                    }
                    break;

            }


        }
    }

拍照返回后进行剪切图片:

在这里需要注意的是通过data获得的是一张缩略图一般不超过10k,如果想获得一张剪切后的原图,就需要指定图片的保存地址

/**
     * 剪切图片
     * @param uri
     */
    public void startPhotoZoom(Uri uri) {

        File yunDir = new File(IMAGE_FILE);
        if(!yunDir.exists()){
            yunDir.mkdirs();
        }
        imageUri = Uri.fromFile(new File(IMAGE_FILE_LOCATION));

        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri, "image/*");
        intent.putExtra("crop", "true");
        intent.putExtra("circleCrop", "true");
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        intent.putExtra("outputX", ICON_SIZE);
        intent.putExtra("outputY", ICON_SIZE);
        // TODO 剪切图 结果存放到文件中
        intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);//图像输出
        intent.putExtra("outputFormat",
                Bitmap.CompressFormat.JPEG.toString());
        intent.putExtra("noFaceDetection", true);
        // TODO  回调方法data.getExtras().getParcelable("data") 返回数据为空
        // TODO  如果只取缩略图 置为true
        intent.putExtra("return-data", false);
        startActivityForResult(intent, PHOTO_CROP_RESOULT);


    }

剪切完后,需要判断图片是否有旋转角度,目前好像就三星的手机有旋转角度

/**
     * 获取jpeg的旋转信息
     * @param filepath
     * @return
     */
    public static int getExifOrientation(String filepath) {
        int degree = 0;
        ExifInterface exif = null;
        try {
            exif = new ExifInterface(filepath);
        } catch (IOException ex) {
            //LogUtil.i("test", "cannot read exif");
        }
        if (exif != null) {
            int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);
            Log.i("ORIENTATION", orientation + "");
            if (orientation != -1) {
                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;
                }
            }
        }
        return degree;
    }

    /**
     * 旋转图片
     * @param bitmap 原图
     * @param angle2 旋转角度
     * @return
     */
    public static Bitmap rotaingImageView(Bitmap bitmap, int angle2) {
        Matrix matrix = new Matrix();
        // 旋转图片 动作
        matrix.postRotate(angle2);
        System.out.println("angle2=" + angle2);


        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 1;
        bitmap = BitmapFactory.decodeStream(bitmap2IS(bitmap), null, options);

        int height = bitmap.getHeight();
        int width = bitmap.getWidth();
        // 创建新的图片
        Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
        bitmap.recycle();
        return resizedBitmap;
    }

    /**
     * Bitmap转换成InputStream方法
     *
     * @param bm
     * @return
     */
    public static InputStream bitmap2IS(Bitmap bm) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bm.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        InputStream sbs = new ByteArrayInputStream(baos.toByteArray());
        return sbs;
    }


源码地址: http://download.csdn.net/detail/qq55214/9435302

遗留问题:经过旋转在存到本地后,图片变小了,没旋转的没有问题,求指教。。。


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值