当前位置:网站首页>图片打水印 缩放 和一个输入流的转换
图片打水印 缩放 和一个输入流的转换
2022-07-06 17:56:00 【全栈程序员站长】
大家好,又见面了,我是全栈君
图片打水印和缩放:
public final static BufferedImage[] pressImage(InputStream srcImg, String waterImg,float alpha) throws IOException {
//File file = new File(targetImg);
Image image = ImageIO.read(srcImg);
int width = image.getWidth(null);
int height = image.getHeight(null);
BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = bufferedImage.createGraphics();
g.drawImage(image, 0, 0, width, height, null);
Image waterImage = ImageIO.read(new File(waterImg)); // 水印文件
int width_1 = waterImage.getWidth(null);
int height_1 = waterImage.getHeight(null);
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
int div=(int) (0.1*width_1);
for(int y=0,row=0;y<height;y+=height_1+div,row++)
{
int x=0;
for(;x<width;x+=width_1+div)
{
g.drawImage(waterImage, x, y, width_1, height_1, null); // 水印文件结束
}
}
g.dispose();
BufferedImage bufferedImage2=Thumbnails.of(bufferedImage).size(100, 100).asBufferedImage();
return new BufferedImage[]{bufferedImage,bufferedImage2};
}
图片的缩放使用了Thumbnails这个工具,(事实上它也能够用来打水印的,只是我还没有研究)
jar 包或者maven 请自行搜索
须要特别注意的是 Thumbnails 默认是依照等比例进行缩放的而且它的缩放规则是:
如果 把图片所放到 200*300
* 若图片横比200小。高比300小。不变 * 若图片横比200小,高比300大。高缩小到300,图片比例不变 * 若图片横比200大,高比300小。横缩小到200。图片比例不变 * 若图片横比200大,高比300大。图片按比例缩小,横为200或高为300
bufferInage转换 inputSteam
private InputStream getInputstreamFromBufferedImage(BufferedImage img) throws IOException{
ByteArrayOutputStream bs =new ByteArrayOutputStream();
ImageOutputStream imOut =ImageIO.createImageOutputStream(bs);
ImageIO.write(img,"jpg",imOut); //scaledImage1为BufferedImage,jpg为图像的类型
InputStream is =new ByteArrayInputStream(bs.toByteArray());
return is;
}
版权声明:本文博主原创文章,博客,未经同意不得转载。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/116890.html原文链接:https://javaforall.cn
边栏推荐
- 接收用户输入,身高BMI体重指数检测小业务入门案例
- LeetCode:1175. Prime permutation
- What does security capability mean? What are the protection capabilities of different levels of ISO?
- [Niuke] [noip2015] jumping stone
- 搭建【Redis in CentOS7.x】
- C language instance_ four
- Transformation transformation operator
- Data type of pytorch tensor
- ClickHouse字段分组聚合、按照任意时间段粒度查询SQL
- Analysis of mutex principle in golang
猜你喜欢
[100 cases of JVM tuning practice] 05 - Method area tuning practice (Part 2)
Your cache folder contains root-owned files, due to a bug in npm ERR! previous versions of npm which
[Niuke] [noip2015] jumping stone
微信公众号发送模板消息
Gazebo的安装&与ROS的连接
Send template message via wechat official account
力扣1037. 有效的回旋镖
Clickhouse fields are grouped and aggregated, and SQL is queried according to the granularity of any time period
如何管理分布式团队?
Asset security issues or constraints on the development of the encryption industry, risk control + compliance has become the key to breaking the platform
随机推荐
交叉验证如何防止过拟合
Match VIM from zero (0) -- Introduction to vimscript
Asset security issues or constraints on the development of the encryption industry, risk control + compliance has become the key to breaking the platform
剑指 Offer II 035. 最小时间差-快速排序加数据转换
LLDP兼容CDP功能配置
微信公众号发送模板消息
Neon Optimization: summary of performance optimization experience
Make Jar, Not War
The difference between spin and sleep
分享一个通用的so动态库的编译方法
接收用户输入,身高BMI体重指数检测小业务入门案例
THREE. AxesHelper is not a constructor
C语言实例_4
【信号与系统】
ARM裸板调试之JTAG调试体验
mysql: error while loading shared libraries: libtinfo. so. 5: cannot open shared object file: No such
736. Lisp 语法解析 : DFS 模拟题
数据手册中的词汇
黑马笔记---异常处理
【C语言进阶篇】指针的8道笔试题