栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java生成二维码的实例代码

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java生成二维码的实例代码

使用开源的一维/二维码图形处理库zxing GitHub地址

引入依赖



  com.google.zxing
  core
  3.3.0



  com.google.zxing
  javase
  3.3.0

封装工具类

package com.app.utils;
 
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
 
import javax.imageio.ImageIO;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.client.j2se.MatrixToImageConfig;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
 

public class QRCodeUtil
{
  private static final int width = 200; // 图像宽度
  
  private static final int height = 200; // 图像高度
  
  private static final int ON_COLOR = 0xFF000001;
  
  private static final int OFF_COLOR = 0xFFFFFFFF;
  
  
  public static boolean generateQRImage(Integer width, Integer height, String content, String logoPath, String imgPath, String imgName, String suffix)
  {
    if (content == null || imgName == null || suffix == null)
    {
      return false;
    }
    try
    {
      width = width == null ? QRCodeUtil.width : width;
      height = height == null ? QRCodeUtil.height : height;
      if (logoPath != null && !"".equals(logoPath.trim()))
      {
 QREncode(width, height, content, logoPath, imgPath, imgName, suffix);
      }
      else
      {
 QREncode(width, height, content, imgPath, imgName, suffix);
      }
      return true;
    }
    catch (Exception e)
    {
      e.printStackTrace();
      return false;
    }
  }
  
  
  private static void QREncode(int width, int height, String content, String imgPath, String imgName, String suffix)
    throws Exception
  {
    File filePath = new File(imgPath);
    if (!filePath.exists())
    {
      filePath.mkdirs();
    }
    File imageFile = new File(imgPath, imgName);
    Map hints = new HashMap<>();
    // 内容编码格式
    hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
    // 指定纠错等级
    hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
    // 设置二维码边的空度,非负数
    hints.put(EncodeHintType.MARGIN, 1);
    BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
    MatrixToImageWriter.writeToPath(bitMatrix, suffix, imageFile.toPath());// 输出原图片
  }
  
  
  private static void QREncode(int width, int height, String content, String logoPath, String imgPath, String imgName, String suffix)
    throws Exception
  {
    File filePath = new File(imgPath);
    if (!filePath.exists())
    {
      filePath.mkdirs();
    }
    File imageFile = new File(imgPath, imgName);
    Map hints = new HashMap<>();
    // 内容编码格式
    hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
    // 指定纠错等级
    hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
    // 设置二维码边的空度,非负数
    hints.put(EncodeHintType.MARGIN, 1);
    BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
    MatrixToImageConfig matrixToImageConfig = new MatrixToImageConfig(ON_COLOR, OFF_COLOR);
    BufferedImage bufferedImage = LogoMatrix(MatrixToImageWriter.toBufferedImage(bitMatrix, matrixToImageConfig), new File(logoPath));
    ImageIO.write(bufferedImage, suffix, imageFile);// 输出带logo图片
  }
  
  
  private static BufferedImage LogoMatrix(BufferedImage matrixImage, File logoFile)
    throws IOException
  {
    // 读取二维码图片,并构建绘图对象
    Graphics2D gs = matrixImage.createGraphics();
    int matrixWidth = matrixImage.getWidth();
    int matrixHeigh = matrixImage.getHeight();
    int ratioWidth = matrixWidth * 2 / 10;
    int ratioHeight = matrixHeigh * 2 / 10;
    // 读取Logo图片
    BufferedImage logo = ImageIO.read(logoFile);
    int logoWidth = logo.getWidth(null) > ratioWidth ? ratioWidth : logo.getWidth(null);
    int logoHeight = logo.getHeight(null) > ratioHeight ? ratioHeight : logo.getHeight(null);
    int x = (matrixWidth - logoWidth) / 2;
    int y = (matrixHeigh - logoHeight) / 2;
    
    // 绘制
    gs.drawImage(logo, x, y, logoWidth, logoHeight, null);
    gs.setColor(Color.BLACK);
    gs.setBackground(Color.WHITE);
 
    gs.dispose();
    matrixImage.flush();
    return matrixImage;
  }
}

测试生成二维码

QRCodeUtil.generateQRImage(null, null, "https://blog.csdn.net/qq_34928194", null, "E:/", "test.gif", "gif");

以上就是Java生成二维码的实例代码的详细内容,更多关于Java生成二维码的资料请关注考高分网其它相关文章!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/131933.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号