android 拍照+裁剪(解决部分相机拍照角度问题)

android 拍照+裁剪(解决部分相机拍照角度旋转问题)

最近有人问拍照、裁剪、相片角度问题,网上的坑太多,于是自己来填一下。


废话不多说,直接上代码


相关权限

<!--相机权限-->
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />


Test2activity类

此类就一个测试类,其实很简单,所有都调用系统,只是在拍照返回数据时对照片进行下处理,解决角度问题

public class Test2Activity extends Activity {

    private ImageView iv_image;
    private File mCurrentPhotoFile;
    private String image;
    private Bitmap imageBitmap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test2);
        iv_image = (ImageView) findViewById(R.id.iv_image);
        findViewById(R.id.tv_camera).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCurrentPhotoFile = new File(Environment.getExternalStorageDirectory()
                        + "/DCIM/Camera", System.currentTimeMillis() + ".png");
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                // 下面这句指定调用相机拍照后的照片存储的路径
                intent.putExtra(MediaStore.EXTRA_OUTPUT,
                        Uri.fromFile(mCurrentPhotoFile));
                startActivityForResult(intent, 100);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case 100:
                    try {
                        image = compressImage(mCurrentPhotoFile.getAbsolutePath(), System.currentTimeMillis() + ".png", 60);
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }
                    Intent intent1 = new Intent("com.android.camera.action.CROP");
                    intent1.setDataAndType(Uri.fromFile(new File(image)), "image/*");// mUri是已经选择的图片Uri
                    intent1.putExtra("crop", true);
                    intent1.putExtra("aspectX", 1);// 裁剪框比例
                    intent1.putExtra("aspectY", 1);
                    intent1.putExtra("outputX", 150);// 输出图片大小
                    intent1.putExtra("outputY", 150);
                    intent1.putExtra("return-data", true);
                    startActivityForResult(intent1, 200);
                    break;
                case 200:
                    if (intent != null) {
                        imageBitmap = intent.getParcelableExtra("data");
                        iv_image.setImageBitmap(imageBitmap);
                    }
                    break;
            }
        } else if (resultCode == RESULT_CANCELED && requestCode == 200) {
            if (intent == null && mCurrentPhotoFile.exists()) {
                imageBitmap = BitmapFactory.decodeFile(mCurrentPhotoFile.getAbsolutePath());
                iv_image.setImageBitmap(imageBitmap);
            }
        }
    }

    public String compressImage(String filePath, String fileName, int q) throws FileNotFoundException {
        Bitmap bm = BitmapFactory.decodeFile(filePath);
        int degree = readPictureDegree(filePath);
        if (degree != 0) {//旋转照片角度
            bm = rotateBitmap(bm, degree);
        }
        File imageDir = new File(Environment.getExternalStorageDirectory()
                + "/DCIM/Camera");
        File outputFile = new File(imageDir, fileName);
        FileOutputStream out = new FileOutputStream(outputFile);
        bm.compress(Bitmap.CompressFormat.JPEG, q, out);
        deleteTempFile(filePath);
        return outputFile.getPath();
    }

    /**
     * 根据路径删除图片
     *
     * @param path
     */
    public void deleteTempFile(String path) {
        File file = new File(path);
        if (file.exists()) {
            file.delete();
        }
    }


    /**
     * 旋转照片
     *
     * @param bitmap
     * @param degress
     * @return
     */
    public Bitmap rotateBitmap(Bitmap bitmap, int degress) {
        if (bitmap != null) {
            Matrix m = new Matrix();
            m.postRotate(degress);
            bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), m, true);
            return bitmap;
        }
        return bitmap;
    }

    /**
     * 判断照片角度
     *
     * @param path
     * @return
     */
    public 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;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return degree;
    }
}

布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/holo_blue_bright"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv_camera"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@android:color/white"
        android:gravity="center"
        android:text="拍照"
        android:textColor="@android:color/black"
        android:textSize="16sp" />

    <ImageView
        android:id="@+id/iv_image"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="20dp"
        android:scaleType="fitXY" />
</LinearLayout>
如果有什么建议可以联系我:

微信:feifei254

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值