mirror of
https://github.com/Frooodle/Stirling-PDF.git
synced 2025-01-10 00:06:51 +01:00
Split On QR Code
This commit is contained in:
parent
013a3f7e1b
commit
f7e5fd3aef
@ -29,7 +29,7 @@ export async function detectEmptyPages(snapshot, whiteThreashold, PDFJS, OpenCV)
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function areImagesBlank(page, threshold) {
|
async function areImagesBlank(page, threshold) {
|
||||||
const images = getImagesOnPage(page, PDFJS);
|
const images = await getImagesOnPage(page, PDFJS);
|
||||||
for (const image of images) {
|
for (const image of images) {
|
||||||
if(!isImageBlank(image, threshold))
|
if(!isImageBlank(image, threshold))
|
||||||
return false;
|
return false;
|
||||||
@ -37,7 +37,7 @@ export async function detectEmptyPages(snapshot, whiteThreashold, PDFJS, OpenCV)
|
|||||||
return true;
|
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);
|
const src = new OpenCV.cv.Mat(image.width, image.height, OpenCV.cv.CV_8UC4);
|
||||||
src.data.set(image.data);
|
src.data.set(image.data);
|
||||||
// Convert the image to grayscale
|
// Convert the image to grayscale
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import { detectEmptyPages } from "./shared/detectEmptyPages.js";
|
import { detectEmptyPages } from "./shared/detectEmptyPages.js";
|
||||||
import { getImagesOnPage } from "./shared/getImagesOnPage.js";
|
import { getImagesOnPage } from "./shared/getImagesOnPage.js";
|
||||||
|
import { createSubDocument } from "./shared/createSubDocument.js";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @typedef {"BAR_CODE"|"QR_CODE"|"BLANK_PAGE"} SplitType
|
* @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);
|
console.log("Split At Pages: ", splitAtPages);
|
||||||
|
|
||||||
|
// Remove detected Pages & Split
|
||||||
const pdfDoc = await PDFLib.PDFDocument.load(snapshot);
|
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) {
|
async function getPagesWithQRCode(snapshot) {
|
||||||
const pdfDoc = await PDFJS.getDocument(snapshot).promise;
|
const pdfDoc = await PDFJS.getDocument(snapshot).promise;
|
||||||
|
|
||||||
const pagesWithQR = [];
|
const pagesWithQR = [];
|
||||||
for (let i = 1; i <= pdfDoc.numPages; i++) {
|
for (let i = 0; i < pdfDoc.numPages; i++) {
|
||||||
const page = await pdfDoc.getPage(i);
|
const page = await pdfDoc.getPage(i + 1);
|
||||||
console.log("Checking page " + i);
|
console.log("Checking page " + i);
|
||||||
|
|
||||||
const images = await getImagesOnPage(page, PDFJS);
|
const images = await getImagesOnPage(page, PDFJS);
|
||||||
|
@ -93,8 +93,7 @@ export async function * traverseOperations(operations, input, Functions) {
|
|||||||
buffer: splitResult[j]
|
buffer: splitResult[j]
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
return splits;
|
||||||
input = splits;
|
|
||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
case "editMetadata":
|
case "editMetadata":
|
||||||
@ -117,8 +116,17 @@ export async function * traverseOperations(operations, input, Functions) {
|
|||||||
break;
|
break;
|
||||||
case "splitOn":
|
case "splitOn":
|
||||||
yield* oneToN(input, operation, async (input) => {
|
yield* oneToN(input, operation, async (input) => {
|
||||||
input.fileName += "_split";
|
const splitResult = await Functions.splitOn(input.buffer, operation.values["type"], operation.values["whiteThreashold"]);
|
||||||
input.buffer = 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;
|
break;
|
||||||
default:
|
default:
|
||||||
@ -138,13 +146,14 @@ export async function * traverseOperations(operations, input, Functions) {
|
|||||||
|
|
||||||
async function * oneToN(input, operation, callback) {
|
async function * oneToN(input, operation, callback) {
|
||||||
if(Array.isArray(input)) {
|
if(Array.isArray(input)) {
|
||||||
|
let output = [];
|
||||||
for (let i = 0; i < input.length; i++) {
|
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 {
|
else {
|
||||||
await callback(input);
|
input = await callback(input);
|
||||||
yield* nextOperation(operation.operations, input);
|
yield* nextOperation(operation.operations, input);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user