Compare commits

..

11 Commits

Author SHA1 Message Date
ColonelParrot
e9af1941e6
Update README.md 2025-03-13 23:21:51 -04:00
ColonelParrot
ce85c440ad fix scaling issue for logo 2025-03-13 23:12:58 -04:00
ColonelParrot
3f4b902c48 Bump to v1.4.0 2025-02-18 11:38:31 -05:00
ColonelParrot
1fa3133944 optimize extractPaper by saving compute when cornerPoints supplied 2025-02-18 11:36:46 -05:00
ColonelParrot
58b26c34c7 fix skipped tests with before() 2025-02-18 11:36:12 -05:00
ColonelParrot
b4e68b12c9 Update canvas, jsdom and mocha to resolve audit warnings 2025-02-02 00:09:54 -05:00
ColonelParrot
57bf40f3fb bump to v1.3.3 2025-02-01 21:10:04 -05:00
ColonelParrot
c8e6ab035d Add null check in extractPaper 2025-02-01 21:06:30 -05:00
ColonelParrot
e2af210cf2 port bugfix to node 2025-02-01 21:01:53 -05:00
Leon Scherer
f0428d79ca fix: sending invalid index to opencv 2025-02-01 20:23:48 -05:00
ColonelParrot
a447c90039 add wiki link 2025-01-29 13:01:03 -05:00
6 changed files with 1497 additions and 2017 deletions

View File

@ -32,7 +32,7 @@
fill="black" class="octo-body"></path> fill="black" class="octo-body"></path>
</svg> </svg>
</a> </a>
<img src="images/logo-full.png" alt="jscanify logo" style="height: 100px" /> <img src="images/logo-full.png" alt="jscanify logo" style="width: 375px" />
<h2>the javascript document scanning library.</h2> <h2>the javascript document scanning library.</h2>
<br /> <br />
<div class="view-on"> <div class="view-on">

3266
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
{ {
"name": "jscanify", "name": "jscanify",
"version": "1.3.2", "version": "1.4.0",
"description": "Open-source Javascript mobile document scanner.", "description": "Open-source Javascript mobile document scanner.",
"main": "src/jscanify-node.js", "main": "src/jscanify-node.js",
"directories": { "directories": {
@ -25,8 +25,8 @@
}, },
"homepage": "https://colonelparrot.github.io/jscanify/", "homepage": "https://colonelparrot.github.io/jscanify/",
"dependencies": { "dependencies": {
"canvas": "^2.11.2", "canvas": "^3.1.0",
"jsdom": "^22.0.0", "jsdom": "^26.0.0",
"mocha": "^10.2.0" "mocha": "^11.1.0"
} }
} }

View File

