From 866d49b6bdeab74719795950fa228f103290731c Mon Sep 17 00:00:00 2001 From: yurii-scanbot Date: Tue, 1 Dec 2026 21:39:44 +0100 Subject: [PATCH 1/2] Update examples to Scanbot SDK 0.1100.3 and add straightening with prior corners Bump SDK from 0.1000.4 to 0.1100.3 for C, Java, Node.js and Python (CMakeLists.txt, build.gradle, package.json + lockfile, CI workflow, test-scripts README). Libraries.txt header updated to 11.0.0. Adapt to API changes in 0.1100.3: - DocumentCleanupResult no longer has a status (DocumentCleanupStatus removed); drop status output from the cleanup snippets in all languages. - C: scanbotsdk_document_straightening_parameters_create takes max corner error, partially-visible-documents mode and inpainted-mask flag; scanbotsdk_document_straightener_run no longer takes prior corners. - Java: DocumentStraightener.run(image, params) no longer takes a corners list. Add DocumentStraightener run-with-prior-corners example in all languages: - New command: enhance straighten_document_with_prior_corners - C straighten_document_with_prior_corners, Java runWithPriorCorners, Node.js runWithPriorCorners, Python run_with_prior_corners - Usage text and READMEs updated Tests: - Add "Document straighten with prior corners enhance" case to the Linux test scripts (C, Java, Node.js, Python) and Windows scripts (C, Python). --- .github/workflows/run-tests-ci.yml | 2 +- Libraries.txt | 2 +- examples/c/CMakeLists.txt | 2 +- examples/c/README.md | 1 + .../snippets/document/document_straightener.h | 1 + examples/c/src/main.c | 6 ++ .../src/snippets/document/document_cleanup.c | 16 ----- .../snippets/document/document_straightener.c | 58 ++++++++++++++++++- examples/c/src/utils/utils.c | 3 +- examples/java/README.md | 1 + examples/java/build.gradle | 2 +- .../io/scanbot/sdk/ScanbotSDKExample.java | 5 ++ .../document/DocumentCleanupSnippet.java | 2 - .../document/DocumentStraightenerSnippet.java | 22 ++++++- .../io/scanbot/sdk/utils/ExampleUsage.java | 3 +- examples/nodejs/README.md | 1 + examples/nodejs/package-lock.json | 8 +-- examples/nodejs/package.json | 2 +- examples/nodejs/src/index.ts | 5 ++ .../src/snippets/document/document-cleanup.ts | 2 - .../document/document-straightener.ts | 21 +++++++ .../src/snippets/utils/example-usage.ts | 3 +- examples/python/README.md | 1 + examples/python/main.py | 5 +- .../snippets/document/document_cleanup.py | 17 +++--- .../document/document_straightener.py | 21 +++++++ examples/python/utils.py | 3 +- test-scripts/README.md | 2 +- test-scripts/linux/test-c.sh | 2 + test-scripts/linux/test-java.sh | 2 + test-scripts/linux/test-nodejs.sh | 2 + test-scripts/linux/test-python.sh | 2 + test-scripts/windows/test-c.ps1 | 1 + test-scripts/windows/test-python.ps1 | 1 + 34 files changed, 178 insertions(+), 49 deletions(-) diff --git a/.github/workflows/run-tests-ci.yml b/.github/workflows/run-tests-ci.yml index 39eb4ce..ca8ef97 100644 --- a/.github/workflows/run-tests-ci.yml +++ b/.github/workflows/run-tests-ci.yml @@ -41,7 +41,7 @@ on: # Global environment variables env: - SDK_VERSION: '0.1000.4' + SDK_VERSION: '0.1100.3' jobs: test-x86_64: diff --git a/Libraries.txt b/Libraries.txt index 8415a17..2f1070c 100644 --- a/Libraries.txt +++ b/Libraries.txt @@ -1,4 +1,4 @@ -Open Source libraries used in the Scanbot Linux SDK version 10.0.0: +Open Source libraries used in the Scanbot Linux SDK version 11.0.0: +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ diff --git a/examples/c/CMakeLists.txt b/examples/c/CMakeLists.txt index 68447fa..d85eec5 100644 --- a/examples/c/CMakeLists.txt +++ b/examples/c/CMakeLists.txt @@ -5,7 +5,7 @@ set(CMAKE_C_STANDARD 99) # Find or download Scanbot SDK if(NOT SCANBOTSDK_VERSION) - set(SCANBOTSDK_VERSION "0.1000.4") + set(SCANBOTSDK_VERSION "0.1100.3") endif() set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake" ${CMAKE_MODULE_PATH}) find_package(ScanbotSDK REQUIRED) diff --git a/examples/c/README.md b/examples/c/README.md index a701b13..2ae2396 100644 --- a/examples/c/README.md +++ b/examples/c/README.md @@ -98,6 +98,7 @@ The example supports five modes: **scan**, **analyze**, **enhance**, **parse**, ./scanbotsdk_example analyze analyze_multi_page --file files/doc.pdf --license ./scanbotsdk_example analyze crop_analyze --file images/doc.jpg --save out/crop.jpg --license ./scanbotsdk_example enhance straighten_document --file images/doc.jpg --license +./scanbotsdk_example enhance straighten_document_with_prior_corners --file images/doc.jpg --license ./scanbotsdk_example enhance cleanup_document --file ../../test-scripts/test-images/highlighted.png --mask ../../test-scripts/test-images/highlighted_mask_001.png --save out/cleaned.jpg --license ./scanbotsdk_example parse mrz --text "P ./scanbotsdk_example live barcode --file images/example.jpg --license diff --git a/examples/c/include/snippets/document/document_straightener.h b/examples/c/include/snippets/document/document_straightener.h index 1c63bc7..7085c10 100644 --- a/examples/c/include/snippets/document/document_straightener.h +++ b/examples/c/include/snippets/document/document_straightener.h @@ -4,5 +4,6 @@ #include scanbotsdk_error_code_t straighten_document(scanbotsdk_image_t *image); +scanbotsdk_error_code_t straighten_document_with_prior_corners(scanbotsdk_image_t *image); #endif diff --git a/examples/c/src/main.c b/examples/c/src/main.c index 0f88f0d..b031762 100644 --- a/examples/c/src/main.c +++ b/examples/c/src/main.c @@ -81,6 +81,12 @@ int main(int argc, char *argv[]) { ec = straighten_document(image); } + else if (strcmp(command, "straighten_document_with_prior_corners") == 0) { + ec = load_image_from_path(file_path, &image); + if (ec != SCANBOTSDK_OK) goto cleanup; + + ec = straighten_document_with_prior_corners(image); + } else if (strcmp(command, "cleanup_document") == 0) { if (!mask_path) { print_usage(argv[0]); ec = SCANBOTSDK_ERROR_INVALID_ARGUMENT; goto cleanup; } diff --git a/examples/c/src/snippets/document/document_cleanup.c b/examples/c/src/snippets/document/document_cleanup.c index f141e5e..229f72d 100644 --- a/examples/c/src/snippets/document/document_cleanup.c +++ b/examples/c/src/snippets/document/document_cleanup.c @@ -12,8 +12,6 @@ scanbotsdk_error_code_t document_cleanup( scanbotsdk_document_cleanup_configuration_t *configuration = NULL; scanbotsdk_document_cleanup_t *cleanup = NULL; scanbotsdk_document_cleanup_result_t *result = NULL; - scanbotsdk_document_cleanup_status_t status; - const char *status_str = NULL; scanbotsdk_image_t *cleaned_image = NULL; scanbotsdk_image_info_t *original_info = NULL; @@ -43,20 +41,6 @@ scanbotsdk_error_code_t document_cleanup( goto cleanup; } - ec = scanbotsdk_document_cleanup_result_get_status(result, &status); - if (ec != SCANBOTSDK_OK) { - fprintf(stderr, "document_cleanup_result_get_status: %d: %s\n", ec, error_message(ec)); - goto cleanup; - } - - ec = scanbotsdk_document_cleanup_status_t_to_string(status, &status_str); - if (ec != SCANBOTSDK_OK) { - fprintf(stderr, "document_cleanup_status_t_to_string: %d: %s\n", ec, error_message(ec)); - goto cleanup; - } - - printf("Document cleanup status: %s\n", status_str); - ec = scanbotsdk_image_info(image, &original_info); if (ec != SCANBOTSDK_OK) { fprintf(stderr, "image_info(original): %d: %s\n", ec, error_message(ec)); diff --git a/examples/c/src/snippets/document/document_straightener.c b/examples/c/src/snippets/document/document_straightener.c index d09d79d..497a6b5 100644 --- a/examples/c/src/snippets/document/document_straightener.c +++ b/examples/c/src/snippets/document/document_straightener.c @@ -27,9 +27,12 @@ scanbotsdk_error_code_t straighten_document(scanbotsdk_image_t *image) { if (ec != SCANBOTSDK_OK) { fprintf(stderr, "aspect_ratio_create(3:4): %d: %s\n", ec, error_message(ec)); goto cleanup; } ec = scanbotsdk_document_straightening_parameters_create( + 0.03, SCANBOTSDK_DOCUMENT_STRAIGHTENING_MODE_STRAIGHTEN, aspect_ratios, 4, + SCANBOTSDK_PARTIALLY_VISIBLE_DOCUMENTS_HANDLING_MODE_INPAINTING, + false, &straightening_params ); if (ec != SCANBOTSDK_OK) { fprintf(stderr, "document_straightening_parameters_create: %d: %s\n", ec, error_message(ec)); goto cleanup; } @@ -41,8 +44,6 @@ scanbotsdk_error_code_t straighten_document(scanbotsdk_image_t *image) { straightener, image, straightening_params, - NULL, - 0, &result ); if (ec != SCANBOTSDK_OK) { fprintf(stderr, "document_straighten: %d: %s\n", ec, error_message(ec)); goto cleanup; } @@ -67,4 +68,55 @@ scanbotsdk_error_code_t straighten_document(scanbotsdk_image_t *image) { scanbotsdk_aspect_ratio_free(aspect_ratios[3]); return ec; -} \ No newline at end of file +} + +scanbotsdk_error_code_t straighten_document_with_prior_corners(scanbotsdk_image_t *image) { + scanbotsdk_error_code_t ec = SCANBOTSDK_OK; + + scanbotsdk_document_straightening_result_t *result = NULL; + scanbotsdk_document_straightening_parameters_t *straightening_params = NULL; + scanbotsdk_document_straightener_t *straightener = NULL; + scanbotsdk_image_t *straightened_image = NULL; + + // Apriori known document corners, normalized to [0, 1] and given clockwise: + // top-left, top-right, bottom-right, bottom-left. + // Provide them only if known with high confidence (e.g. from a user interaction). + scanbotsdk_pointf_t prior_corners[4] = { + {0.05f, 0.05f}, + {0.95f, 0.05f}, + {0.95f, 0.95f}, + {0.05f, 0.95f} + }; + + ec = scanbotsdk_document_straightening_parameters_create_with_defaults(&straightening_params); + if (ec != SCANBOTSDK_OK) { fprintf(stderr, "document_straightening_parameters_create_with_defaults: %d: %s\n", ec, error_message(ec)); goto cleanup; } + + ec = scanbotsdk_document_straightener_create(&straightener); + if (ec != SCANBOTSDK_OK) { fprintf(stderr, "document_straightener_create: %d: %s\n", ec, error_message(ec)); goto cleanup; } + + ec = scanbotsdk_document_straightener_run_with_prior_corners( + straightener, + image, + prior_corners, + 4, + straightening_params, + &result + ); + if (ec != SCANBOTSDK_OK) { fprintf(stderr, "document_straighten_with_prior_corners: %d: %s\n", ec, error_message(ec)); goto cleanup; } + + ec = scanbotsdk_document_straightening_result_get_straightened_image(result, &straightened_image); + if (ec != SCANBOTSDK_OK) { fprintf(stderr, "document_straightening_result_get_straightened_image: %d: %s\n", ec, error_message(ec)); goto cleanup; } + + if (straightened_image == NULL) { + fprintf(stderr, "No straightened image returned.\n"); + } + + /* straightened_image can be saved or processed further here */ + +cleanup: + scanbotsdk_document_straightener_free(straightener); + scanbotsdk_document_straightening_result_free(result); + scanbotsdk_document_straightening_parameters_free(straightening_params); + + return ec; +} diff --git a/examples/c/src/utils/utils.c b/examples/c/src/utils/utils.c index 425492d..0d53f48 100644 --- a/examples/c/src/utils/utils.c +++ b/examples/c/src/utils/utils.c @@ -103,7 +103,7 @@ void print_usage(const char *prog) { printf(" medical_certificate | mrz | ocr | text_pattern | vin\n\n"); printf("Available enhance commands:\n"); - printf(" straighten_document | cleanup_document\n\n"); + printf(" straighten_document | straighten_document_with_prior_corners | cleanup_document\n\n"); printf("Available analyze commands:\n"); printf(" analyze_multi_page | crop_analyze\n\n"); @@ -132,6 +132,7 @@ void print_usage(const char *prog) { printf(" %s analyze analyze_multi_page --file files/doc.pdf --license \n", prog); printf(" %s analyze crop_analyze --file images/doc.jpg --save out/crop.jpg --license \n", prog); printf(" %s enhance straighten_document --file images/doc.jpg --license \n", prog); + printf(" %s enhance straighten_document_with_prior_corners --file images/doc.jpg --license \n", prog); printf(" %s enhance cleanup_document --file ../../test-scripts/test-images/highlighted.png --mask ../../test-scripts/test-images/highlighted_mask_001.png --save out/cleaned.jpg --license \n", prog); printf(" %s parse mrz --text \"P\n", prog); printf(" %s live barcode --file images/example.jpg --license \n", prog); diff --git a/examples/java/README.md b/examples/java/README.md index d52cdc0..62dc302 100644 --- a/examples/java/README.md +++ b/examples/java/README.md @@ -25,6 +25,7 @@ The example supports four commands: **scan**, **analyze**, **enhance**, and **pa ./gradlew run --args='analyze analyze_multi_page --resource files/doc.pdf --license ' ./gradlew run --args='analyze crop_analyze --file images/doc.jpg --save out/crop.jpg --license ' ./gradlew run --args='enhance straighten_document --file images/doc.jpg --license ' +./gradlew run --args='enhance straighten_document_with_prior_corners --file images/doc.jpg --license ' ./gradlew run --args='enhance cleanup_document --file ../../test-scripts/test-images/highlighted.png --mask ../../test-scripts/test-images/highlighted_mask_001.png --save out/cleaned.jpg --license ' ./gradlew run --args='parse mrz --text "P' ``` diff --git a/examples/java/build.gradle b/examples/java/build.gradle index beaed37..06a0b20 100644 --- a/examples/java/build.gradle +++ b/examples/java/build.gradle @@ -4,7 +4,7 @@ plugins { } // TODO Add your SCANBOTSDK_VERSION here. -def SCANBOTSDK_VERSION = "0.1000.4" +def SCANBOTSDK_VERSION = "0.1100.3" def arch = System.getProperty("os.arch") def SCANBOTSDK_ARCHITECTURE = (arch.contains("arm") || arch.contains("aarch64")) ? "aarch64" : "x86_64" diff --git a/examples/java/src/main/java/io/scanbot/sdk/ScanbotSDKExample.java b/examples/java/src/main/java/io/scanbot/sdk/ScanbotSDKExample.java index a34754e..5e79d6e 100644 --- a/examples/java/src/main/java/io/scanbot/sdk/ScanbotSDKExample.java +++ b/examples/java/src/main/java/io/scanbot/sdk/ScanbotSDKExample.java @@ -83,6 +83,11 @@ public static void main(String[] args) throws Exception { DocumentStraightenerSnippet.run(image); } break; + case "straighten_document_with_prior_corners": + try (ImageRef image = Utils.createImageRef(file, resource)) { + DocumentStraightenerSnippet.runWithPriorCorners(image); + } + break; case "cleanup_document": if (mask == null && maskResource == null) { ExampleUsage.print(); return; } DocumentCleanupSnippet.run(file, resource, mask, maskResource, save); diff --git a/examples/java/src/main/java/io/scanbot/sdk/snippets/document/DocumentCleanupSnippet.java b/examples/java/src/main/java/io/scanbot/sdk/snippets/document/DocumentCleanupSnippet.java index cf5edfc..1a4e3f6 100644 --- a/examples/java/src/main/java/io/scanbot/sdk/snippets/document/DocumentCleanupSnippet.java +++ b/examples/java/src/main/java/io/scanbot/sdk/snippets/document/DocumentCleanupSnippet.java @@ -22,8 +22,6 @@ public static void run( DocumentCleanup cleanup = new DocumentCleanup(image, new DocumentCleanupConfiguration()); DocumentCleanupResult result = cleanup.run(mask) ) { - System.out.println("Document cleanup status: " + result.getStatus()); - ImageInfo originalInfo = image.imageInfo(); ImageInfo cleanedInfo = result.getImage().imageInfo(); System.out.printf("Original WxH: %dx%d%n", originalInfo.getWidth(), originalInfo.getHeight()); diff --git a/examples/java/src/main/java/io/scanbot/sdk/snippets/document/DocumentStraightenerSnippet.java b/examples/java/src/main/java/io/scanbot/sdk/snippets/document/DocumentStraightenerSnippet.java index b867c62..24399ef 100644 --- a/examples/java/src/main/java/io/scanbot/sdk/snippets/document/DocumentStraightenerSnippet.java +++ b/examples/java/src/main/java/io/scanbot/sdk/snippets/document/DocumentStraightenerSnippet.java @@ -6,6 +6,7 @@ import io.scanbot.sdk.documentscanner.DocumentStraighteningParameters; import io.scanbot.sdk.documentscanner.DocumentStraighteningResult; import io.scanbot.sdk.geometry.AspectRatio; +import io.scanbot.sdk.geometry.PointF; import io.scanbot.sdk.image.ImageRef; public class DocumentStraightenerSnippet { @@ -21,7 +22,26 @@ public static void run(ImageRef image) throws Exception { try ( DocumentStraightener straightener = new DocumentStraightener(); - DocumentStraighteningResult result = straightener.run(image, params, List.of()) + DocumentStraighteningResult result = straightener.run(image, params) + ) { + // The straightened image can be accessed via result.getStraightenedImage() and saved or further processed as needed. + } + } + + public static void runWithPriorCorners(ImageRef image) throws Exception { + // Apriori known document corners, normalized to [0, 1] and given clockwise: + // top-left, top-right, bottom-right, bottom-left. + // Provide them only if known with high confidence (e.g. from a user interaction). + List priorCorners = List.of( + new PointF(0.05f, 0.05f), + new PointF(0.95f, 0.05f), + new PointF(0.95f, 0.95f), + new PointF(0.05f, 0.95f) + ); + + try ( + DocumentStraightener straightener = new DocumentStraightener(); + DocumentStraighteningResult result = straightener.runWithPriorCorners(image, priorCorners, new DocumentStraighteningParameters()) ) { // The straightened image can be accessed via result.getStraightenedImage() and saved or further processed as needed. } diff --git a/examples/java/src/main/java/io/scanbot/sdk/utils/ExampleUsage.java b/examples/java/src/main/java/io/scanbot/sdk/utils/ExampleUsage.java index 6471268..73a8e1b 100644 --- a/examples/java/src/main/java/io/scanbot/sdk/utils/ExampleUsage.java +++ b/examples/java/src/main/java/io/scanbot/sdk/utils/ExampleUsage.java @@ -26,7 +26,7 @@ public static void print() { System.out.println(" analyze_multi_page | crop_analyze"); System.out.println(); System.out.println("Available enhance commands:"); - System.out.println(" straighten_document | cleanup_document"); + System.out.println(" straighten_document | straighten_document_with_prior_corners | cleanup_document"); System.out.println(); System.out.println("Available parse commands:"); System.out.println(" mrz | barcode_doc"); @@ -41,6 +41,7 @@ public static void print() { System.out.println(" ./gradlew run --args='analyze analyze_multi_page --resource files/doc.pdf --license '"); System.out.println(" ./gradlew run --args='analyze crop_analyze --file images/doc.jpg --save out/crop.jpg --license '"); System.out.println(" ./gradlew run --args='enhance straighten_document --file images/doc.jpg --license '"); + System.out.println(" ./gradlew run --args='enhance straighten_document_with_prior_corners --file images/doc.jpg --license '"); System.out.println(" ./gradlew run --args='enhance cleanup_document --file ../../test-scripts/test-images/highlighted.png --mask ../../test-scripts/test-images/highlighted_mask_001.png --save out/cleaned.jpg --license '"); System.out.println(" ./gradlew run --args='parse mrz --text \"P'"); System.out.println(); diff --git a/examples/nodejs/README.md b/examples/nodejs/README.md index 00f92d0..3841014 100644 --- a/examples/nodejs/README.md +++ b/examples/nodejs/README.md @@ -30,6 +30,7 @@ npx ts-node src/index.ts scan barcode --file images/example.jpg --license npx ts-node src/index.ts analyze analyze_multi_page --file files/doc.pdf --license npx ts-node src/index.ts analyze crop_analyze --file images/doc.jpg --save out/crop.jpg --license npx ts-node src/index.ts enhance straighten_document --file images/doc.jpg --license +npx ts-node src/index.ts enhance straighten_document_with_prior_corners --file images/doc.jpg --license npx ts-node src/index.ts enhance cleanup_document --file ../../test-scripts/test-images/highlighted.png --mask ../../test-scripts/test-images/highlighted_mask_001.png --save out/cleaned.jpg --license npx ts-node src/index.ts parse mrz --text "P ``` diff --git a/examples/nodejs/package-lock.json b/examples/nodejs/package-lock.json index d182673..602111d 100644 --- a/examples/nodejs/package-lock.json +++ b/examples/nodejs/package-lock.json @@ -9,7 +9,7 @@ "version": "1.0.0", "license": "ISC", "dependencies": { - "scanbotsdk": "https://github.com/doo/scanbot-sdk-example-linux/releases/download/standalone-sdk%2Fv0.1000.4/nodejs-scanbotsdk-0.1000.4.tgz" + "scanbotsdk": "https://github.com/doo/scanbot-sdk-example-linux/releases/download/standalone-sdk%2Fv0.1100.3/nodejs-scanbotsdk-0.1100.3.tgz" }, "devDependencies": { "@types/node": "^24.3.0", @@ -154,9 +154,9 @@ "license": "ISC" }, "node_modules/scanbotsdk": { - "version": "0.1000.4", - "resolved": "https://github.com/doo/scanbot-sdk-example-linux/releases/download/standalone-sdk%2Fv0.1000.4/nodejs-scanbotsdk-0.1000.4.tgz", - "integrity": "sha512-ssPoOUqNE+mC/ALfG6UDCZ+daEnfuXDVnBt5y4V/lc/Yl+9Jeu3EW9/m2RvwTa+dokj5+RQhwqyPxQk3Nl9u+A==", + "version": "0.1100.3", + "resolved": "https://github.com/doo/scanbot-sdk-example-linux/releases/download/standalone-sdk%2Fv0.1100.3/nodejs-scanbotsdk-0.1100.3.tgz", + "integrity": "sha512-v0dZ6nTQJQc1copZVnVbp4Jn6n8LittzdInrxdCfmmRUQOHYn89I2J4RCZiA4ay3w+EEJ1UT7K5/cGul7e2yzw==", "hasInstallScript": true, "license": "Commercial", "os": [ diff --git a/examples/nodejs/package.json b/examples/nodejs/package.json index 107aabf..a2721c3 100644 --- a/examples/nodejs/package.json +++ b/examples/nodejs/package.json @@ -15,6 +15,6 @@ "typescript": "^5.9.2" }, "dependencies": { - "scanbotsdk": "https://github.com/doo/scanbot-sdk-example-linux/releases/download/standalone-sdk%2Fv0.1000.4/nodejs-scanbotsdk-0.1000.4.tgz" + "scanbotsdk": "https://github.com/doo/scanbot-sdk-example-linux/releases/download/standalone-sdk%2Fv0.1100.3/nodejs-scanbotsdk-0.1100.3.tgz" } } diff --git a/examples/nodejs/src/index.ts b/examples/nodejs/src/index.ts index 676b5d4..f6a3d7e 100644 --- a/examples/nodejs/src/index.ts +++ b/examples/nodejs/src/index.ts @@ -105,6 +105,11 @@ async function main(): Promise { await DocumentStraightenerSnippet.run(image); break; } + case "straighten_document_with_prior_corners": { + const image = await ScanbotSDK.ImageRef.fromPath(file); + await DocumentStraightenerSnippet.runWithPriorCorners(image); + break; + } case "cleanup_document": if (!mask) { printUsage(); return; } await DocumentCleanupSnippet.run(file, mask, save); diff --git a/examples/nodejs/src/snippets/document/document-cleanup.ts b/examples/nodejs/src/snippets/document/document-cleanup.ts index e798e9e..6777441 100644 --- a/examples/nodejs/src/snippets/document/document-cleanup.ts +++ b/examples/nodejs/src/snippets/document/document-cleanup.ts @@ -10,8 +10,6 @@ export class DocumentCleanupSnippet { ); await using result = await cleanup.run(mask); - console.log("Document cleanup status: " + result.status); - const originalInfo = await image.imageInfo(); const cleanedInfo = await result.image.imageInfo(); diff --git a/examples/nodejs/src/snippets/document/document-straightener.ts b/examples/nodejs/src/snippets/document/document-straightener.ts index ed8a4c5..532c505 100644 --- a/examples/nodejs/src/snippets/document/document-straightener.ts +++ b/examples/nodejs/src/snippets/document/document-straightener.ts @@ -19,4 +19,25 @@ export class DocumentStraightenerSnippet { // The straightened image can be accessed via result.straightenedImage and saved or further processed as needed. } + + public static async runWithPriorCorners(image: ScanbotSDK.ImageRef): Promise { + // Apriori known document corners, normalized to [0, 1] and given clockwise: + // top-left, top-right, bottom-right, bottom-left. + // Provide them only if known with high confidence (e.g. from a user interaction). + const priorCorners = [ + { x: 0.05, y: 0.05 }, + { x: 0.95, y: 0.05 }, + { x: 0.95, y: 0.95 }, + { x: 0.05, y: 0.95 } + ]; + + await using straightener = await ScanbotSDK.DocumentStraightener.create(); + await using result = await straightener.runWithPriorCorners( + image, + priorCorners, + new ScanbotSDK.DocumentStraighteningParameters() + ); + + // The straightened image can be accessed via result.straightenedImage and saved or further processed as needed. + } } diff --git a/examples/nodejs/src/snippets/utils/example-usage.ts b/examples/nodejs/src/snippets/utils/example-usage.ts index d568f67..8517e5c 100644 --- a/examples/nodejs/src/snippets/utils/example-usage.ts +++ b/examples/nodejs/src/snippets/utils/example-usage.ts @@ -7,7 +7,7 @@ Usage: Categories & subcommands: scan analyze - enhance + enhance parse Flags: @@ -23,6 +23,7 @@ Examples: npx ts-node src/index.ts analyze analyze_multi_page --file files/doc.pdf --license npx ts-node src/index.ts analyze crop_analyze --file images/doc.jpg --save out/crop.jpg --license npx ts-node src/index.ts enhance straighten_document --file images/doc.jpg --license + npx ts-node src/index.ts enhance straighten_document_with_prior_corners --file images/doc.jpg --license npx ts-node src/index.ts enhance cleanup_document --file ../../test-scripts/test-images/highlighted.png --mask ../../test-scripts/test-images/highlighted_mask_001.png --save out/cleaned.jpg --license npx ts-node src/index.ts parse mrz --text "P `); diff --git a/examples/python/README.md b/examples/python/README.md index eabba85..b548ac1 100644 --- a/examples/python/README.md +++ b/examples/python/README.md @@ -147,6 +147,7 @@ python main.py scan barcode --file images/example.jpg --license python main.py analyze analyze_multi_page --file files/doc.pdf --license python main.py analyze crop_analyze --file images/doc.jpg --save out/crop.jpg --license python main.py enhance straighten_document --file images/doc.jpg --license +python main.py enhance straighten_document_with_prior_corners --file images/doc.jpg --license python main.py enhance cleanup_document --file ../../test-scripts/test-images/highlighted.png --mask ../../test-scripts/test-images/highlighted_mask_001.png --save out/cleaned.jpg --license python main.py parse mrz --text "P python main.py live barcode --device "0" --license diff --git a/examples/python/main.py b/examples/python/main.py index 30ed29b..03f2982 100644 --- a/examples/python/main.py +++ b/examples/python/main.py @@ -1,7 +1,7 @@ import sys import scanbotsdk -from snippets.document.document_straightener import straighten_document +from snippets.document.document_straightener import straighten_document, straighten_document_with_prior_corners from snippets.document.analyze_multi_page import analyze_multi_page from snippets.document.crop_and_analyze import crop_and_analyze from snippets.document.document_cleanup import cleanup_document @@ -74,6 +74,9 @@ def main(): if subcommand == "straighten_document": with create_image_ref(file_path) as image: straighten_document(image) + elif subcommand == "straighten_document_with_prior_corners": + with create_image_ref(file_path) as image: + straighten_document_with_prior_corners(image) elif subcommand == "cleanup_document": if not mask_path: print_usage(); return cleanup_document(file_path, mask_path, save_path) diff --git a/examples/python/snippets/document/document_cleanup.py b/examples/python/snippets/document/document_cleanup.py index a0ce48d..2ec2739 100644 --- a/examples/python/snippets/document/document_cleanup.py +++ b/examples/python/snippets/document/document_cleanup.py @@ -13,14 +13,11 @@ def cleanup_document(image_path: str, mask_path: str, save_path: Optional[str] = ) result = cleanup.run(mask=mask) - print(f"Document cleanup status: {result.status.name}") + original_info = image.info() + cleaned_info = result.image.info() + print(f"Original WxH: {original_info.width}x{original_info.height}") + print(f"Cleaned WxH: {cleaned_info.width}x{cleaned_info.height}") - if result.status == DocumentCleanupStatus.OK: - original_info = image.info() - cleaned_info = result.image.info() - print(f"Original WxH: {original_info.width}x{original_info.height}") - print(f"Cleaned WxH: {cleaned_info.width}x{cleaned_info.height}") - - if save_path: - result.image.save_image(save_path, SaveImageOptions()) - print(f"Saved cleaned image to: {os.path.abspath(save_path)}") + if save_path: + result.image.save_image(save_path, SaveImageOptions()) + print(f"Saved cleaned image to: {os.path.abspath(save_path)}") diff --git a/examples/python/snippets/document/document_straightener.py b/examples/python/snippets/document/document_straightener.py index bfbc0d2..1b7ed55 100644 --- a/examples/python/snippets/document/document_straightener.py +++ b/examples/python/snippets/document/document_straightener.py @@ -14,3 +14,24 @@ def straighten_document(image: ImageRef): result: DocumentStraighteningResult = straightener.run(image=image, parameters=params) # The straightened image can be accessed via result.straightened_image and saved or further processed as needed. + + +def straighten_document_with_prior_corners(image: ImageRef): + # Apriori known document corners, normalized to [0, 1] and given clockwise: + # top-left, top-right, bottom-right, bottom-left. + # Provide them only if known with high confidence (e.g. from a user interaction). + prior_corners = [ + PointF(x=0.05, y=0.05), + PointF(x=0.95, y=0.05), + PointF(x=0.95, y=0.95), + PointF(x=0.05, y=0.95), + ] + + straightener = DocumentStraightener() + result: DocumentStraighteningResult = straightener.run_with_prior_corners( + image=image, + prior_corners_normalized=prior_corners, + parameters=DocumentStraighteningParameters(), + ) + + # The straightened image can be accessed via result.straightened_image and saved or further processed as needed. diff --git a/examples/python/utils.py b/examples/python/utils.py index e5dff9f..57093b8 100644 --- a/examples/python/utils.py +++ b/examples/python/utils.py @@ -68,7 +68,7 @@ def print_usage(): print("Available analyze commands:") print(" analyze_multi_page | crop_analyze\n") print("Available enhance commands:") - print(" straighten_document | cleanup_document\n") + print(" straighten_document | straighten_document_with_prior_corners | cleanup_document\n") print("Available parse commands:") print(" mrz | barcode_doc\n") print("Available live commands:") @@ -86,6 +86,7 @@ def print_usage(): print(" python main.py analyze analyze_multi_page --file files/doc.pdf --license ") print(" python main.py analyze crop_analyze --file images/doc.jpg --save out/crop.jpg --license ") print(" python main.py enhance straighten_document --file images/doc.jpg --license ") + print(" python main.py enhance straighten_document_with_prior_corners --file images/doc.jpg --license ") print(" python main.py enhance cleanup_document --file ../../test-scripts/test-images/highlighted.png --mask ../../test-scripts/test-images/highlighted_mask_001.png --save out/cleaned.jpg --license ") print(" python main.py parse mrz --text \"P") print(" python main.py live barcode --device jetson_csi --license ") diff --git a/test-scripts/README.md b/test-scripts/README.md index 57abf1c..dc9b9b7 100644 --- a/test-scripts/README.md +++ b/test-scripts/README.md @@ -36,7 +36,7 @@ test-scripts/ ```bash export SCANBOT_LICENSE="your-license-key-here" -export SDK_VERSION=0.1000.4 +export SDK_VERSION=0.1100.3 ``` ### 2. Build Test Container diff --git a/test-scripts/linux/test-c.sh b/test-scripts/linux/test-c.sh index 9760d60..f77939b 100755 --- a/test-scripts/linux/test-c.sh +++ b/test-scripts/linux/test-c.sh @@ -47,6 +47,7 @@ commands=( "scan text_pattern --file ../../test-scripts/test-images/Document.jpeg --license \"${SCANBOT_LICENSE}\"" "scan vin --file ../../test-scripts/test-images/VIN.jpeg --license \"${SCANBOT_LICENSE}\"" "enhance straighten_document --file ../../test-scripts/test-images/Document.jpeg --license \"${SCANBOT_LICENSE}\"" + "enhance straighten_document_with_prior_corners --file ../../test-scripts/test-images/Document.jpeg --license \"${SCANBOT_LICENSE}\"" "analyze analyze_multi_page --file ../../test-scripts/test-images/multi_page_document.pdf --save /tmp/out.pdf --license \"${SCANBOT_LICENSE}\"" "analyze crop_analyze --file ../../test-scripts/test-images/Document.jpeg --save /tmp/crop.jpeg --license \"${SCANBOT_LICENSE}\"" "enhance cleanup_document --file ../../test-scripts/test-images/highlighted.png --mask ../../test-scripts/test-images/highlighted_mask_001.png --save /tmp/highlighted_cleaned.png --license \"${SCANBOT_LICENSE}\"" @@ -68,6 +69,7 @@ command_names=( "Text pattern scan" "VIN scan" "Document straighten enhance" + "Document straighten with prior corners enhance" "Multi-page analyze" "Crop analyze" "Document cleanup (highlighted)" diff --git a/test-scripts/linux/test-java.sh b/test-scripts/linux/test-java.sh index 56fccd8..dab8c30 100755 --- a/test-scripts/linux/test-java.sh +++ b/test-scripts/linux/test-java.sh @@ -34,6 +34,7 @@ commands=( "scan text_pattern --file ../../test-scripts/test-images/Document.jpeg --license \"${SCANBOT_LICENSE}\"" "scan vin --file ../../test-scripts/test-images/VIN.jpeg --license \"${SCANBOT_LICENSE}\"" "enhance straighten_document --file ../../test-scripts/test-images/Document.jpeg --license \"${SCANBOT_LICENSE}\"" + "enhance straighten_document_with_prior_corners --file ../../test-scripts/test-images/Document.jpeg --license \"${SCANBOT_LICENSE}\"" "analyze analyze_multi_page --file ../../test-scripts/test-images/multi_page_document.pdf --save /tmp/out.pdf --license \"${SCANBOT_LICENSE}\"" "analyze crop_analyze --file ../../test-scripts/test-images/Document.jpeg --save /tmp/crop.jpeg --license \"${SCANBOT_LICENSE}\"" "enhance cleanup_document --file ../../test-scripts/test-images/highlighted.png --mask ../../test-scripts/test-images/highlighted_mask_001.png --save /tmp/highlighted_cleaned.png --license \"${SCANBOT_LICENSE}\"" @@ -55,6 +56,7 @@ command_names=( "Text pattern scan" "VIN scan" "Document straighten enhance" + "Document straighten with prior corners enhance" "Multi-page analyze" "Crop analyze" "Document cleanup (highlighted)" diff --git a/test-scripts/linux/test-nodejs.sh b/test-scripts/linux/test-nodejs.sh index 0ee7d0c..6eac619 100755 --- a/test-scripts/linux/test-nodejs.sh +++ b/test-scripts/linux/test-nodejs.sh @@ -45,6 +45,7 @@ commands=( "scan text_pattern --file ../../test-scripts/test-images/Document.jpeg --license \"${SCANBOT_LICENSE}\"" "scan vin --file ../../test-scripts/test-images/VIN.jpeg --license \"${SCANBOT_LICENSE}\"" "enhance straighten_document --file ../../test-scripts/test-images/Document.jpeg --license \"${SCANBOT_LICENSE}\"" + "enhance straighten_document_with_prior_corners --file ../../test-scripts/test-images/Document.jpeg --license \"${SCANBOT_LICENSE}\"" "analyze analyze_multi_page --file ../../test-scripts/test-images/multi_page_document.pdf --save /tmp/out.pdf --license \"${SCANBOT_LICENSE}\"" "analyze crop_analyze --file ../../test-scripts/test-images/Document.jpeg --save /tmp/crop.jpeg --license \"${SCANBOT_LICENSE}\"" "enhance cleanup_document --file ../../test-scripts/test-images/highlighted.png --mask ../../test-scripts/test-images/highlighted_mask_001.png --save /tmp/highlighted_cleaned.png --license \"${SCANBOT_LICENSE}\"" @@ -66,6 +67,7 @@ command_names=( "Text pattern scan" "VIN scan" "Document straighten enhance" + "Document straighten with prior corners enhance" "Multi-page analyze" "Crop analyze" "Document cleanup (highlighted)" diff --git a/test-scripts/linux/test-python.sh b/test-scripts/linux/test-python.sh index cc30313..b19835e 100755 --- a/test-scripts/linux/test-python.sh +++ b/test-scripts/linux/test-python.sh @@ -34,6 +34,7 @@ commands=( "scan text_pattern --file ../../test-scripts/test-images/Document.jpeg --license \"${SCANBOT_LICENSE}\"" "scan vin --file ../../test-scripts/test-images/VIN.jpeg --license \"${SCANBOT_LICENSE}\"" "enhance straighten_document --file ../../test-scripts/test-images/Document.jpeg --license \"${SCANBOT_LICENSE}\"" + "enhance straighten_document_with_prior_corners --file ../../test-scripts/test-images/Document.jpeg --license \"${SCANBOT_LICENSE}\"" "analyze analyze_multi_page --file ../../test-scripts/test-images/multi_page_document.pdf --save /tmp/out.pdf --license \"${SCANBOT_LICENSE}\"" "analyze crop_analyze --file ../../test-scripts/test-images/Document.jpeg --save /tmp/crop.jpeg --license \"${SCANBOT_LICENSE}\"" "enhance cleanup_document --file ../../test-scripts/test-images/highlighted.png --mask ../../test-scripts/test-images/highlighted_mask_001.png --save /tmp/highlighted_cleaned.png --license \"${SCANBOT_LICENSE}\"" @@ -55,6 +56,7 @@ command_names=( "Text pattern scan" "VIN scan" "Document straighten enhance" + "Document straighten with prior corners enhance" "Multi-page analyze" "Crop analyze" "Document cleanup (highlighted)" diff --git a/test-scripts/windows/test-c.ps1 b/test-scripts/windows/test-c.ps1 index 2a7ebc5..9e00fd2 100755 --- a/test-scripts/windows/test-c.ps1 +++ b/test-scripts/windows/test-c.ps1 @@ -79,6 +79,7 @@ $commands = @( @{ Name = 'Text pattern scan'; Args = @('scan', 'text_pattern', '--file', '../../test-scripts/test-images/Document.jpeg', '--license', $env:SCANBOT_LICENSE) }, @{ Name = 'VIN scan'; Args = @('scan', 'vin', '--file', '../../test-scripts/test-images/VIN.jpeg', '--license', $env:SCANBOT_LICENSE) }, @{ Name = 'Document straighten enhance'; Args = @('enhance', 'straighten_document', '--file', '../../test-scripts/test-images/Document.jpeg', '--license', $env:SCANBOT_LICENSE) }, + @{ Name = 'Document straighten with prior corners enhance'; Args = @('enhance', 'straighten_document_with_prior_corners', '--file', '../../test-scripts/test-images/Document.jpeg', '--license', $env:SCANBOT_LICENSE) }, @{ Name = 'Document cleanup (highlighted)'; Args = @('enhance', 'cleanup_document', '--file', '../../test-scripts/test-images/highlighted.png', '--mask', '../../test-scripts/test-images/highlighted_mask_001.png', '--save', '/tmp/highlighted_cleaned.png', '--license', $env:SCANBOT_LICENSE) }, @{ Name = 'Multi-page analyze'; Args = @('analyze', 'analyze_multi_page', '--file', '../../test-scripts/test-images/multi_page_document.pdf', '--license', $env:SCANBOT_LICENSE) }, @{ Name = 'Crop analyze'; Args = @('analyze', 'crop_analyze', '--file', '../../test-scripts/test-images/Document.jpeg', '--license', $env:SCANBOT_LICENSE) }, diff --git a/test-scripts/windows/test-python.ps1 b/test-scripts/windows/test-python.ps1 index 915b6c7..a14656f 100755 --- a/test-scripts/windows/test-python.ps1 +++ b/test-scripts/windows/test-python.ps1 @@ -78,6 +78,7 @@ $commands = @( @{ Name = 'Text pattern scan'; Args = @('main.py', 'scan', 'text_pattern', '--file', '../../test-scripts/test-images/Document.jpeg', '--license', $env:SCANBOT_LICENSE) }, @{ Name = 'VIN scan'; Args = @('main.py', 'scan', 'vin', '--file', '../../test-scripts/test-images/VIN.jpeg', '--license', $env:SCANBOT_LICENSE) }, @{ Name = 'Document straighten enhance'; Args = @('main.py', 'enhance', 'straighten_document', '--file', '../../test-scripts/test-images/Document.jpeg', '--license', $env:SCANBOT_LICENSE) }, + @{ Name = 'Document straighten with prior corners enhance'; Args = @('main.py', 'enhance', 'straighten_document_with_prior_corners', '--file', '../../test-scripts/test-images/Document.jpeg', '--license', $env:SCANBOT_LICENSE) }, @{ Name = 'Document cleanup (highlighted)'; Args = @('main.py', 'enhance', 'cleanup_document', '--file', '../../test-scripts/test-images/highlighted.png', '--mask', '../../test-scripts/test-images/highlighted_mask_001.png', '--save', '/tmp/highlighted_cleaned.png', '--license', $env:SCANBOT_LICENSE) }, @{ Name = 'Multi-page analyze'; Args = @('main.py', 'analyze', 'analyze_multi_page', '--file', '../../test-scripts/test-images/multi_page_document.pdf', '--license', $env:SCANBOT_LICENSE) }, @{ Name = 'Crop analyze'; Args = @('main.py', 'analyze', 'crop_analyze', '--file', '../../test-scripts/test-images/Document.jpeg', '--license', $env:SCANBOT_LICENSE) }, From a6e623fae2a7be74cd89e0625ebc201336843d8d Mon Sep 17 00:00:00 2001 From: yurii-scanbot Date: Wed, 2 Dec 2026 10:18:45 +0100 Subject: [PATCH 2/2] Move runWithPriorCorners examples into separate files Split the prior-corners straightening snippet into its own file for Node.js, Python, Java and C, and updated the dispatch code that calls it. The CLI command name is unchanged, so the docs and test scripts are untouched. --- .../snippets/document/document_straightener.h | 1 - ...document_straightener_with_prior_corners.h | 8 +++ examples/c/src/main.c | 1 + .../snippets/document/document_straightener.c | 51 ----------------- ...document_straightener_with_prior_corners.c | 56 +++++++++++++++++++ .../io/scanbot/sdk/ScanbotSDKExample.java | 2 +- .../document/DocumentStraightenerSnippet.java | 20 ------- ...ntStraightenerWithPriorCornersSnippet.java | 30 ++++++++++ examples/nodejs/src/index.ts | 3 +- ...ocument-straightener-with-prior-corners.ts | 24 ++++++++ .../document/document-straightener.ts | 21 ------- examples/python/main.py | 3 +- .../document/document_straightener.py | 21 ------- ...ocument_straightener_with_prior_corners.py | 21 +++++++ 14 files changed, 145 insertions(+), 117 deletions(-) create mode 100644 examples/c/include/snippets/document/document_straightener_with_prior_corners.h create mode 100644 examples/c/src/snippets/document/document_straightener_with_prior_corners.c create mode 100644 examples/java/src/main/java/io/scanbot/sdk/snippets/document/DocumentStraightenerWithPriorCornersSnippet.java create mode 100644 examples/nodejs/src/snippets/document/document-straightener-with-prior-corners.ts create mode 100644 examples/python/snippets/document/document_straightener_with_prior_corners.py diff --git a/examples/c/include/snippets/document/document_straightener.h b/examples/c/include/snippets/document/document_straightener.h index 7085c10..1c63bc7 100644 --- a/examples/c/include/snippets/document/document_straightener.h +++ b/examples/c/include/snippets/document/document_straightener.h @@ -4,6 +4,5 @@ #include scanbotsdk_error_code_t straighten_document(scanbotsdk_image_t *image); -scanbotsdk_error_code_t straighten_document_with_prior_corners(scanbotsdk_image_t *image); #endif diff --git a/examples/c/include/snippets/document/document_straightener_with_prior_corners.h b/examples/c/include/snippets/document/document_straightener_with_prior_corners.h new file mode 100644 index 0000000..4004937 --- /dev/null +++ b/examples/c/include/snippets/document/document_straightener_with_prior_corners.h @@ -0,0 +1,8 @@ +#ifndef DOCUMENT_STRAIGHTENER_WITH_PRIOR_CORNERS_H +#define DOCUMENT_STRAIGHTENER_WITH_PRIOR_CORNERS_H + +#include + +scanbotsdk_error_code_t straighten_document_with_prior_corners(scanbotsdk_image_t *image); + +#endif diff --git a/examples/c/src/main.c b/examples/c/src/main.c index b031762..ae99ffb 100644 --- a/examples/c/src/main.c +++ b/examples/c/src/main.c @@ -23,6 +23,7 @@ #include #include #include +#include #include diff --git a/examples/c/src/snippets/document/document_straightener.c b/examples/c/src/snippets/document/document_straightener.c index 497a6b5..0720fa0 100644 --- a/examples/c/src/snippets/document/document_straightener.c +++ b/examples/c/src/snippets/document/document_straightener.c @@ -69,54 +69,3 @@ scanbotsdk_error_code_t straighten_document(scanbotsdk_image_t *image) { return ec; } - -scanbotsdk_error_code_t straighten_document_with_prior_corners(scanbotsdk_image_t *image) { - scanbotsdk_error_code_t ec = SCANBOTSDK_OK; - - scanbotsdk_document_straightening_result_t *result = NULL; - scanbotsdk_document_straightening_parameters_t *straightening_params = NULL; - scanbotsdk_document_straightener_t *straightener = NULL; - scanbotsdk_image_t *straightened_image = NULL; - - // Apriori known document corners, normalized to [0, 1] and given clockwise: - // top-left, top-right, bottom-right, bottom-left. - // Provide them only if known with high confidence (e.g. from a user interaction). - scanbotsdk_pointf_t prior_corners[4] = { - {0.05f, 0.05f}, - {0.95f, 0.05f}, - {0.95f, 0.95f}, - {0.05f, 0.95f} - }; - - ec = scanbotsdk_document_straightening_parameters_create_with_defaults(&straightening_params); - if (ec != SCANBOTSDK_OK) { fprintf(stderr, "document_straightening_parameters_create_with_defaults: %d: %s\n", ec, error_message(ec)); goto cleanup; } - - ec = scanbotsdk_document_straightener_create(&straightener); - if (ec != SCANBOTSDK_OK) { fprintf(stderr, "document_straightener_create: %d: %s\n", ec, error_message(ec)); goto cleanup; } - - ec = scanbotsdk_document_straightener_run_with_prior_corners( - straightener, - image, - prior_corners, - 4, - straightening_params, - &result - ); - if (ec != SCANBOTSDK_OK) { fprintf(stderr, "document_straighten_with_prior_corners: %d: %s\n", ec, error_message(ec)); goto cleanup; } - - ec = scanbotsdk_document_straightening_result_get_straightened_image(result, &straightened_image); - if (ec != SCANBOTSDK_OK) { fprintf(stderr, "document_straightening_result_get_straightened_image: %d: %s\n", ec, error_message(ec)); goto cleanup; } - - if (straightened_image == NULL) { - fprintf(stderr, "No straightened image returned.\n"); - } - - /* straightened_image can be saved or processed further here */ - -cleanup: - scanbotsdk_document_straightener_free(straightener); - scanbotsdk_document_straightening_result_free(result); - scanbotsdk_document_straightening_parameters_free(straightening_params); - - return ec; -} diff --git a/examples/c/src/snippets/document/document_straightener_with_prior_corners.c b/examples/c/src/snippets/document/document_straightener_with_prior_corners.c new file mode 100644 index 0000000..895397e --- /dev/null +++ b/examples/c/src/snippets/document/document_straightener_with_prior_corners.c @@ -0,0 +1,56 @@ +#include +#include + +#include +#include + +scanbotsdk_error_code_t straighten_document_with_prior_corners(scanbotsdk_image_t *image) { + scanbotsdk_error_code_t ec = SCANBOTSDK_OK; + + scanbotsdk_document_straightening_result_t *result = NULL; + scanbotsdk_document_straightening_parameters_t *straightening_params = NULL; + scanbotsdk_document_straightener_t *straightener = NULL; + scanbotsdk_image_t *straightened_image = NULL; + + // Apriori known document corners, normalized to [0, 1] and given clockwise: + // top-left, top-right, bottom-right, bottom-left. + // Provide them only if known with high confidence (e.g. from a user interaction). + scanbotsdk_pointf_t prior_corners[4] = { + {0.05f, 0.05f}, + {0.95f, 0.05f}, + {0.95f, 0.95f}, + {0.05f, 0.95f} + }; + + ec = scanbotsdk_document_straightening_parameters_create_with_defaults(&straightening_params); + if (ec != SCANBOTSDK_OK) { fprintf(stderr, "document_straightening_parameters_create_with_defaults: %d: %s\n", ec, error_message(ec)); goto cleanup; } + + ec = scanbotsdk_document_straightener_create(&straightener); + if (ec != SCANBOTSDK_OK) { fprintf(stderr, "document_straightener_create: %d: %s\n", ec, error_message(ec)); goto cleanup; } + + ec = scanbotsdk_document_straightener_run_with_prior_corners( + straightener, + image, + prior_corners, + 4, + straightening_params, + &result + ); + if (ec != SCANBOTSDK_OK) { fprintf(stderr, "document_straighten_with_prior_corners: %d: %s\n", ec, error_message(ec)); goto cleanup; } + + ec = scanbotsdk_document_straightening_result_get_straightened_image(result, &straightened_image); + if (ec != SCANBOTSDK_OK) { fprintf(stderr, "document_straightening_result_get_straightened_image: %d: %s\n", ec, error_message(ec)); goto cleanup; } + + if (straightened_image == NULL) { + fprintf(stderr, "No straightened image returned.\n"); + } + + /* straightened_image can be saved or processed further here */ + +cleanup: + scanbotsdk_document_straightener_free(straightener); + scanbotsdk_document_straightening_result_free(result); + scanbotsdk_document_straightening_parameters_free(straightening_params); + + return ec; +} diff --git a/examples/java/src/main/java/io/scanbot/sdk/ScanbotSDKExample.java b/examples/java/src/main/java/io/scanbot/sdk/ScanbotSDKExample.java index 5e79d6e..8e27c0f 100644 --- a/examples/java/src/main/java/io/scanbot/sdk/ScanbotSDKExample.java +++ b/examples/java/src/main/java/io/scanbot/sdk/ScanbotSDKExample.java @@ -85,7 +85,7 @@ public static void main(String[] args) throws Exception { break; case "straighten_document_with_prior_corners": try (ImageRef image = Utils.createImageRef(file, resource)) { - DocumentStraightenerSnippet.runWithPriorCorners(image); + DocumentStraightenerWithPriorCornersSnippet.run(image); } break; case "cleanup_document": diff --git a/examples/java/src/main/java/io/scanbot/sdk/snippets/document/DocumentStraightenerSnippet.java b/examples/java/src/main/java/io/scanbot/sdk/snippets/document/DocumentStraightenerSnippet.java index 24399ef..f15ab72 100644 --- a/examples/java/src/main/java/io/scanbot/sdk/snippets/document/DocumentStraightenerSnippet.java +++ b/examples/java/src/main/java/io/scanbot/sdk/snippets/document/DocumentStraightenerSnippet.java @@ -6,7 +6,6 @@ import io.scanbot.sdk.documentscanner.DocumentStraighteningParameters; import io.scanbot.sdk.documentscanner.DocumentStraighteningResult; import io.scanbot.sdk.geometry.AspectRatio; -import io.scanbot.sdk.geometry.PointF; import io.scanbot.sdk.image.ImageRef; public class DocumentStraightenerSnippet { @@ -27,23 +26,4 @@ public static void run(ImageRef image) throws Exception { // The straightened image can be accessed via result.getStraightenedImage() and saved or further processed as needed. } } - - public static void runWithPriorCorners(ImageRef image) throws Exception { - // Apriori known document corners, normalized to [0, 1] and given clockwise: - // top-left, top-right, bottom-right, bottom-left. - // Provide them only if known with high confidence (e.g. from a user interaction). - List priorCorners = List.of( - new PointF(0.05f, 0.05f), - new PointF(0.95f, 0.05f), - new PointF(0.95f, 0.95f), - new PointF(0.05f, 0.95f) - ); - - try ( - DocumentStraightener straightener = new DocumentStraightener(); - DocumentStraighteningResult result = straightener.runWithPriorCorners(image, priorCorners, new DocumentStraighteningParameters()) - ) { - // The straightened image can be accessed via result.getStraightenedImage() and saved or further processed as needed. - } - } } diff --git a/examples/java/src/main/java/io/scanbot/sdk/snippets/document/DocumentStraightenerWithPriorCornersSnippet.java b/examples/java/src/main/java/io/scanbot/sdk/snippets/document/DocumentStraightenerWithPriorCornersSnippet.java new file mode 100644 index 0000000..33f8cae --- /dev/null +++ b/examples/java/src/main/java/io/scanbot/sdk/snippets/document/DocumentStraightenerWithPriorCornersSnippet.java @@ -0,0 +1,30 @@ +package io.scanbot.sdk.snippets.document; + +import java.util.List; + +import io.scanbot.sdk.documentscanner.DocumentStraightener; +import io.scanbot.sdk.documentscanner.DocumentStraighteningParameters; +import io.scanbot.sdk.documentscanner.DocumentStraighteningResult; +import io.scanbot.sdk.geometry.PointF; +import io.scanbot.sdk.image.ImageRef; + +public class DocumentStraightenerWithPriorCornersSnippet { + public static void run(ImageRef image) throws Exception { + // Apriori known document corners, normalized to [0, 1] and given clockwise: + // top-left, top-right, bottom-right, bottom-left. + // Provide them only if known with high confidence (e.g. from a user interaction). + List priorCorners = List.of( + new PointF(0.05f, 0.05f), + new PointF(0.95f, 0.05f), + new PointF(0.95f, 0.95f), + new PointF(0.05f, 0.95f) + ); + + try ( + DocumentStraightener straightener = new DocumentStraightener(); + DocumentStraighteningResult result = straightener.runWithPriorCorners(image, priorCorners, new DocumentStraighteningParameters()) + ) { + // The straightened image can be accessed via result.getStraightenedImage() and saved or further processed as needed. + } + } +} diff --git a/examples/nodejs/src/index.ts b/examples/nodejs/src/index.ts index f6a3d7e..ca7879a 100644 --- a/examples/nodejs/src/index.ts +++ b/examples/nodejs/src/index.ts @@ -18,6 +18,7 @@ import { AnalyzeMultiPageSnippet } from "./snippets/document/analyze-multipage"; import { CropAndAnalyzeSnippet } from "./snippets/document/crop-analyze"; import { DocumentCleanupSnippet } from "./snippets/document/document-cleanup"; import { DocumentStraightenerSnippet } from "./snippets/document/document-straightener"; +import { DocumentStraightenerWithPriorCornersSnippet } from "./snippets/document/document-straightener-with-prior-corners"; async function awaitPromise(promise: Promise, maxAwaitTimeMs: number = 60 * 1000): Promise { const timer = setTimeout(() => { @@ -107,7 +108,7 @@ async function main(): Promise { } case "straighten_document_with_prior_corners": { const image = await ScanbotSDK.ImageRef.fromPath(file); - await DocumentStraightenerSnippet.runWithPriorCorners(image); + await DocumentStraightenerWithPriorCornersSnippet.run(image); break; } case "cleanup_document": diff --git a/examples/nodejs/src/snippets/document/document-straightener-with-prior-corners.ts b/examples/nodejs/src/snippets/document/document-straightener-with-prior-corners.ts new file mode 100644 index 0000000..51f2634 --- /dev/null +++ b/examples/nodejs/src/snippets/document/document-straightener-with-prior-corners.ts @@ -0,0 +1,24 @@ +import * as ScanbotSDK from "scanbotsdk"; + +export class DocumentStraightenerWithPriorCornersSnippet { + public static async run(image: ScanbotSDK.ImageRef): Promise { + // Apriori known document corners, normalized to [0, 1] and given clockwise: + // top-left, top-right, bottom-right, bottom-left. + // Provide them only if known with high confidence (e.g. from a user interaction). + const priorCorners = [ + { x: 0.05, y: 0.05 }, + { x: 0.95, y: 0.05 }, + { x: 0.95, y: 0.95 }, + { x: 0.05, y: 0.95 } + ]; + + await using straightener = await ScanbotSDK.DocumentStraightener.create(); + await using result = await straightener.runWithPriorCorners( + image, + priorCorners, + new ScanbotSDK.DocumentStraighteningParameters() + ); + + // The straightened image can be accessed via result.straightenedImage and saved or further processed as needed. + } +} diff --git a/examples/nodejs/src/snippets/document/document-straightener.ts b/examples/nodejs/src/snippets/document/document-straightener.ts index 532c505..ed8a4c5 100644 --- a/examples/nodejs/src/snippets/document/document-straightener.ts +++ b/examples/nodejs/src/snippets/document/document-straightener.ts @@ -19,25 +19,4 @@ export class DocumentStraightenerSnippet { // The straightened image can be accessed via result.straightenedImage and saved or further processed as needed. } - - public static async runWithPriorCorners(image: ScanbotSDK.ImageRef): Promise { - // Apriori known document corners, normalized to [0, 1] and given clockwise: - // top-left, top-right, bottom-right, bottom-left. - // Provide them only if known with high confidence (e.g. from a user interaction). - const priorCorners = [ - { x: 0.05, y: 0.05 }, - { x: 0.95, y: 0.05 }, - { x: 0.95, y: 0.95 }, - { x: 0.05, y: 0.95 } - ]; - - await using straightener = await ScanbotSDK.DocumentStraightener.create(); - await using result = await straightener.runWithPriorCorners( - image, - priorCorners, - new ScanbotSDK.DocumentStraighteningParameters() - ); - - // The straightened image can be accessed via result.straightenedImage and saved or further processed as needed. - } } diff --git a/examples/python/main.py b/examples/python/main.py index 03f2982..73c46fa 100644 --- a/examples/python/main.py +++ b/examples/python/main.py @@ -1,7 +1,8 @@ import sys import scanbotsdk -from snippets.document.document_straightener import straighten_document, straighten_document_with_prior_corners +from snippets.document.document_straightener import straighten_document +from snippets.document.document_straightener_with_prior_corners import straighten_document_with_prior_corners from snippets.document.analyze_multi_page import analyze_multi_page from snippets.document.crop_and_analyze import crop_and_analyze from snippets.document.document_cleanup import cleanup_document diff --git a/examples/python/snippets/document/document_straightener.py b/examples/python/snippets/document/document_straightener.py index 1b7ed55..bfbc0d2 100644 --- a/examples/python/snippets/document/document_straightener.py +++ b/examples/python/snippets/document/document_straightener.py @@ -14,24 +14,3 @@ def straighten_document(image: ImageRef): result: DocumentStraighteningResult = straightener.run(image=image, parameters=params) # The straightened image can be accessed via result.straightened_image and saved or further processed as needed. - - -def straighten_document_with_prior_corners(image: ImageRef): - # Apriori known document corners, normalized to [0, 1] and given clockwise: - # top-left, top-right, bottom-right, bottom-left. - # Provide them only if known with high confidence (e.g. from a user interaction). - prior_corners = [ - PointF(x=0.05, y=0.05), - PointF(x=0.95, y=0.05), - PointF(x=0.95, y=0.95), - PointF(x=0.05, y=0.95), - ] - - straightener = DocumentStraightener() - result: DocumentStraighteningResult = straightener.run_with_prior_corners( - image=image, - prior_corners_normalized=prior_corners, - parameters=DocumentStraighteningParameters(), - ) - - # The straightened image can be accessed via result.straightened_image and saved or further processed as needed. diff --git a/examples/python/snippets/document/document_straightener_with_prior_corners.py b/examples/python/snippets/document/document_straightener_with_prior_corners.py new file mode 100644 index 0000000..7ffea98 --- /dev/null +++ b/examples/python/snippets/document/document_straightener_with_prior_corners.py @@ -0,0 +1,21 @@ +from scanbotsdk import * + +def straighten_document_with_prior_corners(image: ImageRef): + # Apriori known document corners, normalized to [0, 1] and given clockwise: + # top-left, top-right, bottom-right, bottom-left. + # Provide them only if known with high confidence (e.g. from a user interaction). + prior_corners = [ + PointF(x=0.05, y=0.05), + PointF(x=0.95, y=0.05), + PointF(x=0.95, y=0.95), + PointF(x=0.05, y=0.95), + ] + + straightener = DocumentStraightener() + result: DocumentStraighteningResult = straightener.run_with_prior_corners( + image=image, + prior_corners_normalized=prior_corners, + parameters=DocumentStraighteningParameters(), + ) + + # The straightened image can be accessed via result.straightened_image and saved or further processed as needed.