<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. [转]启动tensorboard

    https://vivekcek.wordpress.com/tag/tensorboard-windows/   Visualise Computational Graphs with Tensor ...

  2. react添加样式的四种方法

    React给添加元素增加样式 第一种方法: <!DOCTYPE html> <html lang="en"> <head> <meta c ...

  3. Codeforces 986C AND Graph dfs

    原文链接https://www.cnblogs.com/zhouzhendong/p/9161514.html 题目传送门 - Codeforces 986C 题意 给定 $n,m (0\leq n\ ...

  4. 线程有gil锁

    gil锁作用: 遇到阻塞( 比如 recv() , accept() )就切换

  5. Tree Reconstruction Gym - 101911G(构造)

    ---恢复内容开始--- Monocarp has drawn a tree (an undirected connected acyclic graph) and then has given ea ...

  6. POJ 1330 Nearest Common Ancestors (模板题)【LCA】

    <题目链接> 题目大意: 给出一棵树,问任意两个点的最近公共祖先的编号. 解题分析:LCA模板题,下面用的是树上倍增求解. #include <iostream> #inclu ...

  7. mongoose之操作mongoDB数据库

    mongoose是node.js操作mongoDB数据库的一种工具,借助于mongoose,我们可以便捷的完成一些数据库的基本操作,基本使用如下: 1.安装 npm install mongoose ...

  8. php手撸轻量级开发(三)composer小白入门

    composer介绍 composer是php的一个依赖管理工具,在项目中声明所依赖的外部工具库,会自动安装这些工具库及所依赖的库文件. 对我等初阶工程狮来说,就是拉别人的代码,用别人的库,一个进阶版 ...

  9. c++容器加迭代器和python装饰器的对比

    c++利用对象实现简单数据的测试: class TestDataEmptyArray { public: static vector<int> get_array() { std::vec ...

  10. python 数据结构之二分查找的递归和普通实现

    二分查找就是待查找的列表进行分半搜索 如下所示 二分查找普通实现: def erfen(alist, item): start = 0 end = len(alist) - 1 while start ...