日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java旋转图片并画出_java实现图片角度旋转并获得图片信息

發(fā)布時間:2025/4/5 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java旋转图片并画出_java实现图片角度旋转并获得图片信息 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

本文實例為大家分享了java實現(xiàn)圖片角度旋轉并獲得圖片信息的具體代碼,供大家參考,具體內容如下

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;

}

/**

* 計算旋轉參數(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));

}

/**

* 獲得圖片調整角度

* 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

com.drewnoakes

metadata-extractor

2.4.0-beta-1

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持萬仟網(wǎng)。

如您對本文有疑問或者有任何想說的,請點擊進行留言回復,萬千網(wǎng)友為您解惑!

總結

以上是生活随笔為你收集整理的java旋转图片并画出_java实现图片角度旋转并获得图片信息的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。