ImageUtils.java
5.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package com.sincere.weigeng;
import com.drew.imaging.ImageMetadataReader;
import com.drew.imaging.jpeg.JpegMetadataReader;
import com.drew.metadata.Directory;
import com.drew.metadata.Metadata;
import com.drew.metadata.Tag;
import com.sun.imageio.plugins.png.PNGImageReader;
import org.apache.commons.lang.StringUtils;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
public class ImageUtils {
/* *//**
* 获取图片正确显示需要旋转的角度(顺时针)
* @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();
} catch (IOException e) {
e.printStackTrace();
}
return angle;
}*/
/**
* 获得图片调整角度
* make by dongxh 2017年11月1日下午3:40:20
* @param imgFile
* @return
*/
public static Integer getImgRotateAngle(String imgFile){
return 0;
/*int angel = 0;
try {
File file = new File(imgFile);
Metadata metadata = ImageMetadataReader.readMetadata(file);
for (Directory directory : metadata.getDirectories()) {
for (Tag tag : directory.getTags()) {
if (tag.getTagType() == ExifDirectoryBase.TAG_ORIENTATION) {
String description = tag.getDescription();
// System.out.println(description);
if (description.contains("90")) {
// 顺时针旋转90度
angel = 90;
} else if (description.contains("180")) {
// 顺时针旋转180度
angel = 180;
} else if (description.contains("270")) {
// 顺时针旋转270度
angel = 270;
}
}
}
}
// System.out.println(angel);
} catch (Exception e) {
e.printStackTrace();
}
return angel;*/
/*Integer angel = 0;
Metadata metadata = null;
try{
if(StringUtils.isBlank(imgFile))return angel;
File _img_file_ = new File(imgFile);
if(!_img_file_.exists())return angel;
metadata = JpegMetadataReader.readMetadata(_img_file_);
Directory directory = metadata.getDirectory(ExifDirectory.class);
Iterator iterator =directory.getTagIterator();
while (iterator.hasNext()){
System.out.println("directory:"+iterator.next().toString());
}
if(directory != null && directory.containsTag(ExifDirectory.TAG_ORIENTATION)){
int orientation = directory.getInt(ExifDirectory.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(Exception e){
e.printStackTrace();
}
return angel;*/
}
/**
* 旋转照片
* @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);
int swidth=src_width;
int sheight=src_height;
if(angel==90||angel==270){
swidth = src_height;
sheight= src_width;
}
Rectangle rect_des = new Rectangle(new Dimension(swidth,sheight));
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;
}
}