java学习-zxing生成二维码矩阵的简单例子 关于JAVA实现二维码以及添加二维码LOGO

这个例子需要使用google的开源项目zxing的核心jar包

core-3.2.0.jar

可以百度搜索下载jar文件

也可使用maven添加依赖

        <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>core</artifactId>
            <version>3.2.0</version>
        </dependency>

最简单的生成二维码的方法,

   /**
     * 生成二维码图片
     * @param dir        存放的目录
     * @param fileName  文件名要以.jpg结尾
     * @param content  这个内容可以是文字或链接
     */
    public void generateQRCode(String dir, String fileName, String content) {
        //生成二维码的宽高
        int size = 200;
        Map<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
        // 指定纠错等级
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
        // 指定编码格式
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");

        try {
            //encode(String contents, BarcodeFormat format, int width, int height, Map<EncodeHintType, ?> hints)
            BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, size, size, hints);

            bitMatrix = updateBit(bitMatrix, 8);
            File file1 = new File(dir);
            if (!file1.exists()) {
                file1.mkdirs();
            }

            //将生成的矩阵像素写入到指定文件中,这里是以jpg结尾
            MatrixToImageWriter.writeToStream(bitMatrix, "jpg", new FileOutputStream(dir + "/"+fileName));
            System.out.println("创建成功");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (WriterException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

这里生成的二维码留的白色边距有点多,想要适当减小边距,看下面

如果不想边距太大,我们可以取得二维码的真是宽高,然后新建立一个空的BitMatrix对象来放这个二维码即可

margin为白色边距的大小

private static BitMatrix updateBit(BitMatrix matrix, int margin) {

        int tempM = margin * 2;

        //left,top,width,height 
        // 0    1    2     3   对应的数组下标
        //这里的width和height是指去除白色边框后的真实的二维码长宽,而不是图片长宽。
        int[] rec = matrix.getEnclosingRectangle(); // 获取二维码图案的属性

        int resWidth = rec[2] + tempM;//真实宽度加左右边距
        int resHeight = rec[3] + tempM;

        BitMatrix resMatrix = new BitMatrix(resWidth, resHeight); // 按照自定义边框生成新的BitMatrix

        resMatrix.clear();

        for (int i = margin; i < resWidth - margin; i++) { // 循环,将二维码图案绘制到新的bitMatrix中

            for (int j = margin; j < resHeight - margin; j++) {

                if (matrix.get(i - margin + rec[0], j - margin + rec[1])) {

                    resMatrix.set(i, j);

                }

            }

        }

        return resMatrix;
    }

生成二维码

扫描二维码关注公众号,回复: 2624827 查看本文章

这样白色边距就不会太大了,好看多了

后面还有将二维码嵌入到海报,或者其他活动图片上的方法,直接上代码

public void insertQRCode(BufferedImage zxingImage, String backgroundPath) {
        InputStream dest = null;

        try {
            dest = new FileInputStream(backgroundPath);
            BufferedImage image = ImageIO.read(dest);

            Graphics g = image.getGraphics();

            int leftMargin = image.getWidth() - zxingImage.getWidth() - 10;
            int topMargin = image.getHeight() - zxingImage.getHeight() - 10;
            g.drawImage(zxingImage, leftMargin, topMargin, zxingImage.getWidth(), zxingImage.getHeight(), null);
            ImageIO.write(image, "jpg", new FileOutputStream("D:\\QRCode\\zengmei.jpg"));
            System.out.println("创建成功");
            
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

还有修改二维码线条颜色,在二维码中插入logo图标等方法,下次继续补充。

谢谢浏览

 参考链接:

关于JAVA实现二维码以及添加二维码LOGO

https://www.cnblogs.com/qwqwQAQ/p/8118109.html

JAVA实现基于ZXing的二维码自动生成与图片合成

https://blog.csdn.net/mruso/article/details/79744670

Java通过Zxing生成二维码

http://blog.51cto.com/9732420/1742136

开源项目地址

https://github.com/zxing/zxing

猜你喜欢

转载自www.cnblogs.com/gne-hwz/p/9445466.html