Skip to content
This repository has been archived by the owner on Aug 11, 2020. It is now read-only.

Commit

Permalink
Added support for front face camera. Signed-off-by: Emil Marashliev <…
Browse files Browse the repository at this point in the history
…marashliev@gmail.com>
  • Loading branch information
emarashliev authored and EddyVerbruggen committed Jul 24, 2014
1 parent ef1155d commit d3c8ad2
Showing 1 changed file with 59 additions and 12 deletions.
71 changes: 59 additions & 12 deletions src/ios/CDVBarcodeScanner.mm
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ @interface CDVBarcodeScanner : CDVPlugin {}
- (NSString*)isScanNotPossible;
- (void)scan:(CDVInvokedUrlCommand*)command;
- (void)encode:(CDVInvokedUrlCommand*)command;
- (void)returnSuccess:(NSString*)scannedText format:(NSString*)format cancelled:(BOOL)cancelled callback:(NSString*)callback;
- (void)returnSuccess:(NSString*)scannedText format:(NSString*)format cancelled:(BOOL)cancelled flipped:(BOOL)flipped callback:(NSString*)callback;
- (void)returnError:(NSString*)message callback:(NSString*)callback;
@end

Expand All @@ -66,6 +66,9 @@ @interface CDVbcsProcessor : NSObject <AVCaptureVideoDataOutputSampleBufferDeleg
@property (nonatomic) BOOL is1D;
@property (nonatomic) BOOL is2D;
@property (nonatomic) BOOL capturing;
@property (nonatomic) BOOL isFrontCamera;
@property (nonatomic) BOOL isFlipped;


- (id)initWithPlugin:(CDVBarcodeScanner*)plugin callback:(NSString*)callback parentViewController:(UIViewController*)parentViewController alterateOverlayXib:(NSString *)alternateXib;
- (void)scanBarcode;
Expand Down Expand Up @@ -146,7 +149,9 @@ - (void)scan:(CDVInvokedUrlCommand*)command {
parentViewController:self.viewController
alterateOverlayXib:overlayXib
];

[processor retain];
[processor retain];
[processor retain];
// queue [processor scanBarcode] to run on the event loop
[processor performSelector:@selector(scanBarcode) withObject:nil afterDelay:0];
}
Expand All @@ -157,7 +162,7 @@ - (void)encode:(CDVInvokedUrlCommand*)command {
}

//--------------------------------------------------------------------------
- (void)returnSuccess:(NSString*)scannedText format:(NSString*)format cancelled:(BOOL)cancelled callback:(NSString*)callback {
- (void)returnSuccess:(NSString*)scannedText format:(NSString*)format cancelled:(BOOL)cancelled flipped:(BOOL)flipped callback:(NSString*)callback{
NSNumber* cancelledNumber = [NSNumber numberWithInt:(cancelled?1:0)];

NSMutableDictionary* resultDict = [[[NSMutableDictionary alloc] init] autorelease];
Expand All @@ -171,8 +176,9 @@ - (void)returnSuccess:(NSString*)scannedText format:(NSString*)format cancelled:
];

NSString* js = [result toSuccessCallbackString:callback];

[self writeJavascript:js];
if (!flipped) {
[self writeJavascript:js];
}
}

