由于手机系统的问题,导致照片上传旋转90度,android前置旋转270度
接下来为大家解决这一问题
1,获取上传文件的源文件的元数据
2,根据元数据的获取Orientation
3,根据Orientation来旋转图片

2-----------------------------------------------------------

import java.io.File;
import java.util.Iterator;

import com.drew.imaging.jpeg.JpegMetadataReader;
import com.drew.metadata.Directory;
import com.drew.metadata.Metadata;
import com.drew.metadata.Tag;
import com.drew.metadata.exif.ExifDirectory;


/**
 * 测试用于读取图片的EXIF信息
 * 
 * @author Winter Lau
 */
public class ExifRead {
private static final Logger LOG = Logger.getLogger(ExifRead.class);


public static int getOrientation2(String str) throws Exception {
File jpegFile = new File(str);
Metadata metadata = JpegMetadataReader.readMetadata(jpegFile);
Directory exif = metadata.getDirectory(ExifDirectory.class);
// JpegDirectory jpegDirectory =
// (JpegDirectory)metadata.getDirectory(JpegDirectory.class);
int orientation = 0;
Iterator tags = exif.getTagIterator();
while (tags.hasNext()) {
Tag tag = (Tag) tags.next();
if ("Orientation".equalsIgnoreCase(tag.getTagName())) {
orientation = getOrientation(tag.getDescription());
break;
}
}


Integer turn = 360;
if (orientation == 0 || orientation == 1) {
turn = 360;
} else if (orientation == 3) {
turn = 180;
} else if (orientation == 6) {
turn = 90;
} else if (orientation == 8) {
turn = 270;
}
return turn;
}


public static int getOrientation(String orientation) {
int tag = 0;
if ("Top, left side (Horizontal / normal)".equalsIgnoreCase(orientation)) {
tag = 1;
} else if ("Top, right side (Mirror horizontal)".equalsIgnoreCase(orientation)) {
tag = 2;
} else if ("Bottom, right side (Rotate 180)".equalsIgnoreCase(orientation)) {
tag = 3;
} else if ("Bottom, left side (Mirror vertical)".equalsIgnoreCase(orientation)) {
tag = 4;
} else if ("Left side, top (Mirror horizontal and rotate 270 CW)".equalsIgnoreCase(orientation)) {
tag = 5;
} else if ("Right side, top (Rotate 90 CW)".equalsIgnoreCase(orientation)) {
tag = 6;
} else if ("Right side, bottom (Mirror horizontal and rotate 90 CW)".equalsIgnoreCase(orientation)) {
tag = 7;
} else if ("Left side, bottom (Rotate 270 CW)".equalsIgnoreCase(orientation)) {
tag = 8;
}
return tag;
}
}



3,----------------------------------------------------------
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;


import javax.imageio.ImageIO;


public class RotateImage {


public static void main(String[] args) throws IOException {
BufferedImage src = ImageIO.read(new File("E:/java/0.jpg"));
BufferedImage des = RotateImage.Rotate(src, 360);
ImageIO.write(des, "jpg", new File("E:/java/20.jpg"));
}


public static BufferedImage Rotate(Image src, int angel) {
int src_width = src.getWidth(null);
int src_height = src.getHeight(null);
// calculate the new image size
Rectangle rect_des = CalcRotatedSize(new Rectangle(new Dimension(src_width, src_height)), angel);


BufferedImage res = null;
res = new BufferedImage(rect_des.width, rect_des.height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = res.createGraphics();
// transform
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);
return res;
}


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 / 90 % 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));
}
------------------------------------------------------------------------------
附上jar包,metadata-extractor-2.3.1    找不到QQ联系736057729

更多相关文章

  1. “罗永浩抖音首秀”销售数据的可视化大屏是怎么做出来的呢?
  2. Nginx系列教程(三)| 一文带你读懂Nginx的负载均衡
  3. 不吹不黑!GitHub 上帮助人们学习编码的 12 个资源,错过血亏...
  4. Android(安卓)客户端Socket 实现及简单封装。
  5. 一起学android之设置ListView数据显示的动画效果(24)
  6. Android中图片保存到本地,并及时更新到系统相册
  7. Android(安卓)Studio 关于Room的警告: Primary key constraint o
  8. Android下UDP通信DEMO
  9. Android音频开发之尝试音频混合

随机推荐

  1. Python编程必备5大工具,你用过几个?
  2. HBase应用实践专场-HBase问题排查思路
  3. 中国HBase技术社区第五届MeetUp
  4. Spark 从 Kafka 读数并发问题
  5. 简单介绍虚拟化技术vs容器化(2)
  6. Apache Flink状态管理和容错机制介绍
  7. Linux由几部分组成?Linux系统结构介绍!
  8. Flink在唯品会的实践
  9. Go语言学习8-接口类型
  10. MySql基础查询-流程控制函数