本文实例为大家分享了java实现图片角度旋转并获得图片信息的具体代码,供大家参考,具体内容如下
?
|
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
|
public class demo {
/**
* 调整图片角度
* make by dongxh 2017年11月1日下午3:51:08
* @param src
* @param angel
* @return
*/
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;
}
/**
* 计算旋转参数
* make by dongxh 2017年11月1日下午3:51:29
* @param src
* @param angel
* @return
*/
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 rectangle(new dimension(des_width, des_height));
}
/**
* 获得图片调整角度
* make by dongxh 2017年11月1日下午3:40:20
* @param imgfile
* @return
*/
public static integer getimgrotateangle(string imgfile){
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);
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;
}
/**
* 调整图片角度
* make by dongxh 2017年11月1日下午4:31:20
* @param imgfile
*/
public static void rotateimage(string imgfile){
try {
if(stringutils.isblank(imgfile)){
file _img_file_ = new file(imgfile);
if(_img_file_.exists()){
integer angel = getimgrotateangle(imgfile);
if(angel==0)return;
bufferedimage src = imageio.read(_img_file_);
bufferedimage des = rotate(src, angel);
imageio.write(des,"jpg", _img_file_);
}
}
} catch (ioexception e) {
e.printstacktrace();
}
}
public static void main(string[] args)throws exception{
string frompic = "d://88888//img_20171004_122718.jpg";
//rotateimage(file);
integer angel = getimgrotateangle(frompic);
system.out.println(angel);
thumbnails.of(frompic)
.rotate(angel)
.scale(0.2f)
.outputformat("jpg")
.outputquality(0.2f)
.tofile(frompic);
system.out.println("==end==");
}
}
|
获得图片使用metadata-extractor
?
|
1
2
3
4
5
|
<dependency>
<groupid>com.drewnoakes</groupid>
<artifactid>metadata-extractor</artifactid>
<version>2.4.0-beta-1</version>
</dependency>
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。
原文链接:https://blog.csdn.net/fackyou200/article/details/78435709
相关文章
猜你喜欢
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
- ASP.NET自助建站系统的数据库备份与恢复操作指南 2025-06-10
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-06-04 96
-
2025-06-05 42
-
2025-05-29 96
-
使用Visual Studio 2010/2013编译V8引擎步骤分享
2025-05-27 90 -
2025-05-25 50
热门评论

