java生成简单二维码

导入依赖

com.google.zxing core 3.3.0 com.google.zxing javase 3.0.0 代码

package com.example.qrCodeDemo;

import ch.qos.logback.core.encoder.ByteArrayUtil;
import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.itextpdf.text.pdf.qrcode.EncodeHintType;
import com.itextpdf.text.pdf.qrcode.ErrorCorrectionLevel;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.nio.file.Path;
import java.util.HashMap;

import static java.awt.Image.SCALE_FAST;

/**

  • @Description: 生成二维码

  • @Author ajin

  • @Version 1.0

  • @Tool: IntelliJ IDEA
    */
    public class GenerateQRCodeUtil {

    private static final int width = 300; //定义图片宽度
    private static final int height = 300; //定义图片高度
    private static final String format = “png”; //定义图片格式

    /**

    • @Author: ajin
    • @Description: 生成二维码到本地
      */
      public static void generateQRCode(String targetPath, String content) {
      //定义二维码的参数
      HashMap hashMap = new HashMap();
      hashMap.put(EncodeHintType.CHARACTER_SET, “utf-8”); //设置编码
      hashMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); //设置容错等级,等级越高,容量越小
      //生成二维码
      try {
      //生成矩阵
      //内容格式宽高
      BitMatrix bitMatrix = new MultiFormatWriter().encode(content,
      BarcodeFormat.QR_CODE //二维码类型
      , width, height, hashMap);
      Path file = new File(targetPath).toPath();//设置路径
      MatrixToImageWriter.writeToPath(bitMatrix, format, file);//输出图像
      } catch (Exception e) {
      e.printStackTrace();
      }
      }

    /**

    • @Author: ajin
    • @Description: 解析二维码内容
      /
      public static void parseQRCode(String rootPath) {
      try {
      /

      * MultiFormatReader 多格式读取
      * /
      MultiFormatReader formatReader = new MultiFormatReader();
      File file = new File(rootPath);
      //读取图片buffer中
      BufferedImage bufferedImage = ImageIO.read(file);
      /

      * BinaryBitmap 二进制位图
      * HybridBinarizer 混合二值化器
      * BufferedImageLuminanceSource 图像缓存区 亮度 资源
      * */
      BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(bufferedImage)));
      //定义二维码参数
      HashMap hashMap = new HashMap();
      hashMap.put(EncodeHintType.CHARACTER_SET, “utf-8”);//编码方式
      // hashMap.put(BarcodeFormat.QR_CODE, “CODE_128”);//二维码类型
      //hashMap.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);//优化精度
      //hashMap.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);//复杂模式,开启PURE_BARCODE模式
      Result result = formatReader.decode(binaryBitmap, hashMap);
      System.out.println(“二维码类型:” + result.getBarcodeFormat());//BarcodeFormat 条形码格式
      System.out.println(“二维码文本内容:” + result.getText());
      } catch (Exception e) {
      e.printStackTrace();
      }
      }
      }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值