当前位置:网站首页>图片打水印 缩放 和一个输入流的转换
图片打水印 缩放 和一个输入流的转换
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
边栏推荐
- Js逆向——捅了【马蜂窝】的ob混淆与加速乐
- 让我们,从头到尾,通透网络I/O模型
- 405 method not allowed appears when the third party jumps to the website
- Meet in the middle
- Neon Optimization: summary of performance optimization experience
- Taro2.* applet configuration sharing wechat circle of friends
- 从底层结构开始学习FPGA----FIFO IP的定制与测试
- Yunna | work order management software, work order management software app
- Atomic in golang and CAS operations
- 1123. The nearest common ancestor of the deepest leaf node
猜你喜欢
AI 从代码中自动生成注释文档
LLDP兼容CDP功能配置
Go zero micro service practical series (IX. ultimate optimization of seckill performance)
Lldp compatible CDP function configuration
LeetCode:1175. Prime permutation
Yunna - work order management system and process, work order management specification
Start from the bottom structure to learn the customization and testing of fpga---- FIFO IP
2022 Google CTF SEGFAULT LABYRINTH wp
Yunna | work order management measures, how to carry out work order management
Typical problems of subnet division and super network construction
随机推荐
Body mass index program, entry to write dead applet project
从零开始匹配vim(0)——vimscript 简介
Realize incremental data synchronization between MySQL and ES
字节P7专业级讲解:接口测试常用工具及测试方法,福利文
golang中的atomic,以及CAS操作
THREE. AxesHelper is not a constructor
Analysis of mutex principle in golang
JTAG principle of arm bare board debugging
Js逆向——捅了【马蜂窝】的ob混淆与加速乐
Asset security issues or constraints on the development of the encryption industry, risk control + compliance has become the key to breaking the platform
C language instance_ four
Gnet: notes on the use of a lightweight and high-performance go network framework
黑马笔记---异常处理
Taro applet enables wxml code compression
736. Lisp 语法解析 : DFS 模拟题
Google发布安全更新,修复Chrome中已被利用的0 day
1123. 最深叶节点的最近公共祖先
docker 方法安装mysql
Your cache folder contains root-owned files, due to a bug in npm ERR! previous versions of npm which
JTAG debugging experience of arm bare board debugging