• 使用Java代码制作二维码(超级简单)


    操作步骤

    真的是灰常简单,导入三个jar包,十几行代码就搞定了。
    源代码和jar包已经打包放在阿里云盘里面,文末有链接

    import com.google.zxing.BarcodeFormat;
    import com.google.zxing.EncodeHintType;
    import com.google.zxing.MultiFormatWriter;
    import com.google.zxing.client.j2se.MatrixToImageWriter;
    import com.google.zxing.common.BitMatrix;
    import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
    
    import java.nio.file.FileSystems;
    import java.nio.file.Path;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.UUID;
    
    /**
     * @author: 邹祥发
     * @date: 2022/8/20 16:29
     * 制作二维码
     */
    public class Code {
        public boolean CreateCode(String content, String path) throws Exception {
            int width = 300;
            int height = 300;
            String format = "jpg";
            Map<EncodeHintType, Object> map = new HashMap<>();
            map.put(EncodeHintType.CHARACTER_SET, "utf-8");
            //容错率等级L>M>Q>H,等级越高,所需要的扫描时间越长,但是准确率越高
            map.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
            map.put(EncodeHintType.MARGIN, 2);
            BitMatrix encode = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, map);
            Path path1 = FileSystems.getDefault().getPath(path);
            MatrixToImageWriter.writeToPath(encode, format, path1);
            return true;
        }
    
        public static void main(String[] args) throws Exception {
            //创建一个不会重复的UUID
            String uuid = UUID.randomUUID().toString();
            //去掉 - 并截取前8位
            String name = uuid.replace("-", "").substring(0, 8);
            //二维码存放的路径
            String path = "D:\\code\\" + name + ".jpg";
            //使用反射代替new关键字
            Class<?> code = Class.forName("Code");
            Code o = (Code) code.getDeclaredConstructor().newInstance();
            boolean b = o.CreateCode("https://www.hqxiaozou.top", path);
            if (b) {
                System.out.println("二维码创建成功!");
                System.out.println(path);
            } else {
                System.out.println("失败!");
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53

    注意:需要修改 boolean b = new Code().CreateCode("https://www.hqxiaozou.top", path);
    中的代码,第一个参数改成你需要跳转的网页,或者写一段文字也可以。后面一个参数是二维码生成后保存的路径。

    相关资料

    jar包链接https://www.aliyundrive.com/s/fMDeMzXTFj2

  • 相关阅读:
    HarmonyOS ArkTSTabs组件的使用(六)
    2022最新RTMP+HTTP直播地址汇总(亲测可用)
    北邮22级信通院数电:Verilog-FPGA(6)第六周实验:全加器(关注我的uu们加群咯~)
    lvgl 画好一个圆弧arc 要了解的相关知识
    每日10行代码175:按条件取出且仅取出一条记录
    最新免费毕业论文下载java+ssm
    C++ DAY08 异常
    网页转长图插件html2canvas【前端】
    解决【无法处理文件xxx,因为它位于 Internet 或受限区域中,或者文件上具有 Web 标记。要想处理这些文件,请删除 Web 标记】问题
    Axios在vue项目中的基本用法
  • 原文地址:https://blog.csdn.net/Zou_05/article/details/126441780