報錯 `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() 可獲取定位點座標,繪製邊框
}
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 前置預處理。