forked from cozmo/jsQR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
56 lines (48 loc) · 1.56 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
import {binarize} from "./binarizer";
import {BitMatrix} from "./BitMatrix";
import {Chunks} from "./decoder/decodeData";
import {decode} from "./decoder/decoder";
import {extract} from "./extractor";
import {locate, Point} from "./locator";
export interface QRCode {
binaryData: number[];
data: string;
chunks: Chunks;
location: {
topRightCorner: Point;
topLeftCorner: Point;
bottomRightCorner: Point;
bottomLeftCorner: Point;
topRightFinderPattern: Point;
topLeftFinderPattern: Point;
bottomLeftFinderPattern: Point;
bottomRightAlignmentPattern?: Point;
};
}
export default function x(data: Uint8ClampedArray, width: number, height: number): QRCode | null {
const binarized = binarize(data, width, height);
const location = locate(binarized);
if (!location) {
return null;
}
const extracted = extract(binarized, location);
const decoded = decode(extracted.matrix);
if (!decoded) {
return null;
}
return {
binaryData: decoded.bytes,
data: decoded.text,
chunks: decoded.chunks,
location: {
topRightCorner: extracted.mappingFunction(location.dimension, 0),
topLeftCorner: extracted.mappingFunction(0, 0),
bottomRightCorner: extracted.mappingFunction(location.dimension, location.dimension),
bottomLeftCorner: extracted.mappingFunction(0, location.dimension),
topRightFinderPattern: location.topRight,
topLeftFinderPattern: location.topLeft,
bottomLeftFinderPattern: location.bottomLeft,
bottomRightAlignmentPattern: location.alignmentPattern,
},
};
}