android部分手机本地图片上传服务器时图片被旋转的问题

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/LXFX1108/article/details/81566860

问题:Android 部分手机,app中显示本地图片没有被旋转,是正常的。但是上传服务器后,服务器上的图片是被旋转的,有被旋转90度的,也有被旋转180度的。

原因:部分手机拍照后,保存到本地是被旋转的,这个可以通过下面方法验证;

解决思路:

1、根据本地图片绝对路径,获取图片被旋转的角度;

private final int DEGREES_FIRST = 90;
private final int DEGREES_SECOND = 180;
private final int DEGREES_THREE = 270;

/**
 * 读取图片属性:旋转的角度
 * @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 = DEGREES_FIRST;
        break;
      case ExifInterface.ORIENTATION_ROTATE_180:
        degree = DEGREES_SECOND;
        break;
      case ExifInterface.ORIENTATION_ROTATE_270:
        degree = DEGREES_THREE;
        break;
    }
  } catch (IOException e) {
    e.printStackTrace();
  }
  return degree;
}

2、将图片旋转获取的度数;

//然后我们再根据图片的旋转角度,给图片进行相应角度的反向旋转
public static Bitmap rotateBitmap(Bitmap bitmap, int rotate) {
  if (bitmap == null)
    return null;
  int mWidth = bitmap.getWidth();
  int mHeight = bitmap.getHeight();
  Matrix mtx = new Matrix();
  mtx.postRotate(rotate);
  return Bitmap.createBitmap(bitmap, 0, 0, mWidth, mHeight, mtx, true);
}

1、有想做副业,业余开店的,推荐“贝店”,

     应用市场下载“贝店”,注册,输入邀请码 1530973;

     购买指定商品即可开店;

2、另外可以关注我的公众号“生活与认知”;

猜你喜欢

转载自blog.csdn.net/LXFX1108/article/details/81566860