Zxing 生成二维码和条形码去掉白边

源码下载:

https://github.com/AndroidZmm123/ZxingApplication

需求:根据输入内容,生成条形码或者二维码。

我们大多数会选择Zxing。因为jar包较小。且使用简单。根据内容生成二维码的工具类也是一搜一大堆。上面的源码里面也提供了一个。但是我们仔细看了下。会发现。不管生成的是条形码还是二维码都会有一部分的白边。如图:

我们可以看到周围有白边。那我们怎么去掉呢。

我们先把zxing的源码下载下来。然后看下这个白边是如果产生的。

首先是二维码的。

            BitMatrix bitMatrix = new QRCodeWriter().encode(content, BarcodeFormat.QR_CODE, widthPix,
                heightPix, hints);

这是根据我们输入的内容。产生一个矩阵。我们可以看到QRCodeWriter的encode()方法里面的最后两行:

   QRCode code = Encoder.encode(contents, errorCorrectionLevel, hints);
        return renderResult(code, width, height, quietZone);

第一句是将我们输入的内容转换成code.不涉及宽高。

第二句是将code转换成矩阵。加入白边的代码就在这里面。

private static BitMatrix renderResult(QRCode code, int width, int height, int quietZone) {
        ByteMatrix input = code.getMatrix();
        if (input == null) {
            throw new IllegalStateException();
        }
        int inputWidth = input.getWidth();
        int inputHeight = input.getHeight();

        //这里就是在我们给定的宽高的基础上。增加了大小。
        //int qrWidth = inputWidth + (quietZone * 2);
        //int qrHeight = inputHeight + (quietZone * 2);

        //修改--》去掉间距
        int qrWidth = inputWidth;
        int qrHeight = inputHeight;

        int outputWidth = Math.max(width, qrWidth);
        int outputHeight = Math.max(height, qrHeight);

        int multiple = Math.min(outputWidth / qrWidth, outputHeight / qrHeight);
        // Padding includes both the quiet zone and the extra white pixels to accommodate the requested
        // dimensions. For example, if input is 25x25 the QR will be 33x33 including the quiet zone.
        // If the requested size is 200x160, the multiple will be 4, for a QR of 132x132. These will
     
  • 4
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
zxing是一款非常优秀的开源库,可以帮助我们快速、方便地生成二维码。在使用zxing生成二维码时,我们需要先准备好相关的依赖和jar包,并使用QRCodeWriter类来生成普通的二维码。具体的生成过程如下: 1. 首先,我们需要下载zxing的jar包,并将其导入到项目中。 2. 在生成二维码的代码中,我们需要创建一个QRCodeWriter对象,用于生成二维码。然后,我们需要准备一些参数,如二维码的内容、宽度、高度等。这些参数会被传递给QRCodeWriter对象的encode方法。 3. 在生成二维码之前,我们可以设置一些可选的参数,如编码类型、字符集等。可以使用Hashtable对象来存储这些参数。 4. 调用QRCodeWriter对象的encode方法,传入参数,即可生成一个BitMatrix对象,它表示了一个二维码的矩阵。 5. 最后,我们可以将BitMatrix对象转换为图片,并保存到指定的文件路径中。 以上就是使用zxing生成二维码的基本步骤。如果需要生成带有Logo的二维码,我们可以使用MatrixToImageConfig类来实现。无论是生成普通的二维码还是带有Logo的二维码,zxing都是一个非常实用的工具。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [使用zxing生成二维码](https://blog.csdn.net/qinshengfei/article/details/131142998)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *3* [二维码的知识 看这一篇就够了 使用zxing进行二维码的生成](https://blog.csdn.net/q15976405716/article/details/107402967)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值