Skip to content

Commit 9d81479

Browse files
author
Michaël Villeneuve
committed
Add ability to switch cameras
1 parent 4da76d5 commit 9d81479

File tree

7 files changed

+39
-18
lines changed

7 files changed

+39
-18
lines changed

Example/index.ios.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export default class Example extends Component {
2121
this.state = {
2222
image: null,
2323
flashEnabled: false,
24+
useFrontCam: false,
2425
};
2526
}
2627

@@ -46,6 +47,7 @@ export default class Example extends Component {
4647
onPictureTaken={data => this.setState({ image: data.croppedImage })}
4748
overlayColor="rgba(255,130,0, 0.7)"
4849
enableTorch={this.state.flashEnabled}
50+
useFrontCam={this.state.useFrontCam}
4951
brightness={0.2}
5052
saturation={0}
5153
quality={0.5}
@@ -69,9 +71,12 @@ export default class Example extends Component {
6971
</TouchableOpacity>
7072
}
7173

72-
<TouchableOpacity style={styles.flashEnabledButton} onPress={() => this.setState({ flashEnabled: !this.state.flashEnabled })}>
74+
<TouchableOpacity style={[styles.button, styles.left]} onPress={() => this.setState({ flashEnabled: !this.state.flashEnabled })}>
7375
<Text>📸 Flash</Text>
7476
</TouchableOpacity>
77+
<TouchableOpacity style={[styles.button, styles.right]} onPress={() => this.setState({ useFrontCam: !this.state.useFrontCam })}>
78+
<Text>📸 Front Cam</Text>
79+
</TouchableOpacity>
7580
</View>
7681
);
7782
}
@@ -89,7 +94,7 @@ const styles = StyleSheet.create({
8994
alignItems: 'center',
9095
justifyContent: 'center'
9196
},
92-
flashEnabledButton: {
97+
button: {
9398
position: 'absolute',
9499
alignItems: 'center',
95100
justifyContent: 'center',
@@ -100,6 +105,12 @@ const styles = StyleSheet.create({
100105
width: 120,
101106
backgroundColor: '#FFF',
102107
},
108+
left: {
109+
left: 20,
110+
},
111+
right: {
112+
right: 20,
113+
},
103114
welcome: {
104115
fontSize: 20,
105116
textAlign: 'center',

index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class PdfScanner extends React.Component {
3232
{...this.props}
3333
onPictureTaken={this.sendOnPictureTakenEvent.bind(this)}
3434
onRectangleDetect={this.sendOnRectanleDetectEvent.bind(this)}
35+
useFrontCam={this.props.useFrontCam||false}
3536
brightness={this.props.brightness||0}
3637
saturation={this.props.saturation||1}
3738
contrast={this.props.contrast||1}
@@ -48,6 +49,7 @@ PdfScanner.propTypes = {
4849
onRectangleDetect: PropTypes.func,
4950
overlayColor: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
5051
enableTorch: PropTypes.bool,
52+
useFrontCam: PropTypes.bool,
5153
saturation: PropTypes.number,
5254
brightness: PropTypes.number,
5355
contrast: PropTypes.number,

ios/DocumentScannerView.m

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,7 @@ @implementation DocumentScannerView
66
- (instancetype)init {
77
self = [super init];
88
if (self) {
9-
[self setupCameraView];
109
[self setEnableBorderDetection:YES];
11-
12-
13-
[self setOverlayColor: self.overlayColor];
14-
[self setEnableTorch: self.enableTorch];
15-
16-
[self setContrast: self.contrast];
17-
[self setBrightness: self.brightness];
18-
[self setSaturation: self.saturation];
19-
20-
21-
[self start];
2210
[self setDelegate: self];
2311
}
2412

ios/IPDFCameraViewController.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ typedef NS_ENUM(NSInteger, IPDFRectangeType)
3636

3737
@property (nonatomic,assign,getter=isBorderDetectionEnabled) BOOL enableBorderDetection;
3838
@property (nonatomic,assign,getter=isTorchEnabled) BOOL enableTorch;
39+
@property (nonatomic,assign,getter=isFrontCam) BOOL useFrontCam;
3940

4041
@property (weak, nonatomic) id<IPDFCameraViewControllerDelegate> delegate;
4142

ios/IPDFCameraViewController.m

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,19 @@ - (void)setupCameraView
9191
{
9292
[self createGLKView];
9393

94-
NSArray *possibleDevices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
95-
AVCaptureDevice *device = [possibleDevices firstObject];
94+
AVCaptureDevice *device = nil;
95+
NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
96+
for (AVCaptureDevice *possibleDevice in devices) {
97+
if (self.useFrontCam) {
98+
if ([possibleDevice position] == AVCaptureDevicePositionFront) {
99+
device = possibleDevice;
100+
}
101+
} else {
102+
if ([possibleDevice position] != AVCaptureDevicePositionFront) {
103+
device = possibleDevice;
104+
}
105+
}
106+
}
96107
if (!device) return;
97108

98109
_imageDedectionConfidence = 0.0;
@@ -265,6 +276,14 @@ - (void)setEnableTorch:(BOOL)enableTorch
265276
}
266277
}
267278

279+
- (void)setUseFrontCam:(BOOL)useFrontCam
280+
{
281+
_useFrontCam = useFrontCam;
282+
[self stop];
283+
[self setupCameraView];
284+
[self start];
285+
}
286+
268287

269288
- (void)setContrast:(float)contrast
270289
{
@@ -285,7 +304,6 @@ - (void)setBrightness:(float)brightness
285304
- (void)setDetectionRefreshRateInMS:(NSInteger)detectionRefreshRateInMS
286305
{
287306
_detectionRefreshRateInMS = detectionRefreshRateInMS;
288-
NSLog(@"lol : %ld", (long)_detectionRefreshRateInMS);
289307
}
290308

291309

ios/RNPdfScannerManager.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ - (dispatch_queue_t)methodQueue
2121

2222
RCT_EXPORT_VIEW_PROPERTY(overlayColor, UIColor)
2323
RCT_EXPORT_VIEW_PROPERTY(enableTorch, BOOL)
24+
RCT_EXPORT_VIEW_PROPERTY(useFrontCam, BOOL)
2425
RCT_EXPORT_VIEW_PROPERTY(detectionCountBeforeCapture, NSInteger)
2526
RCT_EXPORT_VIEW_PROPERTY(detectionRefreshRateInMS, NSInteger)
2627
RCT_EXPORT_VIEW_PROPERTY(saturation, float)

package.json

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

0 commit comments

Comments
 (0)