Skip to content

Commit

Permalink
Made example work
Browse files Browse the repository at this point in the history
  • Loading branch information
treeder committed Sep 21, 2023
1 parent 040b53a commit 3eae284
Show file tree
Hide file tree
Showing 5 changed files with 218 additions and 90 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

An ESM ready QR code scanner along with a nice web component you can drop in anywhere. This is a fork and cleanup of the [ZXing library](https://github.com/zxing-js/library) to make it work as an ESM module.

We will keep trying to modernize this library to ESM standards and we do accept pull requests, so please contribute if you can.

## Quickstart

The easiest way is to use the web component.
Expand All @@ -12,11 +14,10 @@ The easiest way is to use the web component.
import 'https://cdn.jsdelivr.net/gh/chainparency/zxinger@0/components/zxinger-scanner.js'
document.querySelector("zxinger").addEventListener("change", (e) => console.log("result:", e.detail.value))
document.querySelector("#scanButton").addEventListener("click", () => document.querySelector("#zxinger").open())
</script>

<zxinger-scanner id="zxinger"></zxinger-scanner>
<button id="scanButton"></button>
```

## More control

TODO
2 changes: 1 addition & 1 deletion browser/BrowserCodeReader.js
Original file line number Diff line number Diff line change
Expand Up @@ -798,7 +798,7 @@ var BrowserCodeReader = /** @class */ (function () {
var _this = this;
if (retryIfNotFound === void 0) { retryIfNotFound = true; }
if (retryIfChecksumOrFormatError === void 0) { retryIfChecksumOrFormatError = true; }
console.log("decodeOnce", element, "retryIfNotFound?", retryIfNotFound, "retryIfChecksumOrFormatError?", retryIfChecksumOrFormatError);
// console.log("decodeOnce", element, "retryIfNotFound?", retryIfNotFound, "retryIfChecksumOrFormatError?", retryIfChecksumOrFormatError);
this._stopAsyncDecode = false;
var loop = function (resolve, reject) {
if (_this._stopAsyncDecode) {
Expand Down
46 changes: 45 additions & 1 deletion components/example.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,52 @@
<html>

<head>

<title>ZXinger Demo</title>

<script type="importmap">
{
"imports": {
"lit": "https://cdn.jsdelivr.net/npm/lit@2/index.js",
"lit/": "https://cdn.jsdelivr.net/npm/lit@2/",
"@material/web/": "https://cdn.jsdelivr.net/npm/@material/[email protected]/",
"@lit/reactive-element": "https://cdn.jsdelivr.net/npm/@lit/reactive-element@1/reactive-element.js",
"@lit/reactive-element/": "https://cdn.jsdelivr.net/npm/@lit/reactive-element@1/",
"lit-element/lit-element.js": "https://cdn.jsdelivr.net/npm/lit-element@3/lit-element.js",
"lit-html": "https://cdn.jsdelivr.net/npm/lit-html@2/lit-html.js",
"lit-html/": "https://cdn.jsdelivr.net/npm/lit-html@2/",
"tslib": "https://cdn.jsdelivr.net/npm/tslib@2/tslib.es6.mjs"
}
}
</script>

</head>

<body>

<qr-code-scanner id="scanner" @change=${this.scanComplete}></qr-code-scanner>
<script type="module">
import './zxinger-dialog.js'
import '@material/web/button/filled-button.js'

document.querySelector("#zxinger").addEventListener("change", (e) => {
console.log("result:", e.detail.value)
document.querySelector("#result").innerHTML = e.detail.value
})
document.querySelector("#scanButton").addEventListener("click", () => document.querySelector("#zxinger").open())

</script>

<xzinger-dialog id="zxinger"></xzinger-dialog>

<div style="display: flex; flex-direction: column; gap: 20px; justify-content: center; align-items: center;">
<h1>ZXinger Demo</h1>
<div>
<span id="result"></span>
</div>
<div>
<md-filled-button id="scanButton">Scan</md-filled-button>
</div>
</div>

</body>

Expand Down
139 changes: 139 additions & 0 deletions components/zxinger-dialog.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
import { html, css, LitElement } from 'lit'
import '@material/web/button/filled-button.js'
// import '@material/web/button/outlined-button.js'
// import '@material/web/button/text-button.js'
// import '@material/web/iconbutton/icon-button.js'
// import '@material/web/iconbutton/filled-icon-button.js'
// import '@material/web/progress/circular-progress.js'
// import '@material/web/progress/linear-progress.js'
// import '@material/web/menu/menu.js'
// import '@material/web/menu/menu-item.js'
// import '@material/web/list/list.js'
// import '@material/web/list/list-item.js'
import '@material/web/dialog/dialog.js'
import './zxinger-scanner.js'

export class ZXingerDialog extends LitElement {

static properties = {
selectedDeviceId: { type: String },
result: { type: String },
codeReader: { type: Object },
error: { type: Object },
}

static styles = [
css`
.column {
display: flex;
flex-direction: column;
}
.row {
display: flex;
flex-direction: row;
align-items: center;
justify-content: center;
}
`
]

constructor() {
super()
this.selectedDeviceId = ""
this.result = ""

this.error = null
}

connectedCallback() {
super.connectedCallback()
}

render() {
return html`
<md-dialog id="dialog" @close=${this.dialogClose} @cancel=${this.dialogCancel}>
<span slot="headline">Scan a code</span>
<div slot="content">
<div class="column">
<div class="row">
<xzinger-scanner id="zxinger" @change=${this.changed}></zxinger-scanner>
</div>
<div class="row">
<pre>
<code id="result">
${this.result}
</code>
</pre>
</div>
</div>
</div>
<div slot="actions">
<md-outlined-button type="button" @click=${this.close}>Cancel</md-outlined-button>
<!-- <md-outlined-button type="button" @click=${this._choose}>Ok</md-outlined-button> -->
</div>
</md-dialog>
`
}

changed(e) {
console.log("changed", e.detail.value)
this.result = e.detail.value
this.dispatchEvent(new CustomEvent("change", { detail: { value: this.result } }))
this.close()
}

dialogClose() {
console.log('dialogClose')
this.reset()
}

reset() {
this.renderRoot.querySelector("#zxinger").close()
this.result = ''
}

dialogCancel() {
// console.log('dialogCancel')
// this.reset()
}

firstUpdated() {
super.firstUpdated()
}

_choose() {
this.dispatchEvent(new CustomEvent("change", { detail: { result: this.result } }))
this.close()
}

close() {
// this.reset()
const dialog = this.renderRoot.querySelector('#dialog')
dialog.close()
}

async start() {

let controls = this.decodeOnce(this.codeReader, this.selectedDeviceId)

}


async open() {
const dialog = this.renderRoot.querySelector('#dialog')
dialog.show()

let z = this.renderRoot.querySelector("#zxinger")

try {
z.start()
} catch (err) {
console.error(err)
}
}

}

customElements.define("xzinger-dialog", ZXingerDialog)
114 changes: 29 additions & 85 deletions components/zxinger-scanner.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { html, css, LitElement } from 'https://cdn.jsdelivr.net/npm/lit@2/+esm'
import { BrowserMultiFormatReader, BrowserQRCodeReader } from 'https://cdn.jsdelivr.net/gh/chainparency/zxinger@0/index.js'
import { html, css, LitElement } from 'lit'
import { BrowserMultiFormatReader } from 'https://cdn.jsdelivr.net/gh/chainparency/zxinger@0/index.js'

export class ZxingerScanner extends LitElement {
export class ZXingerScanner extends LitElement {

static properties = {
selectedDeviceId: { type: String },
Expand All @@ -10,27 +10,12 @@ export class ZxingerScanner extends LitElement {
error: { type: Object },
}

static styles = [
css`
.column {
display: flex;
flex-direction: column;
}
.row {
display: flex;
flex-direction: row;
align-items: center;
justify-content: center;
}
`
]
static styles = css``

constructor() {
super()
this.selectedDeviceId = ""
this.result = ""
this.selectedDeviceId = ''
this.result = ''

this.codeReader = new BrowserMultiFormatReader()

Expand All @@ -42,61 +27,42 @@ export class ZxingerScanner extends LitElement {
}

render() {
console.log("render")
return html`
<md-dialog id="dialog" @close=${this.dialogClose} @cancel=${this.dialogCancel}>
<span slot="headline">Scan a code</span>
<div slot="content">
<div class="column">
<div class="row">
return html`
<video id="video" width="300" height="200" style="border: 1px solid gray"></video>
</div>
<div class="row">
<pre>
<code id="result">
${this.result}
</code>
</pre>
</div>
</div>
</div>
<div slot="actions">
<md-outlined-button type="button" @click=${this.close}>Cancel</md-outlined-button>
<!-- <md-outlined-button type="button" @click=${this._choose}>Ok</md-outlined-button> -->
</div>
</md-dialog>
`;
`
}

dialogClose() {
console.log('dialogClose')
close() {
this.reset()
}

dialogCancel() {
// console.log('dialogCancel')
// this.reset()
}

firstUpdated() {
super.firstUpdated()
reset() {
this.codeReader.reset()
this.result = ''
}

_choose() {
this.dispatchEvent(new CustomEvent("change", { detail: { result: this.result } }))
this.close()
}
async start() {
let controls = this.decodeOnce(this.codeReader, this.selectedDeviceId)

close() {
// this.reset()
const dialog = this.renderRoot.querySelector('#dialog')
dialog.close()
}

async start() {
async open() {
const dialog = this.renderRoot.querySelector('#dialog')
dialog.show()

let controls = this.decodeOnce(this.codeReader, this.selectedDeviceId)
try {
let videoInputDevices = await this.codeReader.listVideoInputDevices()
// console.log('videoInputDevices', videoInputDevices)
this.selectedDeviceId = videoInputDevices[0].deviceId
if (videoInputDevices.length > 1) {
this.selectedDeviceId = videoInputDevices[1].deviceId //back camera on mobile phones
}
this.start()

} catch (err) {
console.error(err)
}
}

async decodeOnce(codeReader, selectedDeviceId) {
Expand All @@ -116,29 +82,7 @@ export class ZxingerScanner extends LitElement {
}
}

reset() {
this.codeReader.reset()
this.result = ''
}

async open() {
const dialog = this.renderRoot.querySelector('#dialog')
dialog.show()

try {
let videoInputDevices = await this.codeReader.listVideoInputDevices()
// console.log('videoInputDevices', videoInputDevices)
this.selectedDeviceId = videoInputDevices[0].deviceId
if (videoInputDevices.length > 1) {
this.selectedDeviceId = videoInputDevices[1].deviceId //back camera on mobile phones
}
this.start()

} catch (err) {
console.error(err)
}
}

}

customElements.define("xzinger-scanner", ZxingerScanner)
customElements.define("xzinger-scanner", ZXingerScanner)

0 comments on commit 3eae284

Please sign in to comment.