Split On QR Code

This commit is contained in:
Felix Kaspar 2023-10-28 19:30:12 +02:00
parent 013a3f7e1b
commit f7e5fd3aef
3 changed files with 47 additions and 13 deletions

View File

@ -29,7 +29,7 @@ export async function detectEmptyPages(snapshot, whiteThreashold, PDFJS, OpenCV)
}
async function areImagesBlank(page, threshold) {
const images = getImagesOnPage(page, PDFJS);
const images = await getImagesOnPage(page, PDFJS);
for (const image of images) {
if(!isImageBlank(image, threshold))
return false;
@ -37,7 +37,7 @@ export async function detectEmptyPages(snapshot, whiteThreashold, PDFJS, OpenCV)
return true;
}
async function isImageBlank(image, threshold) {
function isImageBlank(image, threshold) {
const src = new OpenCV.cv.Mat(image.width, image.height, OpenCV.cv.CV_8UC4);
src.data.set(image.data);
// Convert the image to grayscale

View File

@ -1,5 +1,6 @@
import { detectEmptyPages } from "./shared/detectEmptyPages.js";
import { getImagesOnPage } from "./shared/getImagesOnPage.js";
import { createSubDocument } from "./shared/createSubDocument.js";
/**
* @typedef {"BAR_CODE"|"QR_CODE"|"BLANK_PAGE"} SplitType
@ -39,18 +40,42 @@ export async function splitOn(snapshot, type, whiteThreashold, PDFJS, OpenCV, PD
console.log("Split At Pages: ", splitAtPages);
// Remove detected Pages & Split
const pdfDoc = await PDFLib.PDFDocument.load(snapshot);
// TODO: Remove detected Pages & Split
const numberOfPages = pdfDoc.getPages().length;
return pdfDoc.save();
let pagesArray = [];
let splitAfter = splitAtPages.shift();
const subDocuments = [];
for (let i = 0; i < numberOfPages; i++) {
console.log(i);
if(i == splitAfter) {
if(pagesArray.length > 0) {
subDocuments.push(await createSubDocument(pdfDoc, pagesArray, PDFLib));
pagesArray = [];
}
splitAfter = splitAtPages.shift();
}
else { // Skip splitAtPage
console.log("PagesArray")
pagesArray.push(i);
}
}
if(pagesArray.length > 0) {
subDocuments.push(await createSubDocument(pdfDoc, pagesArray, PDFLib));
}
pagesArray = [];
return subDocuments;
async function getPagesWithQRCode(snapshot) {
const pdfDoc = await PDFJS.getDocument(snapshot).promise;
const pagesWithQR = [];
for (let i = 1; i <= pdfDoc.numPages; i++) {
const page = await pdfDoc.getPage(i);
for (let i = 0; i < pdfDoc.numPages; i++) {
const page = await pdfDoc.getPage(i + 1);
console.log("Checking page " + i);
const images = await getImagesOnPage(page, PDFJS);

View File

@ -93,8 +93,7 @@ export async function * traverseOperations(operations, input, Functions) {
buffer: splitResult[j]
})
}
input = splits;
return splits;
});
break;
case "editMetadata":
@ -117,8 +116,17 @@ export async function * traverseOperations(operations, input, Functions) {
break;
case "splitOn":
yield* oneToN(input, operation, async (input) => {
input.fileName += "_split";
input.buffer = await Functions.splitOn(input.buffer, operation.values["type"], operation.values["whiteThreashold"]);
const splitResult = await Functions.splitOn(input.buffer, operation.values["type"], operation.values["whiteThreashold"]);
const splits = [];
for (let j = 0; j < splitResult.length; j++) {
splits.push({
originalFileName: input.originalFileName,
fileName: input.fileName + "_split" + j,
buffer: splitResult[j]
})
}
return splits;
});
break;
default:
@ -138,13 +146,14 @@ export async function * traverseOperations(operations, input, Functions) {
async function * oneToN(input, operation, callback) {
if(Array.isArray(input)) {
let output = [];
for (let i = 0; i < input.length; i++) {
await callback(input[i]);
output = output.concat(await callback(input[i]));
}
yield* nextOperation(operation.operations, input);
yield* nextOperation(operation.operations, output);
}
else {
await callback(input);
input = await callback(input);
yield* nextOperation(operation.operations, input);
}
}