zxing介绍生成二维码

生成二维码实现
1)引入依赖 创建maven工程,在pom.xml中引入如下 依赖

<dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>core</artifactId>
            <version>3.3.3</version>
        </dependency>

        <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>javase</artifactId>
            <version>3.3.3</version>
        </dependency>

2)生成二维码方法

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import com.shanjupay.common.domain.BusinessException;
import com.shanjupay.common.domain.CommonErrorCode;
import com.shanjupay.common.util.EncryptUtil;
import org.apache.commons.lang3.StringUtils;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashMap;

/**
 * <P>
 * 工具类
 * </p>
 *
 */
public class MyUtils  {

	/**
	 * 生成二维码
	 * @param content
	 * @param width
	 * @param height
	 * @return
	 */
	public String createQRCode(String content, int width, int height) throws IOException {
		String resultImage = "";
		if (!StringUtils.isEmpty(content)) {
			ServletOutputStream stream = null;
			ByteArrayOutputStream os = new ByteArrayOutputStream();
			@SuppressWarnings("rawtypes")
			HashMap<EncodeHintType, Comparable> hints = new HashMap<>();
			hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); // 指定字符编码为“utf-8”
			hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M); // 指定二维码的纠错等级为中级
			hints.put(EncodeHintType.MARGIN, 1); // 设置图片的边距

			try {
				QRCodeWriter writer = new QRCodeWriter();
				BitMatrix bitMatrix = writer.encode(content, BarcodeFormat.QR_CODE, width, height, hints);

				BufferedImage bufferedImage = MatrixToImageWriter.toBufferedImage(bitMatrix);
				ImageIO.write(bufferedImage, "png", os);
				//添加 data:image/png;base64 前缀方便前端解析
				resultImage = new String("data:image/png;base64," + EncryptUtil.encodeBase64(os.toByteArray()));
				return resultImage;
			} catch (Exception e) {
				e.printStackTrace();
				throw new BusinessException(CommonErrorCode.CUSTOM, "二维码生成失败");
			} finally {
				if (stream != null) {
					stream.flush();
					stream.close();
				}
			}
		}
		return null;
	}

}
  1. 测试 编写在MyUtils类中编写main方法测试 createQRCode
public static void main(String[] args) throws IOException {
		MyUtils myUtils = new MyUtils();
		System.out.println(myUtils.createQRCode("http://www.pbteach.com/", 200, 200));
	}

4,运行main方法,输出二维码图片的base64编码:

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAADIAQAAAACFI5MzAAABRElEQVR42u2YOxLCMAxE5XFBmSNwlBwtPlqOwhEoU3gstJLzAQYqis0MLvDELwWyVmsrop+G/MlXcheMCyatF9XZn6/s5Gp/3pZuY0lqT/OkmOjJLKl6PM3CMoLoTkIsiNKy/Z6M2PbHdA7i2sELEll4URUn6XVqS2l94bmCGck+yke35CO+71UGcxfVxQIpnhN2ImORvAwexIIMtLwIO9Hbph2EZXXaep0yE1P85Nsfivd4trOemIzaD57kWTi4Cy9BgUp2axSIZpYQPjcJT3TRJF/Sg3ZoCYapBeXanfzolqwES4jAJNTdxcIa2EkfUHyYoaa6Z4GVbP3CiNug32Gz8pO1XxA4ucez1ik1iTssupwaNZufuhxqorH9OPnhNecg6/nzngVSsvfBXqBoh/sdlphsdarx6WFqbxXMR/5fsn5LHu2h3xzh77egAAAAAElFTkSuQmCC

将以上编码拷贝到浏览器中,显示二维码图片,实际项目中由前端展示二维码。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值