<dependency>
<groupId>com.drewnoakes</groupId>
<artifactId>metadata-extractor</artifactId>
<version>2.11.0</version>
</dependency>

import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File; import javax.imageio.ImageIO; import com.drew.imaging.jpeg.JpegMetadataReader;
import com.drew.imaging.jpeg.JpegProcessingException;
import com.drew.metadata.Directory;
import com.drew.metadata.Metadata;
import com.drew.metadata.MetadataException;
import com.drew.metadata.exif.ExifDirectoryBase; public class Demo { // 处理ios图片旋转的问题
public static void getPictureByName(String filePath,String name){ try {
//name为前端请求图片名,如 a.jpg
BufferedImage src = getPicture(filePath+name);
BufferedImage bi = null; //图片存在
if(src != null){
//获取图片旋转角度
int angel = getRotateAngleForPhoto(filePath+name);
//图片被翻转,调整图片
if(angel != 0){
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); bi = new BufferedImage(rect_des.width, rect_des.height,BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = bi.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); int index = name.lastIndexOf(".");
String formate = name.substring(index+1);
ImageIO.write(bi, formate, new File(filePath+name));
}
}else{
//图片不存在
System.out.println("图片不存在");
}
} catch (Exception e) {
e.printStackTrace();
}
} /**
* 读取指定图片
*/
public static BufferedImage getPicture(String path) {
BufferedImage bi = null;
try{
File file = new File(path);
if(!file.exists()){
return null;
}
bi = ImageIO.read(file);
} catch (Exception e){
e.printStackTrace();
}
return bi;
} /**
* 图片翻转时,计算图片翻转到正常显示需旋转角度
*/
public static int getRotateAngleForPhoto(String fileName){ File file = new File(fileName); int angel = 0;
Metadata metadata; try{
metadata = JpegMetadataReader.readMetadata(file);
Directory directory = metadata.getFirstDirectoryOfType(ExifDirectoryBase.class);
if(directory != null && directory.containsTag(ExifDirectoryBase.TAG_ORIENTATION)){
// Exif信息中方向  
int orientation = directory.getInt(ExifDirectoryBase.TAG_ORIENTATION);
// 原图片的方向信息
if(6 == orientation ){
//6旋转90
angel = 90;
}else if( 3 == orientation){
//3旋转180
angel = 180;
}else if( 8 == orientation){
//8旋转90
angel = 270;
}
}
} catch(JpegProcessingException e){
e.printStackTrace();
} catch(MetadataException e){
e.printStackTrace();
} catch(Exception e){
e.printStackTrace();
}
System.out.println("图片旋转角度:" + angel);
return angel;
} /**
* 计算旋转参数
*/
public static Rectangle CalcRotatedSize(Rectangle src,int angel){
// if angel is greater than 90 degree,we need to do some conversion.
if(angel > 90){
if(angel / 9%2 ==1){
int temp = src.height;
src.height = src.width;
src.width = temp;
}
angel = angel % 90;
} double r = Math.sqrt(src.height * src.height + src.width * src.width ) / 2 ;
double len = 2 * Math.sin(Math.toRadians(angel) / 2) * r;
double angel_alpha = (Math.PI - Math.toRadians(angel)) / 2;
double angel_dalta_width = Math.atan((double) src.height / src.width);
double angel_dalta_height = Math.atan((double) src.width / src.height); int len_dalta_width = (int) (len * Math.cos(Math.PI - angel_alpha
- angel_dalta_width));
int len_dalta_height = (int) (len * Math.cos(Math.PI - angel_alpha
- angel_dalta_height));
int des_width = src.width + len_dalta_width * 2;
int des_height = src.height + len_dalta_height * 2;
return new java.awt.Rectangle(new Dimension(des_width, des_height));
} public static void main(String[] args) throws Exception {
String filePath ="d:/" ;
String filename = "333.jpg";
//处理ios图片旋转的问题
getPictureByName(filePath,filename);
}
}

