SMM生成二维码
时间:2023-02-07 14:30:01
1.在pom.xml添加生成二维码的依赖包:
com.google.zxing core 3.3.1 com.google.zxing javase 3.3.1
2.然后是controller层代码:
@RequestMapping("/getQRCode") public void getQRCode(HttpServletRequest request, HttpServletResponse response) throws Exception { String text = "你好,朋友!";///二维码输出的文本内容 // 通过调用写作工具类,获得图片流 ByteArrayOutputStream out = QRCodeUtil.encodeIO(text, "D:\\缓存元气壁纸\\img\\0cb7c825fe3fec52b45a46491290a7df.jpg", true); // 定义返回参数 response.setCharacterEncoding("UTF-8"); response.setContentType("image/jpeg;charset=UTF-8"); response.setContentLength(out.size()); ServletOutputStream outputStream = response.getOutputStream(); outputStream.write(out.toByteArray()); outputStream.flush(); outputStream.close(); }
3.二维码工具类QRCodeUtil:
public class QRCodeUtil { private static final String CHARSET = "utf-8"; private static final String FORMAT_NAME = "JPG"; // 二维码尺寸 private static final int QRCODE_SIZE = 300; // LOGO宽度 private static final int WIDTH = 60; // LOGO高度 private static final int HEIGHT = 60; private static BufferedImage createImage(String content, String imgPath, boolean needCompress) throws Exception { Hashtable hints = new Hashtable(); hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); hints.put(EncodeHintType.CHARACTER_SET, CHARSET); hints.put(EncodeHintType.MARGIN, 1); BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, QRCODE_SIZE, QRCODE_SIZE, hints); int width = bitMatrix.getWidth(); int height = bitMatrix.getHeight(); BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); for (int x = 0; x < width; x ) { for (int y = 0; y < height; y ) { image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF); } } if (imgPath == null || "".equals(imgPath)) { return image; } // 插入图片 QRCodeUtil.insertImage(image, imgPath, needCompress); return image; } private static void insertImage(BufferedImage source, String imgPath, boolean needCompress) throws Exception { File file = new File(imgPath); if (!file.exists()) { System.err.println("" imgPath " 文件不存在!"); return; } Image src = ImageIO.read(new File(imgPath)); int width = src.getWidth(null); int height = src.getHeight(null); if (needCompress) { // 压缩LOGO if (width > WIDTH) { width = WIDTH; } if (height > HEIGHT) { height = HEIGHT; } Image image = src.getScaledInstance(width, height, Image.SCALE_SMOOTH); BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics g = tag.getGraphics(); g.drawImage(image, 0, 0, null); // 画缩小图 g.dispose(); src = image; } // 插入LOGO Graphics2D graph = source.createGraphics(); int x = (QRCODE_SIZE - width) / 2; int y = (QRCODE_SIZE - height) / 2; graph.drawImage(src, x, y, width, height, null); Shape shape = new RoundRectangle2D.Float(x, y, width, width, 6, 6); graph.setStroke(new BasicStroke(3f)); graph.draw(shape); graph.dispose(); } ///获取生成二维码的图片流 public static ByteArrayOutputStream encodeIO(String content,String imgPath,Boolean needCompress) throws Exception { BufferedImage image = QRCodeUtil.createImage(content, imgPath, needCompress); ///创建存储图片二进制流的输出流 ByteArrayOutputStream baos = new ByteArrayOutputStream(); ///将二进制数据写入ByteArrayOutputStream ImageIO.write(image, "jpg", baos); return baos; } }
4.Jsp代码:
定义按钮,定义二维码框架的长度和宽度
【二维码】
5.JS代码: