Skip to content

Commit

Permalink
refactor: call capture() with mode
Browse files Browse the repository at this point in the history
  • Loading branch information
siiky committed Jan 23, 2025
1 parent 3b69fdf commit e6a6a28
Showing 1 changed file with 28 additions and 26 deletions.
54 changes: 28 additions & 26 deletions lib/scanner-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,40 +128,42 @@ const capture = (device, { mode }, returnCallback, stillsCallback, process) => {
}

try {
let processing = false
let lastStillTime = 0

activeStream = stream(device, {
fps: current_fps,
pickFormat: pickFormat(mode),
})
.map(async ({ frame, width, height }) => {
try {
return await process(frame, width, height)
} catch (err) {
console.log("Failed processing frame:", err)
return null
}
})

const externallyClosedHandler = () => {
returnCallback(null, null)
}

activeStream.on('close', externallyClosedHandler)

activeStream.on('data', async (frame) => {
if (processing) return
processing = true
const result = await process(frame)

if (result) {
activeStream.removeListener('close', externallyClosedHandler)
activeStream.destroy()
activeStream = null
returnCallback(null, result)
} else {
const now = Date.now()
if (now - lastStillTime > 1000) {
lastStillTime = now
stillsCallback(frame)
activeStream
.once('close', externallyClosedHandler)
.on('data', async (result) => {
if (result) {
activeStream
.removeListener('close', externallyClosedHandler)
.destroy()
activeStream = null
returnCallback(null, result)
} else {
const now = Date.now()
if (now - lastStillTime > 1000) {
lastStillTime = now
stillsCallback(frame)
}
}
}

processing = false
})
})
} catch (err) {
returnCallback(err, null)
}
Expand All @@ -171,7 +173,7 @@ const scanPDF417 = (resultCallback, idCardStillsCallback) => {
const mode = 'photoId'
const device = getCameraDevice(mode)

capture(device, { mode }, resultCallback, idCardStillsCallback, async frame => {
capture(device, { mode }, resultCallback, idCardStillsCallback, async (frame, width, height) => {
const bwFrame = await sharp(frame).greyscale().toBuffer()
const result = await scanner.scanPDF417(bwFrame)
return result ? Pdf417Parser.parse(result) : null
Expand All @@ -182,7 +184,7 @@ const scanQR = (resultCallback) => {
const mode = 'qr'
const device = getCameraDevice(mode)

capture(device, { mode }, resultCallback, _.noop, async frame => {
capture(device, { mode }, resultCallback, _.noop, async (frame, width, height) => {
const bwFrame = await sharp(frame).greyscale().toBuffer()
return scanner.scanQRcode(bwFrame)
}
Expand All @@ -193,7 +195,7 @@ const scanMainQR = (cryptoCode, qrStillsCallback, resultCallback) => {
const mode = 'qr'
const device = getCameraDevice(mode)

capture(device, { mode }, resultCallback, qrStillsCallback, async frame => {
capture(device, { mode }, resultCallback, qrStillsCallback, async (frame, width, height) => {
const bwFrame = await sharp(frame).greyscale().toBuffer()
const code = await scanner.scanQRcode(bwFrame)
const network = 'main'
Expand All @@ -206,7 +208,7 @@ const scanMainQR = (cryptoCode, qrStillsCallback, resultCallback) => {

const delayedPhoto = (device, config, callback) => {
const timerInit = new Date().getTime()
capture(device, config, callback, _.noop, async frame => {
capture(device, config, callback, _.noop, async (frame, width, height) => {
if (timerInit > new Date().getTime() - getDelayMS()) return null
return frame
})
Expand Down

0 comments on commit e6a6a28

Please sign in to comment.