java 多个二维码读取报错

 多个二维码的图片读取返回数组,在异常里处理

 public static String decode(File file) throws Exception {
        BufferedImage image;
        image = ImageIO.read(file);
        if (image == null) {
            return null;
        }
        BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(image);
        BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
        Result result=null;
        Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>();
        hints.put(DecodeHintType.CHARACTER_SET, CHARSET);
        //优化精度
        hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
        //复杂模式,开启PURE_BARCODE模式
        hints.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);
        try{
            result = new MultiFormatReader().decode(bitmap, hints);
        }catch(Exception e){
            e.printStackTrace();
            QRCodeMultiReader qc = new QRCodeMultiReader();//一张图片有多张二维码取最后一个
            Result[] r = qc.decodeMultiple(bitmap, hints);
            if(r!=null && r.length>0){
                String resultStrTemp = r[r.length-1].getText();
                return resultStrTemp;
            }
        }
        String resultStr = result.getText();
        return resultStr;
    }

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,那么首先你需要使用一个 Java 二维码库来生成和解析二维码。其中最常用的二维码库有 Zxing 和 QRGen。你可以使用 Maven 或者 Gradle 来管理这些库的依赖。 首先,你需要将库的依赖添加到你的项目中。如果使用 Maven,可以在 pom.xml 文件中添加如下内容: ``` <dependency> <groupId>com.google.zxing</groupId> <artifactId>core</artifactId> <version>3.3.3</version> </dependency> ``` 如果使用 Gradle,可以在 build.gradle 文件中添加如下内容: ``` implementation 'com.google.zxing:core:3.3.3' ``` 然后,你可以使用以下代码来生成二维码: ``` import com.google.zxing.BarcodeFormat; import com.google.zxing.WriterException; import com.google.zxing.client.j2se.MatrixToImageWriter; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.QRCodeWriter; import java.io.IOException; import java.nio.file.FileSystems; import java.nio.file.Path; public class GenerateQRCode { private static final String QR_CODE_IMAGE_PATH = "./MyQRCode.png"; public static void generateQRCodeImage(String text, int width, int height, String filePath) throws WriterException, IOException { QRCodeWriter qrCodeWriter = new QRCodeWriter(); BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height); Path path = FileSystems.getDefault().getPath(filePath); MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path); } public static void main(String[] args) { try { generateQRCodeImage("This is my first QR Code", 350, 350, QR_CODE_IMAGE_PATH); } catch (WriterException e) { System.out.println("Could not generate QR Code, WriterException :: " + e.getMessage()); } catch (IOException e) { System.out.println("

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值