代码实例
// 读取原图片信息
//得到文件
File file = new File("d:\\1.png");
//文件转化为图片
Image srcImg = ImageIO.read(file);
//获取图片的宽
int srcImgWidth = srcImg.getWidth(null);
//获取图片的高
int srcImgHeight = srcImg.getHeight(null);
// 加水印
BufferedImage bufImg = new BufferedImage(srcImgWidth, srcImgHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D g = bufImg.createGraphics();
g.drawImage(srcImg, 0, 0, srcImgWidth, srcImgHeight, null);
//使用工具类生成二维码
Image image = createQrCode("二维码内容", 226, 226, "二维码中间logo图片地址");
//将小图片绘到大图片上,表示你的小图片在大图片上的位置。
g.drawImage(image, 263, 700, null);
//或者将图片绘制上去
File file = new File("d:\\2.png");
BufferedImage read = ImageIO.read(file);
Graphics2D k = bufImg.createGraphics();
//将小图片绘到大图片上,表示你的小图片在大图片上的位置。
k.drawImage(read, 650, 188, 150,150,null);
k.setColor(Color.WHITE);
k.dispose();
//设置颜色。
g.setColor(Color.WHITE);
g.dispose();
ByteArrayOutputStream bs = new ByteArrayOutputStream();
ImageOutputStream imOut = ImageIO.createImageOutputStream(bs);
ImageIO.write(bufImg, "png", imOut);
InputStream inputStream = new ByteArrayInputStream(bs.toByteArray());
OutputStream outStream = new FileOutputStream("d:\\3.png");
IOUtils.copy(inputStream, outStream);
inputStream.close();
outStream.close();
private static BufferedImage createQrCode(String content, int width, int height, String logoPath) throws IOException {
QrConfig config = new QrConfig(width, height);
if (logoPath != null) {
Image image = ImageIO.read(new FileInputStream(new File(logoPath)));
config.setImg(image);
}
config.setErrorCorrection(ErrorCorrectionLevel.H);
return QrCodeUtil.generate(
content,
config);
}
二维码生成工具引用
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.2.4</version>
</dependency>
评论 (0)