From 347aa1e37acadaa8621128f9ec3e7e7d5a1e245d Mon Sep 17 00:00:00 2001 From: yushulx Date: Mon, 6 Nov 2023 16:41:17 +0800 Subject: [PATCH] Added command line sample with sharp --- examples/command-line/package.json | 14 +++++++++++++ examples/command-line/test.js | 32 ++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 examples/command-line/package.json create mode 100644 examples/command-line/test.js diff --git a/examples/command-line/package.json b/examples/command-line/package.json new file mode 100644 index 0000000..7ad3435 --- /dev/null +++ b/examples/command-line/package.json @@ -0,0 +1,14 @@ +{ + "name": "command-line", + "version": "1.0.0", + "description": "", + "main": "test.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "ISC", + "dependencies": { + "sharp": "^0.32.6" + } +} diff --git a/examples/command-line/test.js b/examples/command-line/test.js new file mode 100644 index 0000000..929ca0e --- /dev/null +++ b/examples/command-line/test.js @@ -0,0 +1,32 @@ +const sharp = require('sharp'); +var DocRectifier = require('../../index.js'); + +const path = require('path'); +console.log(DocRectifier.getVersionNumber()); +DocRectifier.initLicense('DLS2eyJoYW5kc2hha2VDb2RlIjoiMjAwMDAxLTE2NDk4Mjk3OTI2MzUiLCJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSIsInNlc3Npb25QYXNzd29yZCI6IndTcGR6Vm05WDJrcEQ5YUoifQ=='); + +var obj = new DocRectifier(); +obj.setParameters(DocRectifier.Template.binary); + +sharp('../../images/document.png') + .raw() // This ensures the image data is returned as raw pixel data + .toBuffer((err, data, info) => { + if (err) throw err; + + const width = info.width; + const height = info.height; + const stride = info.size / info.height; + + (async function () { + try { + + let results = await obj.detectBufferAsync(data, width, height, stride); + console.log(results); + let result = results[0]; + result = await obj.normalizeBufferAsync(data, width, height, stride, result['x1'], result['y1'], result['x2'], result['y2'], result['x3'], result['y3'], result['x4'], result['y4']); + obj.save('test.png'); + } catch (error) { + console.log(error); + } + })(); + }); \ No newline at end of file