admin管理员组文章数量:1431763
I am using ZXing library to decode QR codes and barcodes in a HTML / JS environment. The following code returns a valid response with any QR code, but produces an error with regular UPC-A (or any other) barcodes.
Code is based on
.html
HTML to to load the image into
<div class="imageContainer">
<img src="">
</div>
Load libraries and JS logic
"@zxing/library": "^0.18.6"
<script src=".5.1/jquery.min.js"></script>
<script type="text/javascript" src="./node_modules/@zxing/library/umd/index.min.js"></script>
<script type="text/javascript">
//initialize the reader
const codeReader = new ZXing.BrowserMultiFormatReader();
console.log('ZXing code reader initialized');
//set, load, and send image to decoder
var img = $('.imageContainer img')[0];
img.onload = function () {
console.info("Image loaded !");
$(document).ready(function(){
decodeBarcode(img);
});
};
img.onerror = function () {
console.error("Cannot load image");
//do something else...
};
img.src = "img/test.jpg";
//decode the image
function decodeBarcode(img) {
codeReader.decodeFromImage(img).then((result) => {
console.log(result)
})
.catch((err) => {
console.log(err)
})
console.log(`Started decode for image from ${img.src}`);
}
</script>
Expected result for any valid barcode, but currently only works for QR codes
{
"text": "Hello :)",
"rawBytes": {
},
"numBits": 152,
"resultPoints": [
],
"format": 11,
"timestamp": 1636657881106,
"resultMetadata": {}
}
Actual result for UPC-A barcodes (or any barcode that is not a QR code)
R: No MultiFormat Readers were able to detect the code.
at ir.decodeInternal (/@zxing/library/umd/index.min.js:15:252678)
at ir.decodeWithState (/@zxing/library/umd/index.min.js:15:251730)
at t.BrowserMultiFormatReader.decodeBitmap (/@zxing/library/umd/index.min.js:15:288702)
at t.BrowserMultiFormatReader.decode (/@zxing/library/umd/index.min.js:15:34909)
at n (/@zxing/library/umd/index.min.js:15:34314)
at /@zxing/library/umd/index.min.js:15:34495
at new Promise (<anonymous>)
at t.BrowserMultiFormatReader.decodeOnce (/@zxing/library/umd/index.min.js:15:34475)
at t.BrowserMultiFormatReader.decodeFromImageElement (/@zxing/library/umd/index.min.js:15:32072)
at t.BrowserMultiFormatReader.decodeFromImage (/@zxing/library/umd/index.min.js:15:31441)
What's interesting is the same UPC-A image decodes successfully at .jspx
I have tried countless barcode variations, all of which decode successfully at the ZXing website. Regardless of the barcode, if it isn't a QR code it produces the error above. Am I missing something?
How can I use ZXing JS library to successfully decode a standard UPC-A barcode?
I am using ZXing library to decode QR codes and barcodes in a HTML / JS environment. The following code returns a valid response with any QR code, but produces an error with regular UPC-A (or any other) barcodes.
Code is based on
https://github./zxing-js/library/blob/master/docs/examples/multi-image/index.html
HTML to to load the image into
<div class="imageContainer">
<img src="">
</div>
Load libraries and JS logic
"@zxing/library": "^0.18.6"
https://github./zxing-js/library
<script src="https://ajax.googleapis./ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script type="text/javascript" src="./node_modules/@zxing/library/umd/index.min.js"></script>
<script type="text/javascript">
//initialize the reader
const codeReader = new ZXing.BrowserMultiFormatReader();
console.log('ZXing code reader initialized');
//set, load, and send image to decoder
var img = $('.imageContainer img')[0];
img.onload = function () {
console.info("Image loaded !");
$(document).ready(function(){
decodeBarcode(img);
});
};
img.onerror = function () {
console.error("Cannot load image");
//do something else...
};
img.src = "img/test.jpg";
//decode the image
function decodeBarcode(img) {
codeReader.decodeFromImage(img).then((result) => {
console.log(result)
})
.catch((err) => {
console.log(err)
})
console.log(`Started decode for image from ${img.src}`);
}
</script>
Expected result for any valid barcode, but currently only works for QR codes
{
"text": "Hello :)",
"rawBytes": {
},
"numBits": 152,
"resultPoints": [
],
"format": 11,
"timestamp": 1636657881106,
"resultMetadata": {}
}
Actual result for UPC-A barcodes (or any barcode that is not a QR code)
R: No MultiFormat Readers were able to detect the code.
at ir.decodeInternal (https://www.myserver./node_modules/@zxing/library/umd/index.min.js:15:252678)
at ir.decodeWithState (https://www.myserver./node_modules/@zxing/library/umd/index.min.js:15:251730)
at t.BrowserMultiFormatReader.decodeBitmap (https://www.myserver./node_modules/@zxing/library/umd/index.min.js:15:288702)
at t.BrowserMultiFormatReader.decode (https://www.myserver./node_modules/@zxing/library/umd/index.min.js:15:34909)
at n (https://www.myserver./node_modules/@zxing/library/umd/index.min.js:15:34314)
at https://www.myserver./node_modules/@zxing/library/umd/index.min.js:15:34495
at new Promise (<anonymous>)
at t.BrowserMultiFormatReader.decodeOnce (https://www.myserver./node_modules/@zxing/library/umd/index.min.js:15:34475)
at t.BrowserMultiFormatReader.decodeFromImageElement (https://www.myserver./node_modules/@zxing/library/umd/index.min.js:15:32072)
at t.BrowserMultiFormatReader.decodeFromImage (https://www.myserver./node_modules/@zxing/library/umd/index.min.js:15:31441)
What's interesting is the same UPC-A image decodes successfully at https://zxing/w/decode.jspx
I have tried countless barcode variations, all of which decode successfully at the ZXing website. Regardless of the barcode, if it isn't a QR code it produces the error above. Am I missing something?
How can I use ZXing JS library to successfully decode a standard UPC-A barcode?
2 Answers
Reset to default 2As UPC-A is not enabled by default, you've to provide it as hints
for the BrowserMultiFormatReader
const hints = new Map();
const enabledFormats = [
// ...ALL_FORMATS_WHICH_YOU_WANT_TO_ENABLE
ZXing.BarcodeFormat.UPC_A,
];
hints.set(ZXing.DecodeHintType.POSSIBLE_FORMATS, enabledFormats);
const codeReader = new ZXing.BrowserMultiFormatReader(hints);
Available BarcodeFormats can be found here.
processFileImg(e) {
this.$el.innerHTML += `<img id="image" src="${e.target.result}"/>`;
const img = document.getElementById('image');
img.videoWidth = 0;
this.codeReader.decodeFromImage(img).then((result) => {
this.$emit("decode", result.text);
this.$emit("result", result);
}).catch((err) => {
this.$emit("error", err);
})
}
Exactly worked for me!
本文标签: javascriptZXing Library will decode QR code but not barcodeStack Overflow
版权声明:本文标题:javascript - ZXing Library will decode QR code but not barcode - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745583289a2664747.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论