-
-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for detecting and turning on/off flash light #241
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -237,6 +237,7 @@ export class Html5Qrcode { | |
private qrRegion: QrcodeRegionBounds | null = null; | ||
private context: CanvasRenderingContext2D | null = null; | ||
private lastScanImageFile: string | null = null; | ||
private flashOn: boolean; | ||
//#endregion | ||
|
||
public isScanning: boolean; | ||
|
@@ -285,7 +286,7 @@ export class Html5Qrcode { | |
this.localMediaStream; | ||
this.shouldScan = true; | ||
this.isScanning = false; | ||
|
||
this.flashOn = false; | ||
} | ||
|
||
//#region start() | ||
|
@@ -393,7 +394,14 @@ export class Html5Qrcode { | |
qrCodeErrorCallback!) | ||
.then((_) => { | ||
$this.isScanning = true; | ||
resolve(/* Void */ null); | ||
if (!$this.flashOn) { | ||
resolve(/* Void */ null); | ||
return; | ||
} | ||
// Turn on flash light if it was turned on before | ||
$this.setFlash(true).then(() => { | ||
resolve(/* Void */ null); | ||
}); | ||
}) | ||
.catch(reject); | ||
}) | ||
|
@@ -492,6 +500,7 @@ export class Html5Qrcode { | |
|
||
this.localMediaStream!.getVideoTracks().forEach((videoTrack) => { | ||
this.localMediaStream!.removeTrack(videoTrack); | ||
// This will also stop flash light | ||
videoTrack.stop(); | ||
++tracksClosed; | ||
|
||
|
@@ -502,6 +511,63 @@ export class Html5Qrcode { | |
}); | ||
} | ||
|
||
/** | ||
* Checks if current video track has flash light capability. | ||
* | ||
* This feature requires that camera is started. | ||
* | ||
* @returns Promise with detected flash capability result. | ||
*/ | ||
public hasFlash(): Promise<boolean> { | ||
if (!("ImageCapture" in window)) { | ||
return Promise.resolve(false); | ||
} | ||
|
||
const track = this.videoElement?.srcObject ? (<MediaStream>this.videoElement.srcObject).getVideoTracks()[0] : null; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit, split into two lines? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Comment 2: could this lead to null ptr exception if |
||
if (!track) { | ||
throw "Scan should be started to detect flash capability"; | ||
} | ||
|
||
return new ImageCapture(track).getPhotoCapabilities() | ||
.then((result) => { | ||
return result.fillLightMode.includes("flash"); | ||
}) | ||
.catch((err) => { | ||
this.logger.warn(err); | ||
return false; | ||
}); | ||
} | ||
|
||
/** | ||
* Checks if flash is turned on. | ||
* | ||
* @returns Current flash state. | ||
*/ | ||
public isFlashOn(): boolean { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why expose this state via a public method? |
||
return this.flashOn; | ||
} | ||
|
||
/** | ||
* Change flash state. | ||
* | ||
* @param on if true flash will be turned on otherwise turned off. | ||
* @returns Promise that flash state has changed. | ||
*/ | ||
public setFlash(on: boolean): Promise<void> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. optional, rename to more readable |
||
return this.hasFlash().then((hasFlash) => { | ||
if (!hasFlash) { | ||
return Promise.reject("No flash available"); | ||
} | ||
|
||
// At this point we already know that video element is not null and has track | ||
return (<MediaStream>this.videoElement!.srcObject).getVideoTracks()[0].applyConstraints({ | ||
advanced: [{ torch: on }] | ||
}); | ||
}).then(() => { | ||
this.flashOn = on; | ||
}); | ||
} | ||
|
||
/** | ||
* Scans an Image File for QR Code. | ||
* | ||
|
@@ -767,6 +833,7 @@ export class Html5Qrcode { | |
const tracks = stream.getVideoTracks(); | ||
for (const track of tracks) { | ||
track.enabled = false; | ||
// This will also stop flash light | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. super nit, add a "." after the comment There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (ditto elsewhere) |
||
track.stop(); | ||
stream.removeTrack(track); | ||
} | ||
|
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -5,7 +5,7 @@ | |||
"sourceMap": true, | ||||
"moduleResolution": "classic", | ||||
"lib": [ | ||||
"es7", | ||||
"es2018", | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this was already needed for: html5-qrcode/src/html5-qrcode.ts Line 1033 in a14ca5e
|
||||
"dom" | ||||
], | ||||
|
||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit, rename to
isFlashOn
for easier reading.