Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: BarcodeDetector Polyfill (#78)
* feat: very first implement for qr-code reader * feat: first implement of polyfill that not has strictly same api for qr-code reader * fix: BarcodeDetector
- Loading branch information
1 parent
963c329
commit 84b55c5
Showing
9 changed files
with
2,735 additions
and
160 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
40 changes: 40 additions & 0 deletions
40
src/components/Welcomes/QRScanner/ShapeDetectionPolyfill.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import jsQR from 'jsqr' | ||
import { isNull } from 'lodash-es' | ||
/// <reference path="./ShapeDetectionSpec.d.ts" /> | ||
|
||
class BarcodeDetectorPolyfill implements BarcodeDetector { | ||
constructor() {} | ||
// noinspection JSMethodCanBeStatic | ||
public async detect(mediaSource: CanvasImageSource) { | ||
const canvas = document.createElement('canvas') | ||
;[canvas.width, canvas.height] = [1920, 1080] | ||
const ctx = canvas.getContext('2d') | ||
if (isNull(ctx)) { | ||
throw new Error('Canvas was not supported') | ||
} | ||
ctx.drawImage(mediaSource, 0, 0) | ||
const d = ctx.getImageData(0, 0, canvas.width, canvas.height) | ||
const res = jsQR(d.data, canvas.width, canvas.height) | ||
if (isNull(res)) { | ||
return [] | ||
} | ||
const result = new DetectedBarcodePolyfill() | ||
result.rawValue = res.data | ||
return [result] | ||
} | ||
} | ||
class DetectedBarcodePolyfill implements DetectedBarcode { | ||
get boundingBox(): DOMRectReadOnly { | ||
throw new Error('Not implemented') | ||
} | ||
cornerPoints: { x: number; y: number }[] = [] | ||
format = 'qr_code' | ||
rawValue!: string | ||
} | ||
|
||
if (!('BarcodeDetector' in window)) { | ||
Object.assign(window, { | ||
BarcodeDetector: BarcodeDetectorPolyfill, | ||
DetectedBarcode: DetectedBarcodePolyfill, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
declare class BarcodeDetector { | ||
constructor(options: { formats: string[] }) | ||
public async detect(mediaSource: CanvasImageSource): Promise<DetectedBarcode[]> | ||
} | ||
declare class DetectedBarcode { | ||
boundingBox: DOMRectReadOnly | ||
cornerPoints: { x: number; y: number }[] | ||
format: string | ||
rawValue: string | ||
} | ||
|
||
interface Window { | ||
BarcodeDetector: BarcodeDetector | ||
DetectedBarcode: DetectedBarcode | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.