java實(shí)現(xiàn)圖片角度旋轉(zhuǎn)并獲得圖片信息
本文實(shí)例為大家分享了java實(shí)現(xiàn)圖片角度旋轉(zhuǎn)并獲得圖片信息的具體代碼,供大家參考,具體內(nèi)容如下
public class Demo {
/**
* 調(diào)整圖片角度
* 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;
}
/**
* 計(jì)算旋轉(zhuǎn)參數(shù)
* 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));
}
/**
* 獲得圖片調(diào)整角度
* 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旋轉(zhuǎn)90
angel = 90;
}else if( 3 == orientation){
//3旋轉(zhuǎn)180
angel = 180;
}else if( 8 == orientation){
//8旋轉(zhuǎn)90
angel = 270;
}
}
}catch(Exception e){
e.printStackTrace();
}
return angel;
}
/**
* 調(diào)整圖片角度
* 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
<dependency> <groupId>com.drewnoakes</groupId> <artifactId>metadata-extractor</artifactId> <version>2.4.0-beta-1</version> </dependency>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Spring Data JPA實(shí)現(xiàn)分頁(yè)P(yáng)ageable的實(shí)例代碼
本篇文章主要介紹了Spring Data JPA實(shí)現(xiàn)分頁(yè)P(yáng)ageable的實(shí)例代碼,具有一定的參考價(jià)值,有興趣的可以了解一下2017-07-07
Java中do-while循環(huán)的使用方法及注意事項(xiàng)詳解
這篇文章主要介紹了Java中do-while循環(huán)的使用方法及注意事項(xiàng)的相關(guān)資料,在Java編程中,do-while循環(huán)是一種基本的循環(huán)控制結(jié)構(gòu),它至少執(zhí)行一次循環(huán)體,然后根據(jù)條件判斷是否繼續(xù),文中將用法介紹的非常詳細(xì),需要的朋友可以參考下2024-10-10
詳細(xì)介紹Java關(guān)鍵字throw?throws?Throwable的用法與區(qū)別
這篇文章主要介紹了java中throws與throw及Throwable的用法和區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04
不使用myeclipse注冊(cè)機(jī)得到myeclipse注冊(cè)碼的方法(myeclipse序列號(hào))
本文為大家介紹不使用myeclipse注冊(cè)機(jī)就能得到myeclipse注冊(cè)碼(序列號(hào))的方法, 運(yùn)行下面的JAVA代碼就可以了2014-01-01
java調(diào)用openoffice將office系列文檔轉(zhuǎn)換為PDF的示例方法
本篇文章主要介紹了java使用openoffice將office系列文檔轉(zhuǎn)換為PDF的示例方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-11-11
java中的String定義的字面量最大長(zhǎng)度是多少
這篇文章主要介紹了java中的String定義的字面量最大長(zhǎng)度是多少,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12

