Repository navigation
Expand file tree
/
Copy pathfunction.js
More file actions
90 lines (73 loc) · 3.13 KB
/
Copy pathfunction.js
File metadata and controls
90 lines (73 loc) · 3.13 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// INITIALIZE SCANNER OUTSIDE FUNCTION
let scanner = null;
// ENSURE OPENCV IS LOADED
window.addEventListener("load", function () {
scanner = new jscanify();
});
window.function = async function (run, image) {
// CHECK IF FUNCTION SHOULD RUN
if (!run.value || run.value === "false") {
return "Waiting For Trigger";
}
// GET IMAGE URL FROM INPUT
const imageUrl = image.value;
// CHECK IF SCANNER IS INITIALIZED
if (!scanner) {
return imageUrl;
}
// CREATE AN IMAGE ELEMENT TO LOAD THE URL
const img = new Image();
img.crossOrigin = "anonymous";
// WRAP IN PROMISE TO HANDLE ASYNC IMAGE LOADING
return new Promise((resolve, reject) => {
img.onload = function () {
try {
// GET CONTOUR AND CORNER POINTS
const contour = scanner.findPaperContour(cv.imread(img));
const cornerPoints = scanner.getCornerPoints(contour);
if (!cornerPoints) {
// NO PAPER DETECTED, RETURN ORIGINAL
resolve(imageUrl);
return;
}
// CALCULATE DIMENSIONS FROM CORNER POINTS
const width = Math.max(Math.hypot(cornerPoints.topRightCorner.x - cornerPoints.topLeftCorner.x, cornerPoints.topRightCorner.y - cornerPoints.topLeftCorner.y), Math.hypot(cornerPoints.bottomRightCorner.x - cornerPoints.bottomLeftCorner.x, cornerPoints.bottomRightCorner.y - cornerPoints.bottomLeftCorner.y));
const height = Math.max(Math.hypot(cornerPoints.bottomLeftCorner.x - cornerPoints.topLeftCorner.x, cornerPoints.bottomLeftCorner.y - cornerPoints.topLeftCorner.y), Math.hypot(cornerPoints.bottomRightCorner.x - cornerPoints.topRightCorner.x, cornerPoints.bottomRightCorner.y - cornerPoints.topRightCorner.y));
// EXTRACT PAPER WITH DETECTED DIMENSIONS AND CORNER POINTS
const resultCanvas = scanner.extractPaper(img, width, height, cornerPoints);
// CONVERT TO BLACK AND WHITE AND TRIM EDGES
const processedCanvas = document.createElement("canvas");
const ctx = processedCanvas.getContext("2d");
// CALCULATE TRIMMED DIMENSIONS (REMOVE 1% FROM EACH SIDE)
const trimPercent = 0.01;
const trimmedWidth = resultCanvas.width * (1 - 2 * trimPercent);
const trimmedHeight = resultCanvas.height * (1 - 2 * trimPercent);
processedCanvas.width = trimmedWidth;
processedCanvas.height = trimmedHeight;
// DRAW TRIMMED IMAGE
ctx.drawImage(resultCanvas, resultCanvas.width * trimPercent, resultCanvas.height * trimPercent, trimmedWidth, trimmedHeight, 0, 0, trimmedWidth, trimmedHeight);
// CONVERT TO GRAYSCALE
const imageData = ctx.getImageData(0, 0, trimmedWidth, trimmedHeight);
const data = imageData.data;
for (let i = 0; i < data.length; i += 4) {
const gray = data[i] * 0.299 + data[i + 1] * 0.587 + data[i + 2] * 0.114;
data[i] = gray;
data[i + 1] = gray;
data[i + 2] = gray;
}
ctx.putImageData(imageData, 0, 0);
// RETURN THE PROCESSED IMAGE URL
resolve(processedCanvas.toDataURL("image/jpeg", 0.8));
} catch (error) {
// IF SCANNING FAILS, RETURN ORIGINAL IMAGE
resolve(imageUrl);
}
};
img.onerror = function () {
// IF IMAGE FAILS TO LOAD, RETURN ORIGINAL URL
resolve(imageUrl);
};
// START LOADING THE IMAGE
img.src = imageUrl;
});
};