Skip to content
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 Custom camera labels when not available. #580

Merged
merged 1 commit into from
Nov 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
### Version 2.2.8

#### Custom camera labels when not available.
In certain browsers as well as cases like Android Webview it looks like camera
name is not returned by the browser. In such cases the camera selection has
empty named options.

To make the UX better, the library will give custom names to the cameras.

- Github Issue: [Issue#578](https://github.com/mebjas/html5-qrcode/issues/578)

For example in Duck Duck Go browser which has this behavior, it will look like
this

| Before selection | After selection |
| --- | --- |
| ![Screenshot_20221105-005544](https://user-images.githubusercontent.com/3007365/200032567-eb50b4f0-e25f-4bdb-a233-fcbb906122aa.png) | ![Screenshot_20221105-005550](https://user-images.githubusercontent.com/3007365/200032557-21679229-3d21-4212-a22f-1f2558b6f6b6.png) |


### Version 2.2.7

#### Add support for custom CSS
Expand Down
2 changes: 1 addition & 1 deletion minified/html5-qrcode.min.js

Large diffs are not rendered by default.

12 changes: 11 additions & 1 deletion src/html5-qrcode-scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -712,9 +712,19 @@ export class Html5QrcodeScanner {
= `${selectCameraString} (${cameras.length}) `;
}
const options = [];
let anonymousCameraId = 1;
for (const camera of cameras) {
const value = camera.id;
const name = camera.label == null ? value : camera.label;
let name = camera.label == null ? value : camera.label;
// If no name is returned by the browser, replace it with custom
// camera label with a count.
if (!name || name === "") {
name = [
Html5QrcodeScannerStrings.anonymousCameraPrefix(),
anonymousCameraId++
].join(" ");
}

const option = document.createElement("option");
option.value = value;
option.innerText = name;
Expand Down
5 changes: 5 additions & 0 deletions src/strings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,11 @@ export class Html5QrcodeScannerStrings {
public static fileSelectionNoImageSelected(): string {
return "No image choosen";
}

/** Prefix to be given to anonymous cameras. */
public static anonymousCameraPrefix(): string {
return "Anonymous Camera";
}
}

/** Strings used in {@class LibraryInfoDiv} */
Expand Down