二维码代码生成器

pom文件
<dependency>
   <groupId>com.google.zxing</groupId>
   <artifactId>core</artifactId>
   <version>3.3.0</version>
</dependency>
package com.cn.core.util;

import com.cn.core.common.Constant;
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.io.IOException;
import java.util.Hashtable;


/**
 * 二维码生成工具类
 * add by dhy
 */
public class ErCodeUtil {

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

    /**
     * 根据字符串生成对应的二维码图片png
     * 大小:200*200
     * <p>
     * content:要转换的内容
     * path:生成的二维码图片的绝对路径
     * filename: 生成的文件名
     */
    public static void buildQuickMark(String content, String filename) {
        try {
            String path = Constant.PIC_URL;
            Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
            hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
            hints.put(EncodeHintType.MARGIN, 1);
            BitMatrix byteMatrix = new MultiFormatWriter().encode(content,
                        BarcodeFormat.QR_CODE, 200, 200, hints);   //电子卡尺寸

            String format = "png";
            File file = new File(path + filename + "." + format);
            BufferedImage image = toBufferedImage(byteMatrix);
            if (!ImageIO.write(image, format, file)) {
                throw new IOException("Could not write an image of format " + format + " to " + file);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    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 buildQuickMarkToBaoZong(String content, String filename) {
        try {
            String path = Constant.PIC_URL;
            Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
            hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
            hints.put(EncodeHintType.MARGIN, 1);
            BitMatrix   byteMatrix = new MultiFormatWriter().encode(content,
                    BarcodeFormat.QR_CODE, 300, 300, hints); //包总的尺寸

            String format = "png";
            File file = new File(path + filename + "." + format);
            BufferedImage image = toBufferedImage(byteMatrix);
            if (!ImageIO.write(image, format, file)) {
                throw new IOException("Could not write an image of format " + format + " to " + file);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }




    public static void main(String[] args) throws Exception {
        String content = "Http://www.baidu.com";
        String filename = "test";
        buildQuickMark(content, filename);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
VBA作为解释型语言,在Office套件中被广泛应用。二维码生成器是一种较为常见的需求,VBA也可以通过调用API来实现该功能,生成QR Code。下面简单介绍一下VBA二维码生成器源码。 首先,要实现二维码的生成,需要调用API。通常使用“qrencode”库实现。我们在VBE中引入“Microsoft WinHTTP Services 5.1”项目,在代码中加入如下代码段: Public Function GetQRCode(ByVal s As String) As StdPicture Dim Pic As StdPicture Dim qrcode As String Dim req As New WinHttpRequest Dim resp As MemoryStream qrcode = "https://api.qrserver.com/v1/create-qr-code/?size=1000x1000&data=" qrcode = qrcode & s req.Open "GET", qrcode, False req.Send Set resp = New MemoryStream resp.Open resp.Write req.ResponseBody resp.SaveToFile "qr.jpg", adSaveCreateOverWrite Set Pic = LoadPicture("qr.jpg") Set GetQRCode = Pic End Function 这段代码利用API获取二维码,并返回一个标准图像(StdPicture)对象。其中,API返回一张JPEG格式的图片,我们将其保存到文件,并通过LoadPicture方法加载到标准图像对象中。这样就可以实现二维码的生成了。 需要注意的是,在编写VBA二维码生成器源码时,需要确保Office套件的已经安装WinHTTP组件,否则代码可能无法运行,需要进行手工安装。同时,在生成二维码时,API还支持更多的参数设置,包括二维码大小、错误处理等等,可以通过URL的处理来实现。这样就可以实现更加灵活的二维码生成功能。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值