ZXing使用踩坑记录

ZXing使用踩坑记录

在Android开发中使用zxing时反色的二维码无法识别

我们一般扫码的时候都是这样的二维码 白色的背景 黑色的码点 使用呢也是没有问题的
图一:
偶然的发现在实际运用过程中遇到了这种反转色的二维码(黑色背景 白色码点的二维码)
在这里插入图片描述
识别不出来 一开始很纳闷 以为是分辨率的问题 发现同样的手机 微信可以识别 我的程序却不行 找了半天终于找到了问题

如何解决呢?

在识别处理的方法内可以找到GlobalHistogramBinarizer(source)方法或者HybridBinarizer(source)

// 一开始我的写法是这样的

rawResult = mMultiFormatReader.decodeWithState(new BinaryBitmap(new GlobalHistogramBinarizer(source)));

// 只需要将原来的source添加反转即可

rawResult = mMultiFormatReader.decodeWithState(new BinaryBitmap(new GlobalHistogramBinarizer(source.invert())));

//为了能够让我们识别的时候不仅可以识别反色二维码 正常的二维吗也要可以识别我的完整代码如下

try {
                if (System.currentTimeMillis() % 1000 < 500) {
                    rawResult = mMultiFormatReader.decodeWithState(new BinaryBitmap(new GlobalHistogramBinarizer(source.invert())));
                }else {
                    rawResult = mMultiFormatReader.decodeWithState(new BinaryBitmap(new GlobalHistogramBinarizer(source)));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (rawResult == null) {
                if (System.currentTimeMillis() % 1000 < 500) {
                    rawResult = mMultiFormatReader.decodeWithState(new BinaryBitmap(new HybridBinarizer(source.invert())));
                }else {
                    rawResult = mMultiFormatReader.decodeWithState(new BinaryBitmap(new HybridBinarizer(source)));
                }
                if (rawResult != null) {
                    BGAQRCodeUtil.d("GlobalHistogramBinarizer 没识别到,HybridBinarizer 能识别到");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

修改 详情参考zxing中 DecodeHandler.java

https://github.com/zxing/zxing/blob/master/android/src/com/google/zxing/client/android/DecodeHandler.java

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
您好!要使用ZXing和Spring Boot进行二维码的生成和解码,您可以按照以下步骤进行操作: 1. 首先,确保您的Spring Boot项目已经设置好,并且您已经添加了所需的依赖项。您可以在您的pom.xml文件中添加以下依赖项: ```xml <dependency> <groupId>com.google.zxing</groupId> <artifactId>core</artifactId> <version>3.4.1</version> </dependency> <dependency> <groupId>com.google.zxing</groupId> <artifactId>javase</artifactId> <version>3.4.1</version> </dependency> ``` 2. 在Spring Boot中创建一个Controller或Service类,用于处理二维码生成和解码的请求。您可以尝试以下示例代码: ```java import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.MultiFormatWriter; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; import javax.imageio.ImageIO; import javax.servlet.http.HttpServletResponse; import java.awt.image.BufferedImage; import java.io.IOException; import java.io.OutputStream; import java.util.HashMap; import java.util.Map; @RestController public class QRCodeController { @GetMapping("/qrcode/{content}") public void generateQRCode(@PathVariable String content, HttpServletResponse response) throws IOException { int width = 300; int height = 300; String format = "png"; Map<EncodeHintType, Object> hints = new HashMap<>(); hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); hints.put(EncodeHintType.MARGIN, 1); BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints); 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, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF); } } OutputStream outputStream = response.getOutputStream(); ImageIO.write(image, format, outputStream); outputStream.flush(); outputStream.close(); } } ``` 此示例代码中,我们创建了一个`generateQRCode`方法,该方法接受一个内容参数,并生成对应的二维码图像。这个方法使用ZXing库来生成二维码,并将图像写入HttpServletResponse的输出流中。 3. 运行您的Spring Boot应用程序,并在浏览器中访问`http://localhost:8080/qrcode/{content}`,其中`{content}`是您想要生成二维码的内容。例如,您可以尝试访问`http://localhost:8080/qrcode/Hello%20World`来生成包含"Hello World"的二维码。 这只是一个简单的示例,您可以根据您的需求进行更多的定制和扩展。希望对您有帮助!如有任何问题,请随时提问。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值