Skip to content

Commit

Permalink
Add single rectangle detection, ability to customize refresh rate, an…
Browse files Browse the repository at this point in the history
…d more
  • Loading branch information
Michaël Villeneuve committed Jul 19, 2017
1 parent 01bb411 commit a3872a6
Show file tree
Hide file tree
Showing 13 changed files with 111 additions and 19 deletions.
1 change: 1 addition & 0 deletions Example/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ android {
}

dependencies {
compile project(':react-native-document-scanner')
compile project(':react-native-pdf-scanner')
compile fileTree(dir: "libs", include: ["*.jar"])
compile "com.android.support:appcompat-v7:23.0.1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import com.facebook.react.ReactApplication;
import com.reactlibrary.RNPdfScannerPackage;
import com.reactlibrary.RNPdfScannerPackage;
import com.facebook.react.ReactNativeHost;
import com.facebook.react.ReactPackage;
import com.facebook.react.shell.MainReactPackage;
Expand All @@ -24,6 +25,7 @@ public boolean getUseDeveloperSupport() {
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new MainReactPackage(),
new RNPdfScannerPackage(),
new RNPdfScannerPackage()
);
}
Expand Down
2 changes: 2 additions & 0 deletions Example/android/settings.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
rootProject.name = 'Example'
include ':react-native-document-scanner'
project(':react-native-document-scanner').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-document-scanner/android')
include ':react-native-pdf-scanner'
project(':react-native-pdf-scanner').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-pdf-scanner/android')

Expand Down
31 changes: 25 additions & 6 deletions Example/index.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
TouchableOpacity,
View
} from 'react-native';
import PdfScanner from 'react-native-pdf-scanner';
import Scanner from 'react-native-document-scanner';

