-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
67 lines (51 loc) · 2.24 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { QRSharpener } from "./QRSharpener";
import { create } from "domain";
const annotatedImage = document.getElementById("annotatedImage") as HTMLImageElement;
const resultImage = document.getElementById("resultImage") as HTMLImageElement;
const uploader = document.getElementById("uploader") as HTMLInputElement;
const statusDiv = document.getElementById("status") as HTMLDivElement;
const dimensionsEdit = document.getElementById("dimensions") as HTMLInputElement;
const canvas = document.createElement("canvas");
const resultCanvas = document.createElement("canvas");
uploader.addEventListener("change", fileUploaded, false);
class Spinner {
constructor(public readonly target: HTMLDivElement) {}
start() {
this.target.innerHTML = "Computing...";
}
stop() {
this.target.innerText = "";
}
}
function fileUploaded() {
const files = uploader.files;
if (files === null)
return;
const spinner = new Spinner(statusDiv);
spinner.start();
const file = files[0];
createImageBitmap(file).then(processFile).then(res => spinner.stop()).catch((err: any) => console.error(err));
}
function processFile(bitmap: ImageBitmap) {
canvas.width = bitmap.width;
canvas.height = bitmap.height;
const context = canvas.getContext("2d");
if (context === null)
throw new Error("Cannot get 2d canvas context");
const dimensions = parseInt(dimensionsEdit.value);
context.drawImage(bitmap, 0, 0);
const data = context.getImageData(0, 0, bitmap.width, bitmap.height);
const sharpener = new QRSharpener(dimensions, 50);
const result = sharpener.sharpen(data);
const resultImageData = new ImageData(Uint8ClampedArray.from(result.qrCodeBuffer), 29, 29);
const annotatedImageData = new ImageData(Uint8ClampedArray.from(result.annotatedImageBuffer), bitmap.width, bitmap.height);
renderResult(annotatedImageData, annotatedImage);
renderResult(resultImageData, resultImage);
}
function renderResult(imageData: ImageData, destination: HTMLImageElement) {
resultCanvas.width = imageData.width;
resultCanvas.height = imageData.height;
const context = resultCanvas.getContext('2d')!;
context.putImageData(imageData, 0, 0);
destination.src = resultCanvas.toDataURL();
}