Skip to content

Commit cf44ac0

Browse files
author
Michaël Villeneuve
committed
Allow plugging with custom-crop, improvements
1 parent 1655eb4 commit cf44ac0

File tree

6 files changed

+52
-34
lines changed

6 files changed

+52
-34
lines changed

README.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ Features :
1111
- Flash
1212
- Easy to use base64 image
1313

14+
#### Can be easily plugged with [react-native-custom-crop](https://github.com/Michaelvilleneuve/react-native-custom-crop)
15+
16+
17+
![Demo crop gif](https://camo.githubusercontent.com/0ac887deaa7263172a5fd2759dba3d692e98585a/68747470733a2f2f73332d65752d776573742d312e616d617a6f6e6177732e636f6d2f6d69636861656c76696c6c656e657576652f64656d6f2d63726f702e676966)
18+
1419
## Getting started
1520

1621
`$ npm install react-native-document-scanner --save`
@@ -34,7 +39,11 @@ class YourComponent extends Component {
3439
return (
3540
<View>
3641
<DocumentScanner
37-
onPictureTaken={data => this.setState({ image: data.image })}
42+
onPictureTaken={data => this.setState({
43+
image: data.image,
44+
initialImage: data.initialImage,
45+
rectangleCoordinates: data.rectangleCoordinates,
46+
})}
3847
overlayColor="rgba(255,130,0, 0.7)"
3948
enableTorch={false}
4049
brightness={0.3}
@@ -89,7 +98,7 @@ Enum (0, 1 or 2) corresponding to the type of rectangle found
8998

9099
| Prop | Params | Type | Description |
91100
| :----------- |:-------:| :--------:| :----------|
92-
| onPictureTaken | `data` | `object` | Returns the captured image in an object `{ image: 'BASE64 string'}` |
101+
| onPictureTaken | `data` | `object` | Returns the captured image in an object `{ image: 'BASE64 string', initialImage: 'BASE64 string', rectangleCoordinates: 'object of coordinates' }` |
93102

94103

95104

ios/DocumentScannerView.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
//
2-
// DocumentScannerView.h
3-
// DocumentScanner
4-
//
5-
// Created by Marc PEYSALE on 22/06/2017.
6-
// Copyright © 2017 Snapp'. All rights reserved.
7-
//
8-
91
#import "IPDFCameraViewController.h"
102
#import <React/RCTViewManager.h>
113

ios/DocumentScannerView.m

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
//
2-
// DocumentScannerView.m
3-
// DocumentScanner
4-
//
5-
// Created by Marc PEYSALE on 22/06/2017.
6-
// Copyright © 2017 Snapp'. All rights reserved.
7-
//
8-
91
#import "DocumentScannerView.h"
102
#import "IPDFCameraViewController.h"
113

@@ -25,8 +17,6 @@ - (instancetype)init {
2517
[self setBrightness: self.brightness];
2618
[self setSaturation: self.saturation];
2719

28-
NSLog(@"detectionCountBeforeCapture: %ld", (long)self.detectionCountBeforeCapture);
29-
NSLog(@"detectionRefreshRateInMS: %ld", (long)self.detectionRefreshRateInMS);
3020

3121
[self start];
3222
[self setDelegate: self];
@@ -50,10 +40,34 @@ - (void) didDetectRectangle:(CIRectangleFeature *)rectangle withType:(IPDFRectan
5040
}
5141

5242
if (self.stableCounter > self.detectionCountBeforeCapture){
53-
[self captureImageWithCompletionHander:^(id data) {
43+
[self captureImageWithCompletionHander:^(UIImage *croppedImage, UIImage *initialImage, CIRectangleFeature *rectangleFeature) {
5444
if (self.onPictureTaken) {
55-
NSData *imageData = UIImageJPEGRepresentation(data, self.quality);
56-
self.onPictureTaken(@{@"image": [imageData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength]});
45+
NSData *croppedImageData = UIImageJPEGRepresentation(croppedImage, self.quality);
46+
47+
if (initialImage.imageOrientation != UIImageOrientationUp) {
48+
UIGraphicsBeginImageContextWithOptions(initialImage.size, false, initialImage.scale);
49+
[initialImage drawInRect:CGRectMake(0, 0, initialImage.size.width
50+
, initialImage.size.height)];
51+
initialImage = UIGraphicsGetImageFromCurrentImageContext();
52+
UIGraphicsEndImageContext();
53+
}
54+
NSData *initialImageData = UIImageJPEGRepresentation(initialImage, self.quality);
55+
56+
/*
57+
RectangleCoordinates expects a rectanle viewed from portrait,
58+
while rectangleFeature returns a rectangle viewed from landscape, which explains the nonsense of the mapping below.
59+
Sorry about that.
60+
*/
61+
self.onPictureTaken(@{
62+
@"croppedImage": [croppedImageData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength],
63+
@"initialImage": [initialImageData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength],
64+
@"rectangleCoordinates": @{
65+
@"topLeft": @{ @"y": @(rectangleFeature.bottomLeft.x + 30), @"x": @(rectangleFeature.bottomLeft.y)},
66+
@"topRight": @{ @"y": @(rectangleFeature.topLeft.x + 30), @"x": @(rectangleFeature.topLeft.y)},
67+
@"bottomLeft": @{ @"y": @(rectangleFeature.bottomRight.x), @"x": @(rectangleFeature.bottomRight.y)},
68+
@"bottomRight": @{ @"y": @(rectangleFeature.topRight.x), @"x": @(rectangleFeature.topRight.y)},
69+
}
70+
});
5771
[self stop];
5872
}
5973
}];

ios/IPDFCameraViewController.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ typedef NS_ENUM(NSInteger, IPDFRectangeType)
4343

4444
- (void)focusAtPoint:(CGPoint)point completionHandler:(void(^)())completionHandler;
4545

46-
- (void)captureImageWithCompletionHander:(void(^)(id data))completionHandler;
46+
- (void)captureImageWithCompletionHander:(void(^)(UIImage *data, UIImage *initialData, CIRectangleFeature *rectangleFeature))completionHandler;
4747

4848
@property (nonatomic, strong) UIColor* overlayColor;
4949
@property (nonatomic, assign) float saturation;

ios/IPDFCameraViewController.m

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ - (void)focusAtPoint:(CGPoint)point completionHandler:(void(^)())completionHandl
323323
}
324324
}
325325

326-
- (void)captureImageWithCompletionHander:(void(^)(id data))completionHandler
326+
- (void)captureImageWithCompletionHander:(void(^)(id data, id initialData, CIRectangleFeature *rectangleFeature))completionHandler
327327
{
328328
if (_isCapturing) return;
329329

@@ -377,21 +377,24 @@ - (void)captureImageWithCompletionHander:(void(^)(id data))completionHandler
377377
if (rectangleFeature)
378378
{
379379
enhancedImage = [self correctPerspectiveForImage:enhancedImage withFeatures:rectangleFeature];
380+
381+
UIGraphicsBeginImageContext(CGSizeMake(enhancedImage.extent.size.height, enhancedImage.extent.size.width));
382+
[[UIImage imageWithCIImage:enhancedImage scale:1.0 orientation:UIImageOrientationRight] drawInRect:CGRectMake(0,0, enhancedImage.extent.size.height, enhancedImage.extent.size.width)];
383+
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
384+
UIImage *initialImage = [UIImage imageWithData:imageData];
385+
UIGraphicsEndImageContext();
386+
387+
[weakSelf hideGLKView:NO completion:nil];
388+
completionHandler(image, initialImage, rectangleFeature);
380389
}
381390
}
382391

383-
UIGraphicsBeginImageContext(CGSizeMake(enhancedImage.extent.size.height, enhancedImage.extent.size.width));
384-
[[UIImage imageWithCIImage:enhancedImage scale:1.0 orientation:UIImageOrientationRight] drawInRect:CGRectMake(0,0, enhancedImage.extent.size.height, enhancedImage.extent.size.width)];
385-
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
386-
UIGraphicsEndImageContext();
387-
388-
[weakSelf hideGLKView:NO completion:nil];
389-
completionHandler(image);
390392
}
391393
else
392394
{
393395
[weakSelf hideGLKView:NO completion:nil];
394-
completionHandler(imageData);
396+
UIImage *initialImage = [UIImage imageWithData:imageData];
397+
completionHandler(initialImage, initialImage, nil);
395398
}
396399

397400
_isCapturing = NO;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
{
33
"name": "react-native-document-scanner",
44
"description": "Scan documents, automatic border detection, automatic crop",
5-
"version": "1.1.2",
5+
"version": "1.2.0",
66
"main": "index.js",
77
"scripts": {
88
"test": "echo \"Error: no test specified\" && exit 1"

0 commit comments

Comments
 (0)