生成二维码(简洁版)

第一步:导入依赖

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

第二步:创建二维码方法

package com.lc.yangzi.module.marketing.sell;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Hashtable;

/**
 * 二维码生成工具
 *
 * @author: John
 */
public class QRCodeUtil {

	private static final int BLACK = 0xFF000000;
	private static final int WHITE = 0xFFFFFFFF;

	/**
	 * 生成二位码内存图片
	 *
	 * @param content
	 * @param width
	 * @param height
	 * @throws Exception
	 */
	public static BufferedImage toQRCode(String content, int width, int height) throws Exception {
		Hashtable hints = new Hashtable();
		hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
		BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
		return toBufferedImage(bitMatrix);
	}

	/**
	 * 写入文件
	 *
	 * @param content
	 *            内容
	 * @param width
	 *            宽
	 * @param height
	 *            高
	 * @param format
	 *            图片格式,jpg、png
	 * @param path
	 *            图片路径
	 * @throws Exception
	 */
	public static void toQRCode(String content, int width, int height, String format, String path) throws Exception {
		File outputFile = new File(path);
		ImageIO.write(toQRCode(content, width, height), format, outputFile);
	}

	/**
	 * 缓存图生成
	 *
	 * @param matrix
	 * @return
	 */
	private static BufferedImage toBufferedImage(BitMatrix matrix) {
		int width = matrix.getWidth();
		int height = matrix.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, matrix.get(x, y) ? BLACK : WHITE);
			}
		}
		return image;
	}

	public static void main(String[] args) throws Exception {
		String base_url = "D:/";
        String img_url = "/images";
    	String qrcode_url = base_url + img_url + "/qrcode/"+1+".png";

		try {
			File file = new File(base_url+img_url+"/qrcode");
			if (!file.exists() && !file.isDirectory()) {//目录不存在
				System.out.println("//目录不存在");
				file.mkdirs();//不存在则创建目录
			}
			File file1 = new File(base_url + img_url + "/qrcode/"+1+".png");//获取文件
			if (!file1.exists()) {//文件不存在就生成二维码图片
				System.out.println("//图片不存在");
				//第一个参数为二维码内容,如果是网页则直接跳转网页,是接口则直接调用接口,最后一个参数则为二维码地址
				QRCodeUtil.toQRCode("http://192.168.1.61:9909/yz"+"/sell/customerorder/scanQrCode/"+1, 1000, 1000, "png", qrcode_url);
			}else{
				System.out.println("//图片已存在");
			}
		} catch (Exception e) {

		}
	}

}

第三步:接口

    //扫码获取详情
    @RequestMapping(value = "/scanQrCode/{customerOrderId}", method = RequestMethod.GET)
    public ReturnObject<Object> scanQrCode(HttpServletRequest request, @PathVariable(name = "customerOrderId") Long customerOrderId) throws JsonProcessingException {
        QrCodeInfo qrCodeInfo = customerOrderService.selectQrCodeInfoById(customerOrderId);
        if(qrCodeInfo != null) return new ReturnObject<>(ConstantOfReturnCode.GLOBAL_RESULT_SUCESS, "成功", qrCodeInfo);
        return new ReturnObject<>(ConstantOfReturnCode.GLOBAL_RESULT_FALSE,"未查到订单信息");
    }
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值