From ae1119d3a52d53717cbfa030f0bb49efc445d30d Mon Sep 17 00:00:00 2001 From: Guillian Drouin Date: Wed, 7 Feb 2024 08:58:22 +0100 Subject: [PATCH 1/2] Fixed crash when pdf contains different sizes of page --- CHANGELOG.md | 5 ++++ README.md | 4 +-- lib/ocr_scan/helper/pdf_helper.dart | 45 +++++++++++++++-------------- pubspec.yaml | 2 +- 4 files changed, 31 insertions(+), 25 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 85301f1..bda8442 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +## 1.3.2 + +- Fixed crash when pdf contains different sizes of page + + ## 1.3.0 - Updated `file_picker` dependency, watch out for `ffi` version diff --git a/README.md b/README.md index 881a287..7d7c50c 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Flutter OCR Scan Text OCR Flutter -`v1.3.1` +`v1.3.2` Flutter OCR Scan Text is a wrapper around the "[Google ML kit Text Recognition](https://pub.dev/packages/google_mlkit_text_recognition)" library. It helps to facilitate accurate text search and display of results from the camera. It also allows to manage text searches from an image or a pdf. @@ -26,7 +26,7 @@ Note: The library uses the [Camera](https://pub.dev/packages/camera) package, be ```dart dependencies: -ocr_scan_text: 1.3.1 +ocr_scan_text: 1.3.2 ``` #### To use the library, import : diff --git a/lib/ocr_scan/helper/pdf_helper.dart b/lib/ocr_scan/helper/pdf_helper.dart index 386479a..ba58702 100644 --- a/lib/ocr_scan/helper/pdf_helper.dart +++ b/lib/ocr_scan/helper/pdf_helper.dart @@ -9,7 +9,7 @@ import 'package:pdf_render/pdf_render.dart'; class PDFHelper { static Future convertToPDFImage(PdfDocument document) async { - img.Image? image = await _createFullImageFromPDF(document); + img.Image? image = await _createFullImageFromPDF(document: document); if (image == null) { return null; } @@ -26,35 +26,32 @@ class PDFHelper { ); } - static Future _createFullImageFromPDF( - PdfDocument document) async { + static Future _createFullImageFromPDF({required PdfDocument document, int scale = 5}) async { final List imageList = []; - int height = 0, width = 0; + int width = 0; + List heights = []; /// On prend que les 2 premiers page max, sinon c'est le bordel for (int i = 1; i <= min(2, document.pageCount); i++) { final page = await document.getPage(i); - int scaleUp = - 5; // 5 is an arbitrary number, we enlarge the image to improve text detection final pageImage = await page.render( - width: page.width.toInt() * scaleUp, - height: page.height.toInt() * scaleUp, + width: page.width.toInt() * scale, + height: page.height.toInt() * scale, backgroundFill: true, allowAntialiasingIOS: true, - fullWidth: page.width * scaleUp, - fullHeight: page.height * scaleUp, + fullWidth: page.width * scale, + fullHeight: page.height * scale, ); var imageUI = await pageImage.createImageDetached(); var imgBytes = await imageUI.toByteData(format: ImageByteFormat.png); if (imgBytes == null) { continue; } - var libImage = img.decodeImage(imgBytes.buffer - .asUint8List(imgBytes.offsetInBytes, imgBytes.lengthInBytes)); + var libImage = img.decodeImage(imgBytes.buffer.asUint8List(imgBytes.offsetInBytes, imgBytes.lengthInBytes)); if (libImage == null) { continue; } - height += imageUI.height; + heights.add(imageUI.height); if ((imageUI.width) > width) { width = imageUI.width; } @@ -62,23 +59,27 @@ class PDFHelper { imageList.add(libImage); } - final img.Image mergedImage = img.Image(width: width, height: height); + int fullHeight = 0; + for (var height in heights) { + fullHeight += height; + } + + final img.Image mergedImage = img.Image( + width: width, + height: fullHeight, + ); // Merge generated image vertically as vertical-orientated-multi-pdf + var lastOffset = 0; for (var i = 0; i < imageList.length; i++) { - // one page height - final onePageImageOffset = height / document.pageCount; - - // offset for actual page from by y axis - final actualPageOffset = i == 0 ? 0 : onePageImageOffset * i - 1; - img.compositeImage( mergedImage, imageList[i], srcW: width, - srcH: onePageImageOffset.round(), - dstY: actualPageOffset.round(), + srcH: heights[i].round(), + dstY: lastOffset.round(), ); + lastOffset += heights[i]; } return mergedImage; diff --git a/pubspec.yaml b/pubspec.yaml index f668d22..338269b 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: ocr_scan_text description: OCR Scan Text is a wrapper around the "Google ML kit Text Recognition" library. -version: 1.3.1 +version: 1.3.2 homepage: https://github.com/boursorama/ocr_scan_text environment: From 042411a9152567f4722ffd53ec1d2405dc8cadff Mon Sep 17 00:00:00 2001 From: Guillian Drouin Date: Mon, 27 May 2024 09:32:02 +0200 Subject: [PATCH 2/2] Fix: Add MaterialApp and add start module for example --- example/lib/main.dart | 22 ++++++++++++---------- example/lib/scan_all_module.dart | 2 +- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/example/lib/main.dart b/example/lib/main.dart index ee61b9d..247761c 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -16,15 +16,17 @@ class MyApp extends StatefulWidget { class _MyAppState extends State { @override Widget build(BuildContext context) { - return Scaffold( - appBar: AppBar( - title: const Text("Scan"), - ), - body: Center( - child: SizedBox( - width: MediaQuery.of(context).size.width - 20, - height: MediaQuery.of(context).size.height - 40, - child: _buildLiveScan(), + return MaterialApp( + home: Scaffold( + appBar: AppBar( + title: const Text("Scan"), + ), + body: Center( + child: SizedBox( + width: MediaQuery.of(context).size.width - 20, + height: MediaQuery.of(context).size.height - 40, + child: _buildLiveScan(), + ), ), ), ); @@ -35,7 +37,7 @@ class _MyAppState extends State { ocrTextResult: (ocrTextResult) { ocrTextResult.mapResult.forEach((module, result) {}); }, - scanModules: [ScanAllModule()], + scanModules: [ScanAllModule()..start()], ); } } diff --git a/example/lib/scan_all_module.dart b/example/lib/scan_all_module.dart index 769d1ca..b9b828b 100644 --- a/example/lib/scan_all_module.dart +++ b/example/lib/scan_all_module.dart @@ -4,7 +4,7 @@ import 'package:ocr_scan_text/ocr_scan_text.dart'; class ScanAllModule extends ScanModule { ScanAllModule() : super( - label: '', + label: 'All', color: Colors.redAccent.withOpacity(0.3), validateCountCorrelation: 1, );