今天,写了一个二维码制作的工具类,在这里和大家分享一下。
需要在pom文件中添加jar包 <!--zxing二维码包--> <dependency> <groupId>com.google.zxing</groupId> <artifactId>core</artifactId> <version>3.2.1</version> </dependency>
java代码
package com.maque.boothero.common; import com.alibaba.druid.util.StringUtils; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.MultiFormatWriter; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; import javax.imageio.ImageIO; import java.awt.*; import java.awt.geom.RoundRectangle2D; import java.awt.image.BufferedImage; import java.io.*; import java.util.Hashtable; /** * @program: boot-hero * @description: 生成二维码工具类 * @author: maque * @create: 2018-07-19 18:01 */ public class QRCodeImageUtil { // 二维码尺寸 private static final int QRCODE_SIZE = 300; // 说明文本高度 private static final int HEIGHT_TEXT = 30; // LOGO宽度 private static final int WIDTH_LOGO = 110; // LOGO高度 private static final int HEIGHT_LOGO = 110; private static final String Encoding = "utf-8"; private static final String FORMAT_NAME = "JPG"; private static final int BLACK = 0xFF000000; private static final int WHITE = 0xFFFFFFFF; public static void main(String[] args) { String content = "http://www.baidu.com";//可以将此处的地址换成你的登录地址 String text = "百度一下就知道,百度一下就知道,百度一下就知道,百度一下就知道,百度一下就知道,"; try { FileInputStream is = new FileInputStream(new File("E:\\temp\\toPDF\\555.jpg")); ByteArrayOutputStream bytestream = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int ch; while ((ch = is.read(buffer)) != -1) { bytestream.write(buffer,0,ch); } byte[] data = bytestream.toByteArray(); bytestream.close(); // byte[] imageBody = createQRCode("http://www.baidu.com"); // byte[] imageBody = createQRCodeW