From 9d81479bbaf96fb706ac2e31a07ab1434f93ca52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Villeneuve?= Date: Wed, 4 Oct 2017 17:41:07 +0200 Subject: [PATCH] Add ability to switch cameras --- Example/index.ios.js | 15 +++++++++++++-- index.js | 2 ++ ios/DocumentScannerView.m | 12 ------------ ios/IPDFCameraViewController.h | 1 + ios/IPDFCameraViewController.m | 24 +++++++++++++++++++++--- ios/RNPdfScannerManager.m | 1 + package.json | 2 +- 7 files changed, 39 insertions(+), 18 deletions(-) diff --git a/Example/index.ios.js b/Example/index.ios.js index d384973..ba88569 100644 --- a/Example/index.ios.js +++ b/Example/index.ios.js @@ -21,6 +21,7 @@ export default class Example extends Component { this.state = { image: null, flashEnabled: false, + useFrontCam: false, }; } @@ -46,6 +47,7 @@ export default class Example extends Component { onPictureTaken={data => this.setState({ image: data.croppedImage })} overlayColor="rgba(255,130,0, 0.7)" enableTorch={this.state.flashEnabled} + useFrontCam={this.state.useFrontCam} brightness={0.2} saturation={0} quality={0.5} @@ -69,9 +71,12 @@ export default class Example extends Component { } - this.setState({ flashEnabled: !this.state.flashEnabled })}> + this.setState({ flashEnabled: !this.state.flashEnabled })}> 📸 Flash + this.setState({ useFrontCam: !this.state.useFrontCam })}> + 📸 Front Cam + ); } @@ -89,7 +94,7 @@ const styles = StyleSheet.create({ alignItems: 'center', justifyContent: 'center' }, - flashEnabledButton: { + button: { position: 'absolute', alignItems: 'center', justifyContent: 'center', @@ -100,6 +105,12 @@ const styles = StyleSheet.create({ width: 120, backgroundColor: '#FFF', }, + left: { + left: 20, + }, + right: { + right: 20, + }, welcome: { fontSize: 20, textAlign: 'center', diff --git a/index.js b/index.js index 6e584a9..1fe634b 100644 --- a/index.js +++ b/index.js @@ -32,6 +32,7 @@ class PdfScanner extends React.Component { {...this.props} onPictureTaken={this.sendOnPictureTakenEvent.bind(this)} onRectangleDetect={this.sendOnRectanleDetectEvent.bind(this)} + useFrontCam={this.props.useFrontCam||false} brightness={this.props.brightness||0} saturation={this.props.saturation||1} contrast={this.props.contrast||1} @@ -48,6 +49,7 @@ PdfScanner.propTypes = { onRectangleDetect: PropTypes.func, overlayColor: PropTypes.oneOfType([PropTypes.number, PropTypes.string]), enableTorch: PropTypes.bool, + useFrontCam: PropTypes.bool, saturation: PropTypes.number, brightness: PropTypes.number, contrast: PropTypes.number, diff --git a/ios/DocumentScannerView.m b/ios/DocumentScannerView.m index 1158b5c..91ddb8f 100644 --- a/ios/DocumentScannerView.m +++ b/ios/DocumentScannerView.m @@ -6,19 +6,7 @@ @implementation DocumentScannerView - (instancetype)init { self = [super init]; if (self) { - [self setupCameraView]; [self setEnableBorderDetection:YES]; - - - [self setOverlayColor: self.overlayColor]; - [self setEnableTorch: self.enableTorch]; - - [self setContrast: self.contrast]; - [self setBrightness: self.brightness]; - [self setSaturation: self.saturation]; - - - [self start]; [self setDelegate: self]; } diff --git a/ios/IPDFCameraViewController.h b/ios/IPDFCameraViewController.h index 3519971..dd71178 100644 --- a/ios/IPDFCameraViewController.h +++ b/ios/IPDFCameraViewController.h @@ -36,6 +36,7 @@ typedef NS_ENUM(NSInteger, IPDFRectangeType) @property (nonatomic,assign,getter=isBorderDetectionEnabled) BOOL enableBorderDetection; @property (nonatomic,assign,getter=isTorchEnabled) BOOL enableTorch; +@property (nonatomic,assign,getter=isFrontCam) BOOL useFrontCam; @property (weak, nonatomic) id delegate; diff --git a/ios/IPDFCameraViewController.m b/ios/IPDFCameraViewController.m index b1ccf53..20ee126 100644 --- a/ios/IPDFCameraViewController.m +++ b/ios/IPDFCameraViewController.m @@ -91,8 +91,19 @@ - (void)setupCameraView { [self createGLKView]; - NSArray *possibleDevices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo]; - AVCaptureDevice *device = [possibleDevices firstObject]; + AVCaptureDevice *device = nil; + NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo]; + for (AVCaptureDevice *possibleDevice in devices) { + if (self.useFrontCam) { + if ([possibleDevice position] == AVCaptureDevicePositionFront) { + device = possibleDevice; + } + } else { + if ([possibleDevice position] != AVCaptureDevicePositionFront) { + device = possibleDevice; + } + } + } if (!device) return; _imageDedectionConfidence = 0.0; @@ -265,6 +276,14 @@ - (void)setEnableTorch:(BOOL)enableTorch } } +- (void)setUseFrontCam:(BOOL)useFrontCam +{ + _useFrontCam = useFrontCam; + [self stop]; + [self setupCameraView]; + [self start]; +} + - (void)setContrast:(float)contrast { @@ -285,7 +304,6 @@ - (void)setBrightness:(float)brightness - (void)setDetectionRefreshRateInMS:(NSInteger)detectionRefreshRateInMS { _detectionRefreshRateInMS = detectionRefreshRateInMS; - NSLog(@"lol : %ld", (long)_detectionRefreshRateInMS); } diff --git a/ios/RNPdfScannerManager.m b/ios/RNPdfScannerManager.m index 85318dc..f35440f 100644 --- a/ios/RNPdfScannerManager.m +++ b/ios/RNPdfScannerManager.m @@ -21,6 +21,7 @@ - (dispatch_queue_t)methodQueue RCT_EXPORT_VIEW_PROPERTY(overlayColor, UIColor) RCT_EXPORT_VIEW_PROPERTY(enableTorch, BOOL) +RCT_EXPORT_VIEW_PROPERTY(useFrontCam, BOOL) RCT_EXPORT_VIEW_PROPERTY(detectionCountBeforeCapture, NSInteger) RCT_EXPORT_VIEW_PROPERTY(detectionRefreshRateInMS, NSInteger) RCT_EXPORT_VIEW_PROPERTY(saturation, float) diff --git a/package.json b/package.json index 0898ff9..ba0f66e 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "react-native-document-scanner", "description": "Scan documents, automatic border detection, automatic crop", - "version": "1.2.2", + "version": "1.3.0", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1"