@ -1,4 +1,4 @@
/*! jscanify v1.3.2 | (c) ColonelParrot and other contributors | MIT License */ /*! jscanify v1.4.0 | (c) ColonelParrot and other contributors | MIT License */
const { Canvas, createCanvas, Image, ImageData } = require("canvas"); const { Canvas, createCanvas, Image, ImageData } = require("canvas");
const { JSDOM } = require("jsdom"); const { JSDOM } = require("jsdom");
@ -80,7 +80,10 @@ class jscanify {
} }
} }
const maxContour = contours.get(maxContourIndex); const maxContour =
maxContourIndex >= 0 ?
contours.get(maxContourIndex) :
null;
imgGray.delete(); imgGray.delete();
imgBlur.delete(); imgBlur.delete();
@ -138,6 +141,9 @@ class jscanify {
/** /**
* Extracts and undistorts the image detected within the frame. * Extracts and undistorts the image detected within the frame.
*
* Returns `null` if no paper is detected.
*
* @param {*} image image to process * @param {*} image image to process
* @param {*} resultWidth desired result paper width * @param {*} resultWidth desired result paper width
* @param {*} resultHeight desired result paper height * @param {*} resultHeight desired result paper height
@ -147,7 +153,12 @@ class jscanify {
extractPaper(image, resultWidth, resultHeight, cornerPoints) { extractPaper(image, resultWidth, resultHeight, cornerPoints) {
const canvas = createCanvas(); const canvas = createCanvas();
const img = cv.imread(image); const img = cv.imread(image);
const maxContour = this.findPaperContour(img); const maxContour = cornerPoints ? null : this.findPaperContour(img);
if(maxContour == null && cornerPoints === undefined){
return null;
}
const { const {
topLeftCorner, topLeftCorner,
topRightCorner, topRightCorner,

View File

@ -1,4 +1,4 @@
/*! jscanify v1.3.2 | (c) ColonelParrot and other contributors | MIT License */ /*! jscanify v1.4.0 | (c) ColonelParrot and other contributors | MIT License */
(function (global, factory) { (function (global, factory) {
typeof exports === "object" && typeof module !== "undefined" typeof exports === "object" && typeof module !== "undefined"
@ -71,7 +71,10 @@
} }
} }
const maxContour = contours.get(maxContourIndex); const maxContour =
maxContourIndex >= 0 ?
contours.get(maxContourIndex) :
null;
imgGray.delete(); imgGray.delete();
imgBlur.delete(); imgBlur.delete();
@ -129,6 +132,9 @@
/** /**
* Extracts and undistorts the image detected within the frame. * Extracts and undistorts the image detected within the frame.
*
* Returns `null` if no paper is detected.
*
* @param {*} image image to process * @param {*} image image to process
* @param {*} resultWidth desired result paper width * @param {*} resultWidth desired result paper width
* @param {*} resultHeight desired result paper height * @param {*} resultHeight desired result paper height
@ -137,10 +143,12 @@
*/ */
extractPaper(image, resultWidth, resultHeight, cornerPoints) { extractPaper(image, resultWidth, resultHeight, cornerPoints) {
const canvas = document.createElement("canvas"); const canvas = document.createElement("canvas");
const img = cv.imread(image); const img = cv.imread(image);
const maxContour = cornerPoints ? null : this.findPaperContour(img);
const maxContour = this.findPaperContour(img); if(maxContour == null && cornerPoints === undefined){
return null;
}
const { const {
topLeftCorner, topLeftCorner,

View File

@ -39,21 +39,30 @@ function setup() {
}) })
} }
let scanner;
let cv;
before(function (done) {
console.log("=== beginning tests ==="); console.log("=== beginning tests ===");
console.log("loading OpenCV.js..."); console.log("loading OpenCV.js...");
const scanner = new jscanify(); scanner = new jscanify();
scanner.loadOpenCV(function (cv) { scanner.loadOpenCV(function (loadedCv) {
cv = loadedCv;
console.log("Finished loading OpenCV.js"); console.log("Finished loading OpenCV.js");
console.log("Writing test images to: " + OUTPUT_FOLDER); console.log("Writing test images to: " + OUTPUT_FOLDER);
setup()
done();
});
});
/** /**
* tests an individual image * tests an individual image
*/ */
function test(testImage, imageCount) { function test(testImage, imageCount) {
describe("image #" + imageCount, function () { describe("image #" + imageCount, function () {
it("should highlight paper", function (done) { it("should highlight paper", function () {
const highlighted = scanner.highlightPaper(testImage); const highlighted = scanner.highlightPaper(testImage);
const higlightedOutputPath = OUTPUT_FOLDER + "highlighted-" + imageCount + ".jpg"; const higlightedOutputPath = OUTPUT_FOLDER + "highlighted-" + imageCount + ".jpg";
writeFileSync( writeFileSync(
@ -62,10 +71,9 @@ scanner.loadOpenCV(function (cv) {
); );
assert.ok(existsSync(higlightedOutputPath)); assert.ok(existsSync(higlightedOutputPath));
done();
}); });
it("should extract paper", function (done) { it("should extract paper", function () {
const extracted = scanner.extractPaper(testImage, 386, 500); const extracted = scanner.extractPaper(testImage, 386, 500);
const extractedOutputPath = OUTPUT_FOLDER + "extracted-" + imageCount + ".jpg"; const extractedOutputPath = OUTPUT_FOLDER + "extracted-" + imageCount + ".jpg";
@ -75,10 +83,9 @@ scanner.loadOpenCV(function (cv) {
); );
assert.ok(existsSync(extractedOutputPath)); assert.ok(existsSync(extractedOutputPath));
done();
}); });
it("should label corner points", function (done) { it("should label corner points", function () {
const parsedImage = cv.imread(testImage); const parsedImage = cv.imread(testImage);
const paperContour = scanner.findPaperContour(parsedImage); const paperContour = scanner.findPaperContour(parsedImage);
const { const {
@ -111,13 +118,10 @@ scanner.loadOpenCV(function (cv) {
writeFileSync(cornerPointsOutputPath, canvas.toBuffer("image/jpeg")); writeFileSync(cornerPointsOutputPath, canvas.toBuffer("image/jpeg"));
assert.ok(existsSync(cornerPointsOutputPath)); assert.ok(existsSync(cornerPointsOutputPath));
done();
}); });
}); });
} }
setup();
let imageCount = 1; let imageCount = 1;
/* /*
@ -134,4 +138,3 @@ scanner.loadOpenCV(function (cv) {
}); });
} }
}) })
});