Bug: Not working with inline image URL
See original GitHub issueHey,
I am trying to use this library to decode a QR code rendered on canvas. To achieve that, I use canvas.toDataURL()
to encode the QR code in the image URL. This gives us something like this.
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAASwAAACWCAYAAABkW7XSAAAM1UlEQVR4Xu2c67obqQ5EM+//0Gc+J7PHnj42VQVSX9wrfwEhLaA2UuP89evXr//92vffXy/TOXO/9h956tgajd/OM2Pvx4YzdhSXM75q1Vy+VfNhBwLTBB6bdc/D8XAUwfqzXAjW9LZl4F0JIFjPleeGdddTQNyXIYBgIVikhJc5rjj6TrAqN/C7dHOUEjq3HNc/J9V1bamd4sy1Wj/r8rXKrmJEOwSWCSBYywh/G0CwajhiBQJDAghWzQZBsGo4YgUCCNaAQFU6hGBx0CCwAwHnhjVzGEfvkbpqWCt+fkrr1BOM5N1VZQ0rjTVhvsO2YwoIzBE4WrCU16po/zo+6ftuXjVetSe+bG2lHxsQLLVzaP9KAgjWc1mVIKl2BOsrjwhBnYkAgoVgVdXxzrSv8eVLCRwtWGlqU70Mqkal5vt02NO4SAkVadoh8M/v2WbqKQrebNH9nd1VUXGL3anIPOyuCNboZqPSz9RXiu5qx9J+CQJXuGEhWH+20goHBOsSxxEnFQEEa1zDcvi96+PcgLhhKbq0Q2BD4O6CVb0hkndZCFY1fex9PYGrCdZ2QdRNZiWNGtWoHm2jOpOqQamNpcaruLf2SQkVcdovQQDBGi/T7C1ICY7aHGo8gqUI0v6VBK4gWCvgK29YqUgov2fF8NPtbjQfNyy1GrRfggCC5d+wEKxLbGmc/GYCjmCtxJ+mNs4DSuWP+3JbvT9z6mVJkX1UV0rmUvGr9jRuZY92COxGAMF6olZClxbZ1SKupITK9qgdwVqhx9hDCbwTrG6HknrKTBo2uvV0zz3LruJmWTX3rB3GQaCdwBUFSxXSOwXryLm7NoO6WXbNi10IxAQQrM8pYVp/e1jqFMt4cc0BCJYJim7HE0CwECwE6/hziAcmAQTLBFXQ7Sr1s4JQMQGBHgJX/+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0EECwWrBiFAIQ6CCAYHVQxSYEINBCAMFqwYpRCECggwCC1UEVmxCAQAsBBKsFK0YhAIEOAghWB1VsQgACLQQQrBasGIUABDoIIFgdVLEJAQi0EECwWrBiFAIQ6CCAYHVQxSYEINBCAMFqwYpRCECggwCC1UEVmxCAQAsBBKsFK0YhAIEOAghWB1VsQgACLQT+Bl9BJ9Om8CmyAAAAAElFTkSuQmCC
We then render the image on page like so.
const img = new Image();
img.height = canvas.height;
img.src = canvas.toDataURL();
img.width = canvas.width;
document.body.appendChild(img);
Finally, we try to decode our QR code.
const reader = new ZXing.BrowserQRCodeReader();
reader.decodeFromImage(img);
Unfortunately, this operation fails with the following error message.
Error
at FinderPatternFinder.webpackJsonp../node_modules/@zxing/library/esm5/core/qrcode/detector/FinderPatternFinder.js.FinderPatternFinder.selectBestPatterns (FinderPatternFinder.js:546)
at FinderPatternFinder.webpackJsonp../node_modules/@zxing/library/esm5/core/qrcode/detector/FinderPatternFinder.js.FinderPatternFinder.find (FinderPatternFinder.js:165)
at Detector.webpackJsonp../node_modules/@zxing/library/esm5/core/qrcode/detector/Detector.js.Detector.detect (Detector.js:66)
at QRCodeReader.webpackJsonp../node_modules/@zxing/library/esm5/core/qrcode/QRCodeReader.js.QRCodeReader.decode (QRCodeReader.js:63)
at BrowserQRCodeReader.webpackJsonp../node_modules/@zxing/library/esm5/browser/BrowserCodeReader.js.BrowserCodeReader.readerDecode (BrowserCodeReader.js:254)
at BrowserQRCodeReader.webpackJsonp../node_modules/@zxing/library/esm5/browser/BrowserCodeReader.js.BrowserCodeReader.decodeOnce (BrowserCodeReader.js:229)
at BrowserCodeReader.js:180
at new Promise (<anonymous>)
at BrowserQRCodeReader.webpackJsonp../node_modules/@zxing/library/esm5/browser/BrowserCodeReader.js.BrowserCodeReader.decodeFromImage (BrowserCodeReader.js:171)
at index.js:100
If I try to screenshot the rendered <img />
, save it to my computer as screenshot.jpg
and modify my code as follows…
img.src = 'C:\fakepath\screenshot.jpg'
…we get back this.
{
format: 11,
numBits: 288,
rawBytes: Uint8Array(36) [65, 183, 55, 87, 6, 87, 39, 54, 86, 55, 87, 38, 86, 182, 87, 151, 7, 38, 247,
70, 86, 55, 70, 86, 70, 86, 70, 151, 64, 236, 17, 236, 17, 236, 17, 236],
resultMetadata: Map(2) {2 => Array(1), 3 => "H"},
resultPoints: (4) [FinderPattern, FinderPattern, FinderPattern, AlignmentPattern],
text: "supersecurekeyprotectededit",
timestamp: 1533052481452
}
Yay, it works! However, this is not a desired behaviour as we cannot expect the user to download a picture of the canvas on their computer and then upload it again.
Any suggestions how to fix this? I am sure there is a way to achieve what I am trying to do since scanning input from device camera is essentially doing the same thing. Thanks!
Issue Analytics
- State:
- Created 5 years ago
- Comments:25 (10 by maintainers)
Top GitHub Comments
Still not fixed, bot. 🥴
Is there any solution of this ? I also doing same to create Image and set src but decodeFromImage given error that “either src or a loaded img should be provided”