java解决手机上传竖拍照片旋转90\180\270度问题的更多相关文章

  1. 利用exif.js解决手机上传竖拍照片旋转90\180\270度问题

    原文:https://blog.csdn.net/linlzk/article/details/48652635/ html5+canvas进行移动端手机照片上传时,发现ios手机上传竖拍照片会逆时针 ...

  2. 利用exif.js解决ios手机上传竖拍照片旋转90度问题

    html5+canvas进行移动端手机照片上传时,发现ios手机上传竖拍照片会逆时针旋转90度,横拍照片无此问题:Android手机没这个问题. 因此解决这个问题的思路是:获取到照片拍摄的方向角,对非 ...

  3. 解决ios手机上传竖拍照片旋转90度问题

    html5+canvas进行移动端手机照片上传时,发现ios手机上传竖拍照片会逆时针旋转90度,横拍照片无此问题:Android手机没这个问题. 因此解决这个问题的思路是:获取到照片拍摄的方向角,对非 ...

  4. 利用exif.js解决ios或Android手机上传竖拍照片旋转90度问题

    html5+canvas进行移动端手机照片上传时,发现ios手机上传竖拍照片会逆时针旋转90度,横拍照片无此问题:Android手机没这个问题. 因此解决这个问题的思路是:获取到照片拍摄的方向角,对非 ...

  5. 通过transpose和flip实现图像旋转90/180/270度

    在fbc_cv库中,提供了对图像进行任意角度旋转的函数rotate,其实内部也是调用了仿射变换函数warpAffine.如果图像仅是进行90度倍数的旋转,是没有必要用warpAffine函数的.这里通 ...

  6. iOS 解决图片上传到服务器旋转90度的问题(图片倒置)

    //使用swift的朋友们可以,把这个所在的类的.h,在-Header-Swift.h中一用一下. - (UIImage *)fixOrientation:(UIImage *)aImage { if ...

  7. recovery 下界面UI旋转90 180 270修改

    原文修改出自简书:https://www.jianshu.com/p/768fdd954061 应该是MTK修改的google源码,支持recovery下屏幕旋转90/180/270, 作者把MTK的 ...

  8. 对android录制的NV21视频数据进行旋转(90,180,270)与剪切

    android默认的视频采集格式是NV21,(属于YUV420) 在onPreviewFrame中传进来的byte[] data即为NV21格式. 旋转算法 对NV21进行顺时针旋转90度,180度和 ...

  9. 移动端上传照片 预览+Draw on Canvas's Demo(解决 iOS 等设备照片旋转 90 度的 bug)

    背景: 本人的一个移动端H5项目,需求如下: 需求一:手机相册选取或拍摄照片后在页面上预览 需求二:然后绘制在canvas画布上 这里,我们先看一个demo(http://jsfiddle.net/q ...

随机推荐

  1. Docker Client (another java docker client api)

    前一篇提到了docker-java,这里介绍另一个docker client 库,Docker Client 版本兼容 兼容17.03.1~ce - 17.12.1~ce (点 [here][1]查看 ...

  2. 执行shell脚本提示“-bash: ./checkP.sh: /bin/sh^M: bad interpreter: No such file or directory”解决方法

    在windows机器下新建了一个shell脚本如下

  3. ELK使用1-Elasticsearch使用

    一.es 1.通过curl命令获取es进群信息 a.curl -i(设置协议的头信息) -XGET 'http:192.168.30.41:9200/_count' b.查看集群状态 curl -XG ...

  4. Nginx反向代理的基本配置

    (1)proxy_pass 语法:proxy_pass URL; 配置块:location.if 此配置项将当前请求反向代理到URL参数指定的服务器上,URL可以是主机名或IP地址加端口的形式,例如: ...

  5. BZOJ3240 [Noi2013]矩阵游戏 矩阵 快速幂 卡常

    原文链接http://www.cnblogs.com/zhouzhendong/p/8084891.html 题目传送门 - BZOJ3240 题意概括 F[1][1]=1F[i,j]=a*F[i][ ...

  6. psp表格记录-

    PSP2.1 Personal Software Process Stages Time Planning 计划 · Estimate · 估计这个任务需要多少时间 12 Development 开发 ...

  7. java生成Excel文件,下载

    pom引入poi的maven依赖 <dependency> <groupId>org.apache.poi</groupId> <artifactId> ...

  8. Idea问题:“marketplace plugins are not loaded”解决方案

    博主本人遇见该问题时是想要通过Idea的plugins工具下载阿里巴巴的代码规约工具 但是在我点开settings,然后打开plugins工具时竟然给我提示“marketplace plugins a ...

  9. shell脚本中的set -e和set -o pipefail

    工作中经常在shell脚本中看到set的这两个用法,但就像生活中的很多事情,习惯导致忽视,直到出现问题才引起关注. 1. set -eset命令的-e参数,linux自带的说明如下:"Exi ...

  10. 通用图片加载组件UniversalImageLoader

    通用图片加载组件UniversalImageLoader   UniversalImageLoader是一款通用图片加载组件.该组件支持多种图片来源,如网络.SD卡.Assets文件夹等.在网络请求的 ...