BitMatrix 生普通二维码,加logo二维码,和二维码底部加文字得二维码 com.google.zxing,并且以流返回给前端,和byte方式返回前端

本文介绍了如何利用BitMatrix在Java中生成普通二维码、加Logo的二维码以及二维码底部添加文字的方法。参考了相关博客,实现了接口功能,包括以流和Byte形式返回给前端。
摘要由CSDN通过智能技术生成

BitMatrix 生普通二维码,加logo二维码,和二维码底部加文字得二维码 com.google.zxing

因开发需求,并且搜了很多博客无果,参考了几篇博客后,终于做出来了,在此记录一下,注:带logo的二维码按照底部文字使用,这里没有这个需求,就没做

上代码

package com.fc.test.util;


import com.fc.test.test.erweima.QRCodeUtil;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

import java.awt.*;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;

/**
 *  <p>
 *      生成二维码的工具类
 *  </p>
 *
 * @author robocon
 * @date 2019-05-09
 * */
public class QRCodeUtils {
   

    /**
     *  生成二维码
     * @param content 二维码的内容
     * @return BitMatrix对象
     * */
    public static BitMatrix createCode(Map<String, String> content) throws IOException {
   
        //二维码的宽高
        int width = 400;
        int height = 400;

        //其他参数,如字符集编码
        Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        //容错级别为H
        hints.put(EncodeHintType.ERROR_CORRECTION , ErrorCorrectionLevel.H);
        //白边的宽度,可取0~4
        hints.put(EncodeHintType.MARGIN , 3);

        BitMatrix bitMatrix = null;
        try {
   
            //生成矩阵,
            bitMatrix = new MultiFormatWriter().encode(String.valueOf(content),
                    BarcodeFormat.QR_CODE, width, height, hints);

            //bitMatrix = deleteWhite(bitMatrix);
        } catch (WriterException e) {
   
            e.printStackTrace();
        }

        return bitMatrix;
    }

    /**
     * 创建 二维码所需图片
     * @param content 内容
     * @return
     * @throws Exception
     */
    public static BufferedImage createImage(Map<String, String> content, String bottomDes) throws Exception {
   
        Hashtable hints = new Hashtable();
        int width = 400;
        int height = 400;
        //容错级别 H->30%
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");</
  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
您可以使用com.google.zxing二维码。下面是一个使用Java代码二维码的示例: ```java import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.MultiFormatWriter; import com.google.zxing.WriterException; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.QRCodeWriter; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.util.HashMap; import java.util.Map; public class QRCodeGenerator { private static final int QR_CODE_SIZE = 300; public static void generateQRCode(String text, String filePath) throws WriterException, IOException { Map<EncodeHintType, Object> hints = new HashMap<>(); hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); QRCodeWriter qrCodeWriter = new QRCodeWriter(); BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, QR_CODE_SIZE, QR_CODE_SIZE, hints); BufferedImage qrImage = new BufferedImage(QR_CODE_SIZE, QR_CODE_SIZE, BufferedImage.TYPE_INT_RGB); qrImage.createGraphics(); Graphics2D graphics = (Graphics2D) qrImage.getGraphics(); graphics.setColor(Color.WHITE); graphics.fillRect(0, 0, QR_CODE_SIZE, QR_CODE_SIZE); graphics.setColor(Color.BLACK); for (int i = 0; i < QR_CODE_SIZE; i++) { for (int j = 0; j < QR_CODE_SIZE; j++) { if (bitMatrix.get(i, j)) { graphics.fillRect(i, j, 1, 1); } } } ImageIO.write(qrImage, "png", new File(filePath)); } public static void main(String[] args) { String text = "https://www.google.com"; String filePath = "qrcode.png";
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值