Skip to content

Commit 8170bb1

Browse files
committed
fix: scanning high resolution frames fails
jsQR seems to not decode frames with a certain high resolution. Scaling frames down to 1920x1080 resolves this issue and might also improve scanning speed.
1 parent 046b3d3 commit 8170bb1

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

src/misc/image-data.js

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,31 @@ import { hasFired } from './promisify.js'
44
const canvas = document.createElement('canvas')
55
const canvasCtx = canvas.getContext('2d')
66

7-
export function imageDataFromImage (imageElement) {
8-
canvas.width = imageElement.naturalWidth
9-
canvas.height = imageElement.naturalHeight
7+
canvas.width = 1920
8+
canvas.height = 1080
109

11-
const bounds = [0, 0, canvas.width, canvas.height]
10+
function imageDataFromCanvas (canvasImageSource, width, height) {
11+
const scalingRatio = Math.min(1, canvas.width / width, canvas.height / height)
12+
const widthScaled = scalingRatio * width
13+
const heightScaled = scalingRatio * height
1214

13-
canvasCtx.drawImage(imageElement, ...bounds)
15+
canvasCtx.drawImage(canvasImageSource, 0, 0, widthScaled, heightScaled)
1416

15-
return canvasCtx.getImageData(...bounds)
17+
return canvasCtx.getImageData(0, 0, widthScaled, heightScaled)
1618
}
1719

18-
export function imageDataFromVideo (videoElement) {
19-
canvas.width = videoElement.videoWidth
20-
canvas.height = videoElement.videoHeight
20+
export function imageDataFromImage (imageElement) {
21+
const width = imageElement.naturalWidth
22+
const height = imageElement.naturalHeight
2123

22-
const bounds = [0, 0, canvas.width, canvas.height]
24+
return imageDataFromCanvas(imageElement, width, height)
25+
}
2326

24-
canvasCtx.drawImage(videoElement, ...bounds)
27+
export function imageDataFromVideo (videoElement) {
28+
const width = videoElement.videoWidth
29+
const height = videoElement.videoHeight
2530

26-
return canvasCtx.getImageData(...bounds)
31+
return imageDataFromCanvas(videoElement, width, height)
2732
}
2833

2934
export async function imageDataFromUrl (url) {

0 commit comments

Comments
 (0)