Android手机拍照后照片被旋转或者需要旋转的问题

1、读取图片的旋转属性

/**
  * 读取图片的旋转的角度
  *
  * @param path
  *            图片绝对路径
  * @return 图片的旋转角度
  */
private  int  getBitmapDegree(String path) {
     int  degree =  0 ;
     try  {
         // 从指定路径下读取图片,并获取其EXIF信息
         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;
}

	/**
	 * 将图片按照某个角度进行旋转
	 *
	 * @param bm
	 *            需要旋转的图片
	 * @param degree
	 *            旋转角度
	 * @return 旋转后的图片
	 */
	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;
	}

在部分Android手机(如MT788、Note2)上,使用Camera拍照以后,得到的照片会被自动旋转(90°、180°、270°),这个情况很不符合预期。仔细分析了一下,因为照片属性中是存储了旋转信息的,所以要解决这个问题,可以在onActivityResult方法中,获取到照片数据后,读取它的旋转信息,如果不是0,说明这个照片已经被旋转过了,那么再使用android.graphics.Matrix将照片旋转回去即可。

我的代码中使用了这个方法:
// 为防止原始图片过大导致内存溢出,这里先缩小原图显示,然后释放原始Bitmap占用的内存,变成65px
					Bitmap smallBitmap = UploadImageTools.zoomBitmap(photo,image_px, image_py);
					//smallBitmap = rotateBitmapByDegree(smallBitmap,90);
					// 压缩图片,得到base64str ,用于上传到服务器
					ll_com_cover.setImageBitmap(smallBitmap);// 图片选择 那个位置的视图改成
																// 当前用户选择的图片
					ll_com_cover.setOnClickListener(new ImageDeleteListener());// 设置图片删除事件,用户上传了一张照片,可以很快就删除,重新选择
					//photo = rotateBitmapByDegree(photo,90);
					base64ImgStr = UploadImageTools.BitmapToBase64Str2(photo);// 把要上传的图片转换成为64位码
					// 预览成功之后 马上把图片发送给服务器

但是最后还是没有使用,放弃了,
决定还是搞个预览功能,并且支持旋转,由用户自己来决定到底是选择 竖着的还是横着的

文章未完,敬请期待
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这个问题的原因是由于不同的设备在拍照后保存图片的方向不同,导致在显示图片时出现了旋转的情况。为了解决这个问题,可以通过以下方法进行处理: 1. 使用 ExifInterface 类 可以通过 ExifInterface 类读取图片的旋转角度信息,然后将图片进行相应的旋转。具体的代码如下: ```java ExifInterface exif = new ExifInterface(photoPath); int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1); Matrix matrix = new Matrix(); if (orientation == ExifInterface.ORIENTATION_ROTATE_90) { matrix.postRotate(90); } else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) { matrix.postRotate(180); } else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) { matrix.postRotate(270); } Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); ``` 2. 使用系统相机 在调用系统相机拍照时,可以通过设置 Camera.Parameters 属性来控制图片的旋转角度。具体的代码如下: ```java Camera.Parameters parameters = mCamera.getParameters(); Camera.CameraInfo info = new Camera.CameraInfo(); Camera.getCameraInfo(cameraId, info); int rotation = activity.getWindowManager().getDefaultDisplay().getRotation(); int degrees = 0; switch (rotation) { case Surface.ROTATION_0: degrees = 0; break; case Surface.ROTATION_90: degrees = 90; break; case Surface.ROTATION_180: degrees = 180; break; case Surface.ROTATION_270: degrees = 270; break; } int result; if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) { result = (info.orientation + degrees) % 360; result = (360 - result) % 360; } else { result = (info.orientation - degrees + 360) % 360; } mCamera.setDisplayOrientation(result); parameters.setRotation(result); ``` 以上两种方法都可以解决Android部分手机拍照后获取的图片被旋转问题

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值