Java 操作图片进行缩放旋转翻转加水印
创始人
2024-05-27 15:55:50
0

1 纯原生手写图片操作工具类

import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
public class RotateImageUtil {public static BufferedImage rotateImage(BufferedImage bufferedImage, int angel) {if (bufferedImage == null) {return null;}if (angel < 0) {// 将负数角度,纠正为正数角度angel = angel + 360;}int imageWidth = bufferedImage.getWidth(null);int imageHeight = bufferedImage.getHeight(null);// 计算重新绘制图片的尺寸Rectangle rectangle = calculatorRotatedSize(new Rectangle(new Dimension(imageWidth, imageHeight)), angel);// 获取原始图片的透明度int type = bufferedImage.getColorModel().getTransparency();BufferedImage newImage = null;newImage = new BufferedImage(rectangle.width, rectangle.height, type);Graphics2D graphics = newImage.createGraphics();// 平移位置graphics.translate((rectangle.width - imageWidth) / 2, (rectangle.height - imageHeight) / 2);// 旋转角度graphics.rotate(Math.toRadians(angel), imageWidth / 2, imageHeight / 2);// 绘图graphics.drawImage(bufferedImage, null, null);return newImage;}public static BufferedImage rotateImage(Image image, int angel) {if (image == null) {return null;}if (angel < 0) {// 将负数角度,纠正为正数角度angel = angel + 360;}int imageWidth = image.getWidth(null);int imageHeight = image.getHeight(null);Rectangle rectangle = calculatorRotatedSize(new Rectangle(new Dimension(imageWidth, imageHeight)), angel);BufferedImage newImage = null;newImage = new BufferedImage(rectangle.width, rectangle.height, BufferedImage.TYPE_INT_RGB);Graphics2D graphics = newImage.createGraphics();graphics.translate((rectangle.width - imageWidth) / 2, (rectangle.height - imageHeight) / 2);graphics.rotate(Math.toRadians(angel), imageWidth / 2, imageHeight / 2);graphics.drawImage(image, null, null);return newImage;}//计算旋转后的尺寸private static Rectangle calculatorRotatedSize(Rectangle src, int angel) {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));}
}

main方法调用

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
public class test {//测试一下,直接抛出异常了,小伙伴们项目中要记住用try···catch···哦public static void main(String[] args) throws Exception {File picture = new File("C:\\Users\\Administrator\\Desktop\\saga.png");BufferedImage sourceImg = ImageIO.read(new FileInputStream(picture));if ( sourceImg.getHeight() > sourceImg.getWidth() ) {//旋转,这里数值代表顺时针,逆时针,各位可以换其他角度玩儿玩儿BufferedImage rotate = RotateImageUtil.rotateImage(sourceImg, -90);ImageIO.write(rotate, "png", new File("D:\\", picture.getName()));InputStream is = new FileInputStream( new File("D:\\", picture.getName()) );byte[] data = new byte[is.available()];is.read(data);is.close();} else {System.out.println(false);}}
}

2 Hutool操作图片

cn.hutoolhutool-all5.1.0

操作图片示例代码:

import cn.hutool.core.img.ImgUtil;
import cn.hutool.core.io.FileUtil;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;public class HutoolImage {public static void main(String[] args)  {//提供两种重载方法,按长宽缩放,按比例缩放ImgUtil.scale(FileUtil.file("C:\\Users\\DELL\\Desktop\\animal.jpg"),FileUtil.file("C:\\Users\\DELL\\Desktop\\Image\\animal_result.jpg"),0.5f   //图片缩放比例);//图片按一定的尺寸裁剪ImgUtil.cut(FileUtil.file("C:\\Users\\DELL\\Desktop\\animal.jpg"),FileUtil.file("C:\\Users\\DELL\\Desktop\\Image\\animal_small.jpg"),new Rectangle(200, 200, 100, 100)//裁剪的矩形区域);//按照行列裁剪切片ImgUtil.slice(FileUtil.file("C:\\Users\\DELL\\Desktop\\animal.jpg"), FileUtil.file("C:\\Users\\DELL\\Desktop\\test\\"), 10, 10);//将图片彩色转黑白色ImgUtil.gray(FileUtil.file("C:\\Users\\DELL\\Desktop\\animal.jpg"), FileUtil.file("C:\\Users\\DELL\\Desktop\\Image\\animal_black.jpg"));//给图片添加文字水印ImgUtil.pressText(//FileUtil.file("C:\\Users\\DELL\\Desktop\\animal.jpg"), //FileUtil.file("C:\\Users\\DELL\\Desktop\\Image\\animal_logo.jpg"), //"版权所有", Color.BLUE, //文字new Font("宋体", Font.BOLD, 100), //字体0, //x坐标修正值。 默认在中间,偏移量相对于中间偏移0, //y坐标修正值。 默认在中间,偏移量相对于中间偏移0.8f//透明度:alpha 必须是范围 [0.0, 1.0] 之内(包含边界值)的一个浮点数字);//旋转图片180度BufferedImage image = null;try {image = (BufferedImage) ImgUtil.rotate(ImageIO.read(FileUtil.file("C:\\Users\\DELL\\Desktop\\animal.jpg")), 180);} catch (IOException e) {e.printStackTrace();}ImgUtil.write(image, FileUtil.file("C:\\Users\\DELL\\Desktop\\Image\\animal_tangle.jpg"));//图片水平翻转ImgUtil.flip(FileUtil.file("C:\\Users\\DELL\\Desktop\\animal.jpg"), FileUtil.file("C:\\Users\\DELL\\Desktop\\Image\\animal_flat.jpg"));//转换图片存储格式ImgUtil.convert(FileUtil.file("C:\\Users\\DELL\\Desktop\\animal.jpg"), FileUtil.file("C:\\Users\\DELL\\Desktop\\Image\\animal.png"));}
}

3 谷歌开源框架Thumbnailator

Thumbnailator 是一个优秀的图片处理的Google开源Java类库。处理效果远比Java API的好。从API提供现有的图像文件和图像对象的类中简化了处理过程,两三行代码就能够从现有图片生成处理后的图片,且允许微调图片的生成方式,同时保持了需要写入的最低限度的代码量。还支持对一个目录的所有图片进行批量处理操作

支持的处理操作:图片缩放,区域裁剪,水印,旋转,保持比例。

另外值得一提的是,Thumbnailator至今仍不断更新,怎么样,感觉很有保障吧!
Thumbnailator官网: http://code.google.com/p/thumbnailator/

文章教程:https://blog.csdn.net/qq_37712731/article/details/118514686

1、指定大小进行缩放

//size(宽度, 高度)/** 若图片横比200小,高比300小,不变* 若图片横比200小,高比300大,高缩小到300,图片比例不变* 若图片横比200大,高比300小,横缩小到200,图片比例不变* 若图片横比200大,高比300大,图片按比例缩小,横为200或高为300*/
Thumbnails.of("images/a380_1280x1024.jpg").size(200, 300).toFile("c:/a380_200x300.jpg");Thumbnails.of("images/a380_1280x1024.jpg").size(2560, 2048).toFile("c:/a380_2560x2048.jpg");1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.

2、按照比例进行缩放

//scale(比例)
Thumbnails.of("images/a380_1280x1024.jpg").scale(0.25f).toFile("c:/a380_25%.jpg");Thumbnails.of("images/a380_1280x1024.jpg").scale(1.10f).toFile("c:/a380_110%.jpg");1.2.3.4.5.6.7.8.

3、不按照比例,指定大小进行缩放

//rotate(角度),正数:顺时针负数:逆时针
Thumbnails.of("images/a380_1280x1024.jpg").size(1280,1024).rotate(90).toFile("c:/a380_rotate+90.jpg");Thumbnails.of("images/a380_1280x1024.jpg").size(1280,1024).rotate(-90).toFile("c:/a380_rotate-90.jpg");1.2.3.4.5.6.7.8.9.10.
//keepAspectRatio(false)默认是按照比例缩放的
Thumbnails.of("images/a380_1280x1024.jpg").size(200,200).keepAspectRatio(false).toFile("c:/a380_200x200.jpg");1.2.3.4.5.

5、水印

//watermark(位置,水印图,透明度)
Thumbnails.of("images/a380_1280x1024.jpg").size(1280,1024).watermark(Positions.BOTTOM_RIGHT,ImageIO.read(newFile("images/watermark.png")),0.5f).outputQuality(0.8f).toFile("c:/a380_watermark_bottom_right.jpg");Thumbnails.of("images/a380_1280x1024.jpg").size(1280,1024).watermark(Positions.CENTER,ImageIO.read(newFile("images/watermark.png")),0.5f).outputQuality(0.8f).toFile("c:/a380_watermark_center.jpg");1.2.3.4.5.6.7.8.9.10.11.12.

6、裁剪

//sourceRegion()//图片中心400*400的区域
Thumbnails.of("images/a380_1280x1024.jpg").sourceRegion(Positions.CENTER,400,400).size(200,200).keepAspectRatio(false).toFile("c:/a380_region_center.jpg");//图片右下400*400的区域
Thumbnails.of("images/a380_1280x1024.jpg").sourceRegion(Positions.BOTTOM_RIGHT,400,400).size(200,200).keepAspectRatio(false).toFile("c:/a380_region_bootom_right.jpg");//指定坐标
Thumbnails.of("images/a380_1280x1024.jpg").sourceRegion(600,500,400,400).size(200,200).keepAspectRatio(false).toFile("c:/a380_region_coord.jpg");1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.

7、转化图像格式

//outputFormat(图像格式)
Thumbnails.of("images/a380_1280x1024.jpg").size(1280,1024).outputFormat("png").toFile("c:/a380_1280x1024.png");Thumbnails.of("images/a380_1280x1024.jpg").size(1280,1024).outputFormat("gif").toFile("c:/a380_1280x1024.gif");1.2.3.4.5.6.7.8.9.10.

8、输出到OutputStream

//toOutputStream(流对象)
OutputStreamos=newFileOutputStream("c:/a380_1280x1024_OutputStream.png");
Thumbnails.of("images/a380_1280x1024.jpg").size(1280,1024).toOutputStream(os);1.2.3.4.5.

9、输出到BufferedImage

//asBufferedImage()返回BufferedImage
BufferedImagethumbnail=Thumbnails.of("images/a380_1280x1024.jpg").size(1280,1024).asBufferedImage();
ImageIO.write(thumbnail,"jpg",newFile("c:/a380_1280x1024_BufferedImage.jpg"));

4 总结

谷歌开源框架处理图片实测性能较差,用起来比较方便,建议用Hutool因为只是对awt做的封装不用自己写,性能也高。

原文地址:https://blog.csdn.net/ZGL_cyy/article/details/126680559

相关内容

热门资讯

教研活动主持词 教研活动主持词  主持人在台上表演的灵魂就表现在主持词中。在当下的中国社会,主持成为很多活动不可或缺...
艺术节主持词开场白 艺术节主持词开场白  什么是艺术节  艺术节是文艺工作者及艺术家、艺术爱好者之间学术交流与学习的重要...
老板在公司年会致辞 老板在公司年会致辞15篇  在平平淡淡的学习、工作、生活中,大家最不陌生的就是致辞了吧,致辞具有针对...
央视春晚主持词台词 央视春晚主持词台词  主持词是主持人在节目进行过程中用于串联节目的串联词。在各种集会、活动不断增多的...
教师节朗诵晚会串词 教师节朗诵晚会串词  主持词需要富有情感,充满热情,才能有效地吸引到观众。在当今不断发展的世界,越来...
趣味运动会主持稿 趣味运动会主持稿(通用7篇)  在充满活力,日益开放的今天,很多地方都会使用到主持稿,主持稿是主持人...
鼠年文艺晚会的主持词 鼠年文艺晚会的主持词  1、喜鹊枝头叫,猴年已来到。好运来报道,健康身边绕。跨羊威武耀,进财装元宝。...
订婚仪式主持词 订婚仪式主持词(通用16篇)  主持词的写作需要将主题贯穿于所有节目之中。在人们积极参与各种活动的今...
文化艺术节闭幕词 文化艺术节闭幕词(精选6篇)  在日新月异的现代社会中,我们都可能会用到闭幕词,闭幕词的作用是辅助讲...
幼儿园签约仪式主持词 幼儿园签约仪式主持词  主持词没有固定的格式,他的最大特点就是富有个性。现今社会在不断向前发展,主持...
电影后来的我们台词 电影后来的我们台词  电影《后来的我们》可以说是风波不断,那么后来的我们经典语录台词有哪些?下面是小...
音乐串词 音乐串词甲:金牛报春来! 又是瑞雪飘飞的季节了,我们打一小学这个大家庭每到这时候总会欢聚一堂。 共庆...
不差钱的经典台词 不差钱的经典台词  赵本山:苏格兰调情。  小沈阳:人家是纯爷们。  丫蛋:洪湖水浪打浪,长江后浪推...
《终结者2》的经典台词 《终结者2》的经典台词  1.I’ll be back。  我会回来的。  2.I need you...
婚宴新娘致辞 婚宴新娘致辞(合集15篇)  无论在学习、工作或是生活中,大家都对致辞很是熟悉吧,在各种重大的庆典、...
单位领导证婚词 单位领导证婚词尊敬的各位来宾、各位亲朋好友,女士们、先生们:大家中--午--好!今天,艳阳高照,天赐...
诵经典唱红歌主持词 诵经典唱红歌主持词  女:各位领导、各位来宾,  男:老师们、同学们,  合:大家好!  女:五月良...
笑傲江湖之东方不败台词 笑傲江湖之东方不败台词大全  天下风云出我辈,  一入江湖岁月催。  皇图霸业谈笑中,  不胜人生一...
学校元宵联欢晚会的主持词 学校元宵联欢晚会的主持词(精选6篇)  利用在中国拥有几千年文化的诗词能够有效提高主持词的感染力。我...
新婚婚礼主持词 新婚婚礼主持词  主持词的写作要突出活动的主旨并贯穿始终。我们眼下的社会,各种集会的节目都通过主持人...