<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. net core体系-API-1Ocelot-(1)概要

    从这篇开始探讨Ocelot,Ocelot是一个.NET API网关,仅适用于.NET Core,用于.NET面向微服务/服务的架构中.当客户端(web站点.ios. app 等)访问web api时, ...

  2. python3改版后的特征

    1.原始数据类型和运算符 # 整数 3 # => 3 # 算术没有什么出乎意料的 1 + 1 # => 2 8 - 1 # => 7 10 * 2 # => 20 # 但是除法 ...

  3. 第K人||约瑟夫环(链表)

    http://oj.acm.zstu.edu.cn/JudgeOnline/problem.php?id=4442 很容易超时 通过数组来记录,删除 //数组从1开始好像不行 后面一些数字就乱码了,因 ...

  4. Knn:Knn实现对150朵共三种花的实例的萼片长度、宽,花瓣长、宽数据统计,根据一朵新花的四个特征来预测其种类

    from sklearn import neighbors from sklearn import datasets knn = neighbors.KNeighborsClassifier() ir ...

  5. 【JavaScript】快速入门

    摘抄地址快速入门 No1: JavaScript严格区分大小写 No2: JavaScript不区分整数和浮点数,统一用Number表示 NaN表示Not a Number,当无法计算结果时用NaN表 ...

  6. 【python】web开发

    No1: hello.py def application(environ,start_response): start_response('200 OK',[('Content-Type','tex ...

  7. gradle根据不同渠道设置不同的开屏启动页

    需求:根据不同渠道,app的开屏启动页不一样 思路:因为app的启动页是在清单文件配置的,而清单文件最后是要和main里面的清单文件合并的,所以每个渠道都要配一个清单文件,在里面设置 然后在Andro ...

  8. shell编程第四天

  9. C# LnkHelper

    using System; using System.Collections.Generic; using System.Text; using Microsoft.Win32; using Syst ...

  10. python 3.6 + numpy + matplotlib + opencv + scipy 安装

    首先,下载并安装 python3.6: 然后,在网址http://www.lfd.uci.edu/~gohlke/pythonlibs/ 上 分别下载 numpy.scipy.matplotlib.o ...