Android解决三星手机图片旋转问题

来源:互联网 发布:vb程序代码 编辑:程序博客网 时间:2024/04/20 06:32

此文章只是记录自己在项目中遇到的问题,写下来,给自己提醒,相信接触过图片选择的必定会遇到三星手机从相册或者是拍照之后拿到图片路径,获取Bitmap对象,图片大了还得对bitmap进行压缩,最后显示在ImageView上,就会发现照片会旋转90°(我遇见的,不知道其他人旋转了多少度),解决办法也是比较方便快捷:


1.通过图片路径得到图片的旋转角度

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;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return degree;
    }


2.根据图片的选装角度将图片旋转回去

public static Bitmap rotateBitmapByDegree(Bitmap bm, int degree) {
        Bitmap returnBm = null;

        // 根据旋转角度,生成旋转矩阵
        Matrix matrix = new Matrix();
        matrix.postRotate(degree);
        try {
            // 将原始图片按照旋转矩阵进行旋转,并得到新的图片
            returnBm = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true);
        } catch (OutOfMemoryError e) {
            
        }
        if (returnBm == null) {
            returnBm = bm;
        }
        if (bm != returnBm) {
            bm.recycle();
        }
        return returnBm;
    }


通过以上两部基本上就解决了,刚遇到的时候可能会让人头疼,不过解决了会那么容易,也许这就是小小的一点成长,加油吧


1 0
原创粉丝点击