首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> 程式設計綜合問答

ZXing 掃描圖片中不固定位置/多條碼:開啟 TryHarder、指定 PossibleFormats、用 DecodeMultiple

     編輯:程式設計綜合問答

報錯 `NotFoundException` 通常因:圖片噪點大、條碼區域小、旋轉/透視變形、多條碼干擾。ZXing 進階配置可大幅提升識別率。

核心參數

Map hints = new EnumMap<>(DecodeHintType.class);
hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);          // 更積極嘗試旋轉/縮放
hints.put(DecodeHintType.POSSIBLE_FORMATS, Arrays.asList(    // 限定格式,減少誤判
    BarcodeFormat.CODE_128, BarcodeFormat.QR_CODE, BarcodeFormat.EAN_13
));
hints.put(DecodeHintType.PURE_BARCODE, Boolean.FALSE);       // 非純條碼圖片

掃描多條碼

BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));
Result[] results = new MultiFormatReader().decodeMultiple(bitmap, hints);
for (Result r : results) {
    System.out.println("格式: " + r.getBarcodeFormat() + "  內容: " + r.getText());
    // r.getResultPoints() 可獲取定位點座標,繪製邊框
}

預處理提升識別率(OpenCV 配合)

  1. 灰度化 → 自適應二值化(`cv::adaptiveThreshold`)。
  2. 形態學閉運算連接斷開條碼線。
  3. 輪廓過濾:面積、長寬比、矩形度篩選候選區域。
  4. 透視變換矯正 → 輸出標準化子圖 → 單獨送 ZXing。

.NET 版

var reader = new BarcodeReader {
    TryHarder = true,
    PossibleFormats = new[] { BarcodeFormat.CODE_128, BarcodeFormat.QR_CODE },
    AutoRotate = true
};
var results = reader.DecodeMultiple(bitmap);
foreach (var r in results) Console.WriteLine(r.Text);

**關鍵**:`TRY_HARDER` + `POSSIBLE_FORMATS` + `DecodeMultiple` 解決 90% 場景;極端場景需 OpenCV 前置預處理。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved