Compare commits

..

1 Commits

Author SHA1 Message Date
ColonelParrot
1138b6216e
Update README.md 2025-01-27 20:05:13 -05:00
6 changed files with 2022 additions and 1502 deletions

View File

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

3282
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

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

View File

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

View File

@ -1,4 +1,4 @@
/*! jscanify v1.4.0 | (c) ColonelParrot and other contributors | MIT License */
/*! jscanify v1.3.2 | (c) ColonelParrot and other contributors | MIT License */
(function (global, factory) {
typeof exports === "object" && typeof module !== "undefined"
@ -71,10 +71,7 @@
}
}
const maxContour =
maxContourIndex >= 0 ?
contours.get(maxContourIndex) :
null;
const maxContour = contours.get(maxContourIndex);
imgGray.delete();
imgBlur.delete();
@ -132,10 +129,7 @@
/**
* 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 {*} resultHeight desired result paper height
* @param {*} cornerPoints optional custom corner points, in case automatic corner points are incorrect
@ -143,12 +137,10 @@
*/
extractPaper(image, resultWidth, resultHeight, cornerPoints) {
const canvas = document.createElement("canvas");
const img = cv.imread(image);
const maxContour = cornerPoints ? null : this.findPaperContour(img);
if(maxContour == null && cornerPoints === undefined){
return null;
}
const img = cv.imread(image);
const maxContour = this.findPaperContour(img);
const {
topLeftCorner,

View File

@ -16,125 +16,122 @@ const path = require("path");
const OUTPUT_FOLDER = __dirname.replaceAll("\\", "/") + "/output/";
const TEST_IMAGE_DIRECTORY = path.join(
__dirname,
"..",
"docs",
"images",
"test"
__dirname,
"..",
"docs",
"images",
"test"
);
/**
* delete previously generated output images
*/
function setup() {
console.log("=== setting up tests ===");
console.log("Deleting previously generated images");
console.log("=== setting up tests ===");
console.log("Deleting previously generated images");
if (!existsSync(OUTPUT_FOLDER)) {
mkdirSync(OUTPUT_FOLDER);
}
if (!existsSync(OUTPUT_FOLDER)) {
mkdirSync(OUTPUT_FOLDER);
}
readdirSync(OUTPUT_FOLDER).forEach((file) => {
unlinkSync(path.join(OUTPUT_FOLDER, file));
})
readdirSync(OUTPUT_FOLDER).forEach((file) => {
unlinkSync(path.join(OUTPUT_FOLDER, file));
})
}
let scanner;
let cv;
console.log("=== beginning tests ===");
console.log("loading OpenCV.js...");
before(function (done) {
console.log("=== beginning tests ===");
console.log("loading OpenCV.js...");
const scanner = new jscanify();
scanner.loadOpenCV(function (cv) {
scanner = new jscanify();
scanner.loadOpenCV(function (loadedCv) {
cv = loadedCv;
console.log("Finished loading OpenCV.js");
console.log("Writing test images to: " + OUTPUT_FOLDER);
console.log("Finished loading OpenCV.js");
console.log("Writing test images to: " + OUTPUT_FOLDER);
setup()
done();
});
});
/**
* tests an individual image
*/
function test(testImage, imageCount) {
/**
* tests an individual image
*/
function test(testImage, imageCount) {
describe("image #" + imageCount, function () {
it("should highlight paper", function () {
const highlighted = scanner.highlightPaper(testImage);
const higlightedOutputPath = OUTPUT_FOLDER + "highlighted-" + imageCount + ".jpg";
writeFileSync(
higlightedOutputPath,
highlighted.toBuffer("image/jpeg")
);
it("should highlight paper", function (done) {
const highlighted = scanner.highlightPaper(testImage);
const higlightedOutputPath = OUTPUT_FOLDER + "highlighted-" + imageCount + ".jpg";
writeFileSync(
higlightedOutputPath,
highlighted.toBuffer("image/jpeg")
);
assert.ok(existsSync(higlightedOutputPath));
assert.ok(existsSync(higlightedOutputPath));
done();
});
it("should extract paper", function (done) {
const extracted = scanner.extractPaper(testImage, 386, 500);
const extractedOutputPath = OUTPUT_FOLDER + "extracted-" + imageCount + ".jpg";
writeFileSync(
extractedOutputPath,
extracted.toBuffer("image/jpeg")
);
assert.ok(existsSync(extractedOutputPath));
done();
});
it("should label corner points", function (done) {
const parsedImage = cv.imread(testImage);
const paperContour = scanner.findPaperContour(parsedImage);
const {
topLeftCorner,
topRightCorner,
bottomLeftCorner,
bottomRightCorner,
} = scanner.getCornerPoints(paperContour, testImage);
const canvas = createCanvas();
cv.imshow(canvas, parsedImage);
const ctx = canvas.getContext("2d");
const points = [
{ p: topLeftCorner, text: "top left corner" },
{ p: topRightCorner, text: "top right corner" },
{ p: bottomLeftCorner, text: "bottom left corner" },
{ p: bottomRightCorner, text: "bottom right corner" },
];
ctx.fillStyle = "cyan";
ctx.font = "25px serif";
points.forEach(({ p: point, text }) => {
ctx.beginPath();
ctx.arc(point.x, point.y, 15, 0, 2 * Math.PI, false);
ctx.fillText(text, point.x + 30, point.y)
ctx.fill();
});
it("should extract paper", function () {
const extracted = scanner.extractPaper(testImage, 386, 500);
const extractedOutputPath = OUTPUT_FOLDER + "extracted-" + imageCount + ".jpg";
const cornerPointsOutputPath = OUTPUT_FOLDER + "corner_points-" + imageCount + ".jpg";
writeFileSync(cornerPointsOutputPath, canvas.toBuffer("image/jpeg"));
writeFileSync(
extractedOutputPath,
extracted.toBuffer("image/jpeg")
);
assert.ok(existsSync(extractedOutputPath));
});
it("should label corner points", function () {
const parsedImage = cv.imread(testImage);
const paperContour = scanner.findPaperContour(parsedImage);
const {
topLeftCorner,
topRightCorner,
bottomLeftCorner,
bottomRightCorner,
} = scanner.getCornerPoints(paperContour, testImage);
const canvas = createCanvas();
cv.imshow(canvas, parsedImage);
const ctx = canvas.getContext("2d");
const points = [
{ p: topLeftCorner, text: "top left corner" },
{ p: topRightCorner, text: "top right corner" },
{ p: bottomLeftCorner, text: "bottom left corner" },
{ p: bottomRightCorner, text: "bottom right corner" },
];
ctx.fillStyle = "cyan";
ctx.font = "25px serif";
points.forEach(({ p: point, text }) => {
ctx.beginPath();
ctx.arc(point.x, point.y, 15, 0, 2 * Math.PI, false);
ctx.fillText(text, point.x + 30, point.y)
ctx.fill();
});
const cornerPointsOutputPath = OUTPUT_FOLDER + "corner_points-" + imageCount + ".jpg";
writeFileSync(cornerPointsOutputPath, canvas.toBuffer("image/jpeg"));
assert.ok(existsSync(cornerPointsOutputPath));
});
assert.ok(existsSync(cornerPointsOutputPath));
done();
});
});
}
}
let imageCount = 1;
setup();
/*
* go through all images in test image directory
*/
readdirSync(TEST_IMAGE_DIRECTORY).forEach((file) => {
let imageCount = 1;
/*
* go through all images in test image directory
*/
readdirSync(TEST_IMAGE_DIRECTORY).forEach((file) => {
const TEST_IMAGE_PATH = path.join(TEST_IMAGE_DIRECTORY, file);
if (!file.endsWith("-sized.png")) { // these images are for the website, not testing
let tempCount = imageCount++;
if(!file.endsWith("-sized.png")){ // these images are for the website, not testing
let tempCount = imageCount++;
loadImage(TEST_IMAGE_PATH).then(function (image) {
test(image, tempCount);
});
loadImage(TEST_IMAGE_PATH).then(function (image) {
test(image, tempCount);
});
}
})
})
});