java解决手机等移动设备中照片上传至服务器方向不正确的问题
2015-04-01 13:57
891 查看
如果将手机等移动设备中的照片上传至服务器,再显示已上传的图片,可能会有方向不正确的问题,这是因为照片中含有方向信息,它告知你照片是什么方向的,但我们如果没有处理方向,显示就会不正确。如果用正常方法读取已上传照片的宽和高:
这样,手机照片上传上来方向就正常了。
本文出自 “ThatWay” 博客,请务必保留此出处http://thatway.blog.51cto.com/4815281/1627283
BufferedImage image = ImageIO.read(new File(fullPath)); int width = image.getWidth(); //图片的宽 int height = height.getHeight(); //图片的高这样获取出的宽和高,可能不是正确的,因为没有考虑图片的方向信息,简单说,我发现如果是手机,将手机顺时针旋转90度成为横屏,这时候照相,照出的照片,上传到服务器是正常的,也就是说,这时的照片中的方向信息是标准方向,如果你是用手机正常竖屏来照,则需要要把照片旋转90度才和你照照片时方向相同,同理,还有两个方向是需要把照片旋转180度和270度才能够正确显示。照片的方向信息在图片文件的EXIF信息中,那么只要读取到图片文件的EXIF信息,就能知道需要旋转的角度,那么在上传完成后,把图片再按角度旋转一下重新写到服务器,不就正常了么,我解决这个问题的思路就是这样。那么,获取图片的EXIF信息,需要使用mediautil-1.0.jar、metadata-extractor-2.3.1.jar,已上传到附件中。获取需要旋转的角度,代码:
/** * 获取图片正确显示需要旋转的角度(顺时针) * @return */ public static int getRotateAngleForPhoto(String filePath){ File file = new File(filePath); int angle = 0; Metadata metadata; try { metadata = JpegMetadataReader.readMetadata(file); Directory directory = metadata.getDirectory(ExifDirectory.class); if(directory.containsTag(ExifDirectory.TAG_ORIENTATION)){ // Exif信息中方向 int orientation = directory.getInt(ExifDirectory.TAG_ORIENTATION); // 原图片的方向信息 if(6 == orientation ){ //6旋转90 angle = 90; }else if( 3 == orientation){ //3旋转180 angle = 180; }else if( 8 == orientation){ //8旋转90 angle = 270; } } } catch (JpegProcessingException e) { e.printStackTrace(); } catch (MetadataException e) { e.printStackTrace(); } return angle; }获取到需要旋转的角度以后,在上传完毕后,加一步旋转的操作,重新生成一遍图片:
/** * 旋转手机照片 * @return */ public static String rotatePhonePhoto(String fullPath, int angel){ BufferedImage src; try { src = ImageIO.read(new File(fullPath)); int src_width = src.getWidth(null); int src_height = src.getHeight(null); Rectangle rect_des = CalcRotatedSize(new Rectangle(new Dimension(src_width, src_height)), angel); BufferedImage res = new BufferedImage(rect_des.width, rect_des.height,BufferedImage.TYPE_INT_RGB); Graphics2D g2 = res.createGraphics(); g2.translate((rect_des.width - src_width) / 2, (rect_des.height - src_height) / 2); g2.rotate(Math.toRadians(angel), src_width / 2, src_height / 2); g2.drawImage(src, null, null); ImageIO.write(res, "jpg", new File(fullPath)); } catch (IOException e) { e.printStackTrace(); } return fullPath; }
这样,手机照片上传上来方向就正常了。
本文出自 “ThatWay” 博客,请务必保留此出处http://thatway.blog.51cto.com/4815281/1627283
相关文章推荐
- java解决手机等移动设备中照片上传至服务器方向不正确的问题
- iOS 手机照片上传服务器方向不对的原因以及解决方法
- 基于java在服务端解决手机上传竖拍照片旋转90度问题
- 针对"您的计算机配置似乎是正确的,但该设备或资源(DNS 服务器)没有响应"问题解决
- Android开发中调用系统相册上传图片到服务器OPPO等部分手机上出现短暂的显示桌面问题的解决方法
- 解决ios手机上传竖拍照片旋转90度问题
- 利用exif.js插件解决ios手机上传竖拍照片旋转90度问题
- java input 调用手机相机和本地照片上传图片到服务器然后压缩的方法
- js,angularjs 图片上传服务器,并解决照片旋转问题,前端图片压缩上传
- 利用exif.js解决ios手机上传竖拍照片旋转90度问题
- 利用exif.js解决ios手机上传竖拍照片旋转90度问题
- 利用exif.js解决ios手机上传竖拍照片旋转90度问题
- 利用exif.js解决ios手机上传竖拍照片旋转90度问题
- 利用exif.js解决ios手机上传竖拍照片旋转90度问题
- 利用exif.js解决ios或Android手机上传竖拍照片旋转90度问题
- 详解利用exif.js解决ios手机上传竖拍照片旋转90度问题
- Android 上传头像,解决小米3等手机无法直接得到裁剪照片问题
- 利用exif.js解决ios手机上传竖拍照片旋转90度问题
- 解决相片上传主键唯一性问题,java 上传相片到服务器的时候,相片保存主键用guid。
- JavaME 开发体会:线程编程解决手机地图按住方向键不放地图移动问题