Skip to content

Commit

Permalink
Add ability to switch cameras
Browse files Browse the repository at this point in the history
  • Loading branch information
Michaël Villeneuve committed Oct 4, 2017
1 parent 4da76d5 commit 9d81479
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 18 deletions.
15 changes: 13 additions & 2 deletions Example/index.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export default class Example extends Component {
this.state = {
image: null,
flashEnabled: false,
useFrontCam: false,
};
}

Expand All @@ -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}
Expand All @@ -69,9 +71,12 @@ export default class Example extends Component {
</TouchableOpacity>
}

<TouchableOpacity style={styles.flashEnabledButton} onPress={() => this.setState({ flashEnabled: !this.state.flashEnabled })}>
<TouchableOpacity style={[styles.button, styles.left]} onPress={() => this.setState({ flashEnabled: !this.state.flashEnabled })}>
<Text>📸 Flash</Text>
</TouchableOpacity>
<TouchableOpacity style={[styles.button, styles.right]} onPress={() => this.setState({ useFrontCam: !this.state.useFrontCam })}>
<Text>📸 Front Cam</Text>
</TouchableOpacity>
</View>
);
}
Expand All @@ -89,7 +94,7 @@ const styles = StyleSheet.create({
alignItems: 'center',
justifyContent: 'center'
},
flashEnabledButton: {
button: {
position: 'absolute',
alignItems: 'center',
justifyContent: 'center',
Expand All @@ -100,6 +105,12 @@ const styles = StyleSheet.create({
width: 120,
backgroundColor: '#FFF',
},
left: {
left: 20,
},
right: {
right: 20,
},
welcome: {
fontSize: 20,
textAlign: 'center',
Expand Down
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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,
Expand Down
12 changes: 0 additions & 12 deletions ios/DocumentScannerView.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}

Expand Down
1 change: 1 addition & 0 deletions ios/IPDFCameraViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<IPDFCameraViewControllerDelegate> delegate;

Expand Down
24 changes: 21 additions & 3 deletions ios/IPDFCameraViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -265,6 +276,14 @@ - (void)setEnableTorch:(BOOL)enableTorch
}
}

- (void)setUseFrontCam:(BOOL)useFrontCam
{
_useFrontCam = useFrontCam;
[self stop];
[self setupCameraView];
[self start];
}


- (void)setContrast:(float)contrast
{
Expand All @@ -285,7 +304,6 @@ - (void)setBrightness:(float)brightness
- (void)setDetectionRefreshRateInMS:(NSInteger)detectionRefreshRateInMS
{
_detectionRefreshRateInMS = detectionRefreshRateInMS;
NSLog(@"lol : %ld", (long)_detectionRefreshRateInMS);
}


Expand Down
1 change: 1 addition & 0 deletions ios/RNPdfScannerManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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"
Expand Down

0 comments on commit 9d81479

Please sign in to comment.