Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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 :
Expand Down
22 changes: 12 additions & 10 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@ class MyApp extends StatefulWidget {
class _MyAppState extends State<MyApp> {
@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(),
),
),
),
);
Expand All @@ -35,7 +37,7 @@ class _MyAppState extends State<MyApp> {
ocrTextResult: (ocrTextResult) {
ocrTextResult.mapResult.forEach((module, result) {});
},
scanModules: [ScanAllModule()],
scanModules: [ScanAllModule()..start()],
);
}
}
2 changes: 1 addition & 1 deletion example/lib/scan_all_module.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
Expand Down
45 changes: 23 additions & 22 deletions lib/ocr_scan/helper/pdf_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import 'package:pdf_render/pdf_render.dart';

class PDFHelper {
static Future<ImagePDF?> convertToPDFImage(PdfDocument document) async {
img.Image? image = await _createFullImageFromPDF(document);
img.Image? image = await _createFullImageFromPDF(document: document);
if (image == null) {
return null;
}
Expand All @@ -26,59 +26,60 @@ class PDFHelper {
);
}

static Future<img.Image?> _createFullImageFromPDF(
PdfDocument document) async {
static Future<img.Image?> _createFullImageFromPDF({required PdfDocument document, int scale = 5}) async {
final List<img.Image> imageList = [];
int height = 0, width = 0;
int width = 0;
List<int> 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;
}

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;
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down