export default class Example extends Component {
constructor(props) {
Expand All @@ -24,23 +24,42 @@ export default class Example extends Component {
};
}

renderDetectionType() {
switch (this.state.lastDetectionType) {
case 0:
return "Correct rectangle found"
case 1:
return "Bad angle found";
case 2:
return "Rectangle too far";
default:
return "No rectangle detected yet";
}
}

render() {
return (
<View style={styles.container}>
{this.state.image ?
<Image style={{ flex: 1, width: 300, height: 200 }} source={{ uri: `data:image/png;base64,${this.state.image}`}} resizeMode="contain" /> :
<PdfScanner
<Scanner
onPictureTaken={data => this.setState({ image: data.image })}
overlayColor="rgba(255,130,0, 0.7)"
enableTorch={this.state.flashEnabled}
brightness={0}
saturation={1}
contrast={1.1}
brightness={0.2}
saturation={0}
contrast={1.2}
onRectangleDetect={({ stableCounter, lastDetectionType }) => this.setState({ stableCounter, lastDetectionType })}
detectionCountBeforeCapture={10}
detectionRefreshRateInMS={50}
style={styles.scanner}
/>
}
<Text style={styles.instructions}>
This is a great example of react-native-pdf-scanner 🤗
({this.state.stableCounter || 0} correctly formated rectangle detected
</Text>
<Text style={styles.instructions}>
{this.renderDetectionType()}
</Text>
{this.state.image === null ?
null :
Expand Down
28 changes: 28 additions & 0 deletions Example/ios/Example.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,13 @@
remoteGlobalIDString = 58B5119B1A9E6C1200147676;
remoteInfo = RCTText;
};
D2D661241F1F336E0008DBCE /* PBXContainerItemProxy */ = {
isa = PBXContainerItemProxy;
containerPortal = D2D661051F1F336E0008DBCE /* RNPdfScanner.xcodeproj */;
proxyType = 2;
remoteGlobalIDString = 134814201AA4EA6300B7C361;
remoteInfo = RNPdfScanner;
};
D2EF802A1EFD1271008AB871 /* PBXContainerItemProxy */ = {
isa = PBXContainerItemProxy;
containerPortal = 146833FF1AC3E56700842450 /* React.xcodeproj */;
Expand Down Expand Up @@ -279,6 +286,7 @@
5E91572D1DD0AC6500FF2AA8 /* RCTAnimation.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = RCTAnimation.xcodeproj; path = "../node_modules/react-native/Libraries/NativeAnimation/RCTAnimation.xcodeproj"; sourceTree = "<group>"; };
78C398B01ACF4ADC00677621 /* RCTLinking.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = RCTLinking.xcodeproj; path = "../node_modules/react-native/Libraries/LinkingIOS/RCTLinking.xcodeproj"; sourceTree = "<group>"; };
832341B01AAA6A8300B99B32 /* RCTText.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = RCTText.xcodeproj; path = "../node_modules/react-native/Libraries/Text/RCTText.xcodeproj"; sourceTree = "<group>"; };
D2D661051F1F336E0008DBCE /* RNPdfScanner.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = RNPdfScanner.xcodeproj; path = ../../ios/RNPdfScanner.xcodeproj; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -463,6 +471,7 @@
832341AE1AAA6A7D00B99B32 /* Libraries */ = {
isa = PBXGroup;
children = (
D2D661051F1F336E0008DBCE /* RNPdfScanner.xcodeproj */,
5E91572D1DD0AC6500FF2AA8 /* RCTAnimation.xcodeproj */,
146833FF1AC3E56700842450 /* React.xcodeproj */,
00C302A71ABCB8CE00DB3ED1 /* RCTActionSheet.xcodeproj */,
Expand Down Expand Up @@ -511,6 +520,14 @@
name = Products;
sourceTree = "<group>";
};
D2D661061F1F336E0008DBCE /* Products */ = {
isa = PBXGroup;
children = (
D2D661251F1F336E0008DBCE /* libRNPdfScanner.a */,
);
name = Products;
sourceTree = "<group>";
};
D2EF800D1EFD1271008AB871 /* Products */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -680,6 +697,10 @@
ProductGroup = 146834001AC3E56700842450 /* Products */;
ProjectRef = 146833FF1AC3E56700842450 /* React.xcodeproj */;
},
{
ProductGroup = D2D661061F1F336E0008DBCE /* Products */;
ProjectRef = D2D661051F1F336E0008DBCE /* RNPdfScanner.xcodeproj */;
},
{
ProductGroup = D2EF800D1EFD1271008AB871 /* Products */;
ProjectRef = 29D3F134BEE343FFABB02DE4 /* RNPdfScanner.xcodeproj */;
Expand Down Expand Up @@ -871,6 +892,13 @@
remoteRef = 832341B41AAA6A8300B99B32 /* PBXContainerItemProxy */;
sourceTree = BUILT_PRODUCTS_DIR;
};
D2D661251F1F336E0008DBCE /* libRNPdfScanner.a */ = {
isa = PBXReferenceProxy;
fileType = archive.ar;
path = libRNPdfScanner.a;
remoteRef = D2D661241F1F336E0008DBCE /* PBXContainerItemProxy */;
sourceTree = BUILT_PRODUCTS_DIR;
};
D2EF802B1EFD1271008AB871 /* libthird-party.a */ = {
isa = PBXReferenceProxy;
fileType = archive.ar;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
</AdditionalOptions>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
buildConfiguration = "Release"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
Expand Down
2 changes: 1 addition & 1 deletion Example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"dependencies": {
"react": "16.0.0-alpha.12",
"react-native": "0.45.1",
"react-native-pdf-scanner": "file:../"
"react-native-document-scanner": "file:../"
},
"devDependencies": {
"babel-jest": "20.0.3",
Expand Down
15 changes: 14 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,42 @@ import PropTypes from 'prop-types';
const RNPdfScanner = requireNativeComponent('RNPdfScanner', PdfScanner);

class PdfScanner extends React.Component {

sendOnPictureTakenEvent(event) {
return this.props.onPictureTaken(event.nativeEvent);
}

sendOnRectanleDetectEvent(event) {
if (!this.props.onRectangleDetect) return null;
return this.props.onRectangleDetect(event.nativeEvent);
}

render() {
return (
<RNPdfScanner
{...this.props}
onPictureTaken={this.sendOnPictureTakenEvent.bind(this)}
onRectangleDetect={this.sendOnRectanleDetectEvent.bind(this)}
brightness={this.props.brightness||0}
saturation={this.props.saturation||1}
saturation={typeof this.props.saturation === 'undefined' ? 1 : this.props.saturation}
contrast={this.props.contrast||1}
detectionCountBeforeCapture={this.props.detectionCountBeforeCapture||5}
detectionRefreshRateInMS={this.props.detectionRefreshRateInMS||50}
/>
);
}
}

PdfScanner.propTypes = {
onPictureTaken: PropTypes.func,
onRectangleDetect: PropTypes.func,
overlayColor: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
enableTorch: PropTypes.bool,
saturation: PropTypes.number,
brightness: PropTypes.number,
contrast: PropTypes.number,
detectionCountBeforeCapture: PropTypes.number,
detectionRefreshRateInMS: PropTypes.number,
};

export default PdfScanner;
4 changes: 3 additions & 1 deletion ios/DocumentScannerView.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
@interface DocumentScannerView : IPDFCameraViewController <IPDFCameraViewControllerDelegate>

@property (nonatomic, copy) RCTBubblingEventBlock onPictureTaken;

@property (nonatomic, copy) RCTBubblingEventBlock onRectangleDetect;
@property (nonatomic, assign) NSInteger detectionCountBeforeCapture;
@property (assign, nonatomic) NSInteger stableCounter;


@end
18 changes: 10 additions & 8 deletions ios/DocumentScannerView.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@
#import "DocumentScannerView.h"
#import "IPDFCameraViewController.h"

@interface DocumentScannerView()
@property (assign, nonatomic) NSInteger stableCounter;
@end


@implementation DocumentScannerView

- (instancetype)init {
Expand All @@ -29,9 +24,12 @@ - (instancetype)init {
[self setContrast: self.contrast];
[self setBrightness: self.brightness];
[self setSaturation: self.saturation];

[self setDelegate: self];

NSLog(@"detectionCountBeforeCapture: %ld", (long)self.detectionCountBeforeCapture);
NSLog(@"detectionRefreshRateInMS: %ld", (long)self.detectionRefreshRateInMS);

[self start];
[self setDelegate: self];
}

return self;
Expand All @@ -47,7 +45,11 @@ - (void) didDetectRectangle:(CIRectangleFeature *)rectangle withType:(IPDFRectan
self.stableCounter = 0;
break;
}
if (self.stableCounter > 5){
if (self.onRectangleDetect) {
self.onRectangleDetect(@{@"stableCounter": @(self.stableCounter), @"lastDetectionType": @(type)});
}

if (self.stableCounter > self.detectionCountBeforeCapture){
[self captureImageWithCompletionHander:^(id data) {
if (self.onPictureTaken) {
NSData *imageData = UIImagePNGRepresentation(data);
Expand Down
1 change: 1 addition & 0 deletions ios/IPDFCameraViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ typedef NS_ENUM(NSInteger, IPDFRectangeType)
@property (nonatomic, assign) float saturation;
@property (nonatomic, assign) float contrast;
@property (nonatomic, assign) float brightness;
@property (nonatomic, assign) NSInteger detectionRefreshRateInMS;


@end
19 changes: 18 additions & 1 deletion ios/IPDFCameraViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ @interface IPDFCameraViewController () <AVCaptureVideoDataOutputSampleBufferDele
@property (nonatomic, strong) AVCaptureStillImageOutput* stillImageOutput;

@property (nonatomic, assign) BOOL forceStop;
@property (nonatomic, assign) float lastDetectionRate;

@end

Expand Down Expand Up @@ -218,9 +219,19 @@ - (void)start

[self.captureSession startRunning];

_borderDetectTimeKeeper = [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(enableBorderDetectFrame) userInfo:nil repeats:YES];
float detectionRefreshRate = _detectionRefreshRateInMS;
CGFloat detectionRefreshRateInSec = detectionRefreshRate/100;

if (_lastDetectionRate != _detectionRefreshRateInMS) {
if (_borderDetectTimeKeeper) {
[_borderDetectTimeKeeper invalidate];
}
_borderDetectTimeKeeper = [NSTimer scheduledTimerWithTimeInterval:detectionRefreshRateInSec target:self selector:@selector(enableBorderDetectFrame) userInfo:nil repeats:YES];
}

[self hideGLKView:NO completion:nil];

_lastDetectionRate = _detectionRefreshRateInMS;
}

- (void)stop
Expand Down Expand Up @@ -271,6 +282,11 @@ - (void)setBrightness:(float)brightness
_brightness = brightness;
}

- (void)setDetectionRefreshRateInMS:(NSInteger)detectionRefreshRateInMS
{
_detectionRefreshRateInMS = detectionRefreshRateInMS;
NSLog(@"lol : %ld", (long)_detectionRefreshRateInMS);
}


- (void)focusAtPoint:(CGPoint)point completionHandler:(void(^)())completionHandler
Expand Down Expand Up @@ -397,6 +413,7 @@ - (void)hideGLKView:(BOOL)hidden completion:(void(^)())completion

- (CIImage *)filteredImageUsingEnhanceFilterOnImage:(CIImage *)image
{
[self start];
return [CIFilter filterWithName:@"CIColorControls" keysAndValues:kCIInputImageKey, image, @"inputBrightness", @(self.brightness), @"inputContrast", @(self.contrast), @"inputSaturation", @(self.saturation), nil].outputImage;
}

Expand Down
5 changes: 5 additions & 0 deletions ios/RNPdfScannerManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,13 @@ - (dispatch_queue_t)methodQueue
RCT_EXPORT_MODULE()

RCT_EXPORT_VIEW_PROPERTY(onPictureTaken, RCTBubblingEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onRectangleDetect, RCTBubblingEventBlock)


RCT_EXPORT_VIEW_PROPERTY(overlayColor, UIColor)
RCT_EXPORT_VIEW_PROPERTY(enableTorch, BOOL)
RCT_EXPORT_VIEW_PROPERTY(detectionCountBeforeCapture, NSInteger)
RCT_EXPORT_VIEW_PROPERTY(detectionRefreshRateInMS, NSInteger)
RCT_EXPORT_VIEW_PROPERTY(saturation, float)
RCT_EXPORT_VIEW_PROPERTY(brightness, float)
RCT_EXPORT_VIEW_PROPERTY(contrast, float)
Expand Down

0 comments on commit a3872a6

Please sign in to comment.