android 下载 ios上传图片的角度显示问题

最近做项目时,遇到了android端 显示ios端上传图片的问题 ,图片角度是随着ios端拍照的角度显示的 ,也就是说如果ios端 横着拍照,android端的图片就横着显示,如果ios端竖着拍照,android端的图拍呢就竖着显示, android端显示android端上传的图片没有问题,我想是android系统内部做了处理.鸡精周折,才找到了原来可以获取照片拍照时的角度,

如何获取和改变图片的方向呢,android中提供了一个ExifInterface接口,用于获取图像文件的信息:


public class ImageRotateHelper {

	

	
	/** 从给定的路径载入图片,并指定旋转方向*/
	public static Bitmap ImageRotateBitmap(Bitmap bm,String imgpath) {

			int digree = 0;
			ExifInterface exif = null;
			try {
				exif = new ExifInterface(imgpath);
			} catch (IOException e) {
				e.printStackTrace();
				exif = null;
			}
			if (exif != null) {
				// 读取图片中相机方向信息
				int ori = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
						ExifInterface.ORIENTATION_UNDEFINED);
				// 计算旋转角度
				switch (ori) {
				case ExifInterface.ORIENTATION_ROTATE_90://打印出来是6
					digree = 90;
					break;
				case ExifInterface.ORIENTATION_ROTATE_180://打印出来是3
					digree = 180;
					break;
				case ExifInterface.ORIENTATION_ROTATE_270://打印出来是8
					digree = 270;
					break;
				default:
					digree = 0;
					break;
				}
			}
			if (digree != 0) {
				// 旋转图片
				Matrix m = new Matrix();
				m.postRotate(digree);
				bm = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(),
						bm.getHeight(), m, true);
			}
			return bm;
		
	}
}
通过这个类   就可以将显示方向不正确的图片,正确显示出来,希望能帮到你们 

猜你喜欢

转载自blog.csdn.net/qq_35893839/article/details/78496865