1 Star 2 Fork 0

王天文/cento-practice

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
ImageUtil.java 1.71 KB
一键复制 编辑 原始数据 按行查看 历史
王天文 提交于 1年前 . 添加注释
package com.xiaobai.itextpdf;
import org.apache.poi.util.IOUtils;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.*;
/**
* @author wangtw
* @ClassName ImageUtil
* @description: 图片工具类
* @date 2024/1/1620:36
*/
public class ImageUtil {
/**
* 生成带文字的图片
* @return
*/
public static String createImage() {
int imageWidth = 80;
int imageHeight = 30;
BufferedImage image = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = image.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
Color c = new Color(255, 255, 255);
g2.setColor(c);// 设置背景色
g2.fillRect(0, 0, imageWidth, imageHeight);
String code = "000153";
// 设置文字字体
Font font = new Font(null, Font.PLAIN, 10);
g2.setFont(font);
g2.setColor(new Color(0, 0, 0));
// 文字起始位置
g2.drawString(code, 5, 15);
g2.dispose();
String imagePath = "D:\\usr\\local\\zeus\\resource\\temp/aaa.jpg";
OutputStream baos = null;
try {
baos = new FileOutputStream(new File(imagePath));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
ImageIO.write(image, "jpg", baos);
} catch (IOException e) {
e.printStackTrace();
}
// 关闭输出流
IOUtils.closeQuietly(baos);
return imagePath;
}
public static void main(String[] args) {
createImage();
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/wangtianwen1996/cento-practice.git
git@gitee.com:wangtianwen1996/cento-practice.git
wangtianwen1996
cento-practice
cento-practice
master

搜索帮助