java 生成带logo的二维码

it2026-01-11  7

package com.enation.app.javashop.framework.util; import com.google.gson.Gson; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.MultiFormatWriter; import com.google.zxing.WriterException; import com.google.zxing.client.j2se.MatrixToImageWriter; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; import lombok.SneakyThrows; import javax.imageio.ImageIO; import java.awt.*; import java.awt.geom.RoundRectangle2D; import java.awt.image.BufferedImage; import java.io.*; import java.net.URL; import java.util.HashMap; import java.util.Map; /** * 自提二维码 */ public class CodeImageUtil { // 默认二维码宽度 public static final int WIDTH = 300; // 默认二维码高度 public static final int HEIGHT = 300; // 默认二维码文件格式 public static final String FORMAT = "png"; // 二维码参数 public static final Map<EncodeHintType, Object> HINTS = new HashMap<EncodeHintType, Object>(); //初始化编码格式等参数 static { // 字符编码 HINTS.put(EncodeHintType.CHARACTER_SET, "utf-8"); HINTS.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); // 二维码与图片边距 HINTS.put(EncodeHintType.MARGIN, 2); } /** * * @description:功能描述 将二维码写出到输出流中 * @param object 二维码内容即要存储在二维码中的内容(扫描二维码之后获取的内容) * @param stream 输出流 * @param width 二维码宽 * @param height 二维码高 * @throws WriterException * @throws IOException * @see: 需要参见的其它内容 */ public static void writeToStream(Object object, OutputStream stream, Integer width, Integer height) throws WriterException, IOException { if(width==null){ width=WIDTH; } if(height==null){ height=HEIGHT; } Gson gson = new Gson(); String json = gson.toJson(object); BitMatrix bitMatrix = new MultiFormatWriter().encode(json, BarcodeFormat.QR_CODE, width, height, HINTS); MatrixToImageWriter.writeToStream(bitMatrix, FORMAT, stream); } @SneakyThrows public static InputStream write(InputStream in){ // 生成二维码 //*************添加logo***************** //读取二维码图片 BufferedImage bufferedImage = ImageIO.read(in); //获取画笔 Graphics2D graphics = bufferedImage.createGraphics(); String imageUrl = "图片地址"; URL url = new URL(imageUrl); //打开网络输入流 InputStream inputStream = url.openStream(); //读取logo图片 BufferedImage logo = ImageIO.read(inputStream); //设置二维码大小,太大了会覆盖二维码,此处为20% int logoWidth = logo.getWidth() > bufferedImage.getWidth()*2 /10 ? (bufferedImage.getWidth()*2 /10) : logo.getWidth(); int logoHeight = logo.getHeight() > bufferedImage.getHeight()*2 /10 ? (bufferedImage.getHeight()*2 /10) : logo.getHeight(); //设置logo图片放置的位置,中心 int x = (bufferedImage.getWidth() - logoWidth) / 2; int y = (bufferedImage.getHeight() - logoHeight) / 2; //开始合并并绘制图片 graphics.drawImage(logo,x,y,logoWidth,logoHeight,null); graphics.drawRoundRect(x,y,logoWidth,logoHeight,15,15); //logob边框大小 graphics.setStroke(new BasicStroke(2)); //logo边框颜色 graphics.setColor(Color.WHITE); graphics.drawRect(x,y,logoWidth,logoHeight); graphics.dispose(); logo.flush(); bufferedImage.flush(); ByteArrayOutputStream os = new ByteArrayOutputStream(); ImageIO.write(bufferedImage, "jpg" ,os); in = new ByteArrayInputStream(os.toByteArray()); return in; } } ByteArrayOutputStream out = new ByteArrayOutputStream(); String qrcode="http://m.buyer.gdynyp.com/goods/834?channelid="+providerDO.getFindCode()+""; // 生成二维码图片 CodeImageUtil.writeToStreamString(qrcode, out, 366, 366); InputStream in = new ByteArrayInputStream(out.toByteArray()); in = CodeImageUtil.write(in);
最新回复(0)