//--------------------------------------------------------------------------
Expand Down Expand Up @@ -242,6 +248,9 @@ - (void)dealloc {

//--------------------------------------------------------------------------
- (void)scanBarcode {

// self.captureSession = nil;
// self.previewLayer = nil;
NSString* errorMessage = [self setUpCaptureSession];
if (errorMessage) {
[self barcodeScanFailed:errorMessage];
Expand Down Expand Up @@ -281,7 +290,7 @@ - (void)barcodeScanDone {
//--------------------------------------------------------------------------
- (void)barcodeScanSucceeded:(NSString*)text format:(NSString*)format {
[self barcodeScanDone];
[self.plugin returnSuccess:text format:format cancelled:FALSE callback:self.callback];
[self.plugin returnSuccess:text format:format cancelled:FALSE flipped:FALSE callback:self.callback];
}

//--------------------------------------------------------------------------
Expand All @@ -293,7 +302,19 @@ - (void)barcodeScanFailed:(NSString*)message {
//--------------------------------------------------------------------------
- (void)barcodeScanCancelled {
[self barcodeScanDone];
[self.plugin returnSuccess:@"" format:@"" cancelled:TRUE callback:self.callback];
[self.plugin returnSuccess:@"" format:@"" cancelled:TRUE flipped:self.isFlipped callback:self.callback];
if (self.isFlipped) {
self.isFlipped = NO;
}
}


- (void)flipCamera
{
self.isFlipped = YES;
self.isFrontCamera = !self.isFrontCamera;
[self performSelector:@selector(barcodeScanCancelled) withObject:nil afterDelay:0];
[self performSelector:@selector(scanBarcode) withObject:nil afterDelay:0.1];
}

//--------------------------------------------------------------------------
Expand All @@ -303,8 +324,21 @@ - (NSString*)setUpCaptureSession {
AVCaptureSession* captureSession = [[[AVCaptureSession alloc] init] autorelease];
self.captureSession = captureSession;

AVCaptureDevice* device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if (!device) return @"unable to obtain video capture device";
AVCaptureDevice* __block device = nil;
if (self.isFrontCamera) {

NSArray* devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
[devices enumerateObjectsUsingBlock:^(AVCaptureDevice *obj, NSUInteger idx, BOOL *stop) {
if (obj.position == AVCaptureDevicePositionFront) {
device = obj;
}
}];
} else {
device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if (!device) return @"unable to obtain video capture device";

}


AVCaptureDeviceInput* input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
if (!input) return @"unable to obtain video capture device input";
Expand Down Expand Up @@ -621,7 +655,7 @@ - (id)initWithProcessor:(CDVbcsProcessor*)processor alternateOverlay:(NSString *
//--------------------------------------------------------------------------
- (void)dealloc {
self.view = nil;
self.processor = nil;
// self.processor = nil;
self.shutterPressed = NO;
self.alternateXib = nil;
self.overlayView = nil;
Expand Down Expand Up @@ -679,6 +713,11 @@ - (IBAction)cancelButtonPressed:(id)sender {
[self.processor performSelector:@selector(barcodeScanCancelled) withObject:nil afterDelay:0];
}

- (void)flipCameraButtonPressed:(id)sender
{
[self.processor performSelector:@selector(flipCamera) withObject:nil afterDelay:0];
}

//--------------------------------------------------------------------------
- (UIView *)buildOverlayViewFromXib
{
Expand Down Expand Up @@ -717,22 +756,30 @@ - (UIView*)buildOverlayView {
action:@selector(cancelButtonPressed:)
];


id flexSpace = [[[UIBarButtonItem alloc] autorelease]
initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil
];

id flipCamera = [[[UIBarButtonItem alloc] autorelease]
initWithBarButtonSystemItem:UIBarButtonSystemItemCamera
target:(id)self
action:@selector(flipCameraButtonPressed:)
];


#if USE_SHUTTER
id shutterButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemCamera
target:(id)self
action:@selector(shutterButtonPressed)
];

toolbar.items = [NSArray arrayWithObjects:flexSpace,cancelButton,flexSpace,shutterButton,nil];
toolbar.items = [NSArray arrayWithObjects:flexSpace,cancelButton,flexSpace, flipCamera ,shutterButton,nil];
#else
toolbar.items = [NSArray arrayWithObjects:flexSpace,cancelButton,flexSpace,nil];
toolbar.items = [NSArray arrayWithObjects:flexSpace,cancelButton,flexSpace, flipCamera,nil];
#endif
bounds = overlayView.bounds;

Expand Down

0 comments on commit d3c8ad2

Please sign in to comment.