三星手机选择图片旋转问题

来源:互联网 发布:拙心网络 怎么样 编辑:程序博客网 时间:2024/03/28 20:54

Android开发机型比较多可能会遇到比较奇葩的问题,在选择本地图片上传的时候三星手机会遇到图片旋转90°的情况,这需要我们自己判断图片是否旋转,然后在将其旋转过来就好:

 private String dealPic(String fillPath) {        //针对三星手机拍照旋转的问题        if (getPictureDegree(filePath) != 0) {            bitmap = toturn(bitmap, readPictureDegree(filePath));        }        String path = BitmapUtils.saveImage(this, bitmap);        return path;    }

获取图片旋转角度:

public static int getPictureDegree(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;    }

若图片旋转了就将其旋转回0°:

 /**     * 旋转图片     *     * @param img     * @return     */    public Bitmap toturn(Bitmap img, int degree) {        Matrix matrix = new Matrix();        matrix.postRotate(+degree);        int width = img.getWidth();        int height = img.getHeight();        img = Bitmap.createBitmap(img, 0, 0, width, height, matrix, true);        return img;    }

以上即可解决,目前只在三星手机遇到该问题。

1 0