Skip to content

Commit 3b85a16

Browse files
author
Michaël Villeneuve
authored
Merge pull request #15 from poyannabati/pn/add-image-uri
Changes default behavior to pass URI's instead of base64
2 parents 7736686 + 6af3c95 commit 3b85a16

File tree

5 files changed

+26
-8
lines changed

5 files changed

+26
-8
lines changed

Example/index.ios.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export default class Example extends Component {
4444
{this.state.image ?
4545
<Image style={{ flex: 1, width: 300, height: 200 }} source={{ uri: `data:image/jpeg;base64,${this.state.image}`}} resizeMode="contain" /> :
4646
<Scanner
47+
useBase64
4748
onPictureTaken={data => this.setState({ image: data.croppedImage })}
4849
overlayColor="rgba(255,130,0, 0.7)"
4950
enableTorch={this.state.flashEnabled}

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# React Native Document Scanner (iOS only)
44

5-
Live document detection library. Returns a base64 encoded string of the captured image, allowing you to easily store it or use it as you wish !
5+
Live document detection library. Returns either a URI or a base64 encoded string of the captured image, allowing you to easily store it or use it as you wish !
66

77
Features :
88
- Live detection
@@ -47,6 +47,7 @@ class YourComponent extends Component {
4747
return (
4848
<View>
4949
<DocumentScanner
50+
useBase64
5051
onPictureTaken={data => this.setState({
5152
image: data.croppedImage,
5253
initialImage: data.initialImage,
@@ -83,6 +84,7 @@ class YourComponent extends Component {
8384
| saturation | `1` | `float` | Increase or decrease camera saturation. Set `0` for black & white |
8485
| contrast | `1` | `float` | Increase or decrease camera contrast. Normal as default |
8586
| quality | `0.8` | `float` | Image compression. Reduces both image size and quality |
87+
| useBase64 | `false` | `bool` | If base64 representation should be passed instead of image uri's |
8688

8789
## Manual capture
8890

@@ -119,7 +121,7 @@ Enum (0, 1 or 2) corresponding to the type of rectangle found
119121

120122
| Prop | Params | Type | Description |
121123
| :----------- |:-------:| :--------:| :----------|
122-
| onPictureTaken | `data` | `object` | Returns the captured image in an object `{ croppedImage: 'BASE64 string', initialImage: 'BASE64 string', rectangleCoordinates: 'object of coordinates' }` |
124+
| onPictureTaken | `data` | `object` | Returns the captured image in an object `{ croppedImage: ('URI or BASE64 string'), initialImage: 'URI or BASE64 string', rectangleCoordinates: 'object of coordinates' }` |
123125

124126

125127

ios/DocumentScannerView.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
@property (nonatomic, assign) NSInteger detectionCountBeforeCapture;
99
@property (assign, nonatomic) NSInteger stableCounter;
1010
@property (nonatomic, assign) float quality;
11+
@property (nonatomic, assign) BOOL useBase64;
1112

1213
- (void) capture;
1314

ios/DocumentScannerView.m

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ - (void) didDetectRectangle:(CIRectangleFeature *)rectangle withType:(IPDFRectan
3434

3535
- (void) capture {
3636
[self captureImageWithCompletionHander:^(UIImage *croppedImage, UIImage *initialImage, CIRectangleFeature *rectangleFeature) {
37-
38-
if (self.onPictureTaken) {
37+
if (self.onPictureTaken) {
3938
NSData *croppedImageData = UIImageJPEGRepresentation(croppedImage, self.quality);
4039

4140
if (initialImage.imageOrientation != UIImageOrientationUp) {
@@ -58,10 +57,24 @@ - (void) capture {
5857
@"bottomLeft": @{ @"y": @(rectangleFeature.bottomRight.x), @"x": @(rectangleFeature.bottomRight.y)},
5958
@"bottomRight": @{ @"y": @(rectangleFeature.topRight.x), @"x": @(rectangleFeature.topRight.y)},
6059
} : [NSNull null];
61-
self.onPictureTaken(@{
62-
@"croppedImage": [croppedImageData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength],
63-
@"initialImage": [initialImageData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength],
64-
@"rectangleCoordinates": rectangleCoordinates });
60+
61+
if (self.useBase64) {
62+
self.onPictureTaken(@{
63+
@"croppedImage": [croppedImageData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength],
64+
@"initialImage": [initialImageData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength],
65+
@"rectangleCoordinates": rectangleCoordinates });
66+
} else {
67+
NSString *croppedFilePath = [NSTemporaryDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"cropped_img_%i.jpeg",(int)[NSDate date].timeIntervalSince1970]];
68+
NSString *initialFilePath = [NSTemporaryDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"initial_img_%i.jpeg",(int)[NSDate date].timeIntervalSince1970]];
69+
70+
[croppedImageData writeToFile:croppedFilePath atomically:YES];
71+
[initialImageData writeToFile:initialFilePath atomically:YES];
72+
73+
self.onPictureTaken(@{
74+
@"croppedImage": croppedFilePath,
75+
@"initialImage": initialFilePath,
76+
@"rectangleCoordinates": rectangleCoordinates });
77+
}
6578
}
6679
[self stop];
6780
}];

ios/RNPdfScannerManager.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ - (dispatch_queue_t)methodQueue
2222
RCT_EXPORT_VIEW_PROPERTY(overlayColor, UIColor)
2323
RCT_EXPORT_VIEW_PROPERTY(enableTorch, BOOL)
2424
RCT_EXPORT_VIEW_PROPERTY(useFrontCam, BOOL)
25+
RCT_EXPORT_VIEW_PROPERTY(useBase64, BOOL)
2526
RCT_EXPORT_VIEW_PROPERTY(detectionCountBeforeCapture, NSInteger)
2627
RCT_EXPORT_VIEW_PROPERTY(detectionRefreshRateInMS, NSInteger)
2728
RCT_EXPORT_VIEW_PROPERTY(saturation, float)

0 commit comments

Comments
 (0)