Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for front face camera. #3

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 13 additions & 20 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,31 +40,24 @@
<framework src="QuartzCore.framework" />
</platform>

<platform name="wp7">
<config-file target="config.xml" parent="/*">
<!-- wp8 -->
<platform name="wp8">
<config-file target="config.xml" parent="/*">
<feature name="BarcodeScanner">
<param name="wp-package" value="BarcodeScanner"/>
</feature>
</config-file>

<source-file src="src/wp/BarcodeScanner.cs" />
<source-file src="src/wp/BarcodeScannerResult.cs" />
<source-file src="src/wp/Scan.xaml.cs" />
<source-file src="src/wp/Scan.xaml" />
</platform>

<platform name="wp8">
<config-file target="config.xml" parent="/*">
<feature name="BarcodeScanner">
<param name="wp-package" value="BarcodeScanner"/>
</feature>

<config-file target="Properties/WMAppManifest.xml" parent="/Deployment/App/Capabilities">
<Capability Name="ID_CAP_ISV_CAMERA" />
</config-file>

<source-file src="src/wp/BarcodeScanner.cs" />
<source-file src="src/wp/BarcodeScannerResult.cs" />
<source-file src="src/wp/Scan.xaml.cs" />
<source-file src="src/wp/Scan.xaml" />
</platform>

<source-file src="src/wp8/BarcodeScanner.cs" />
<source-file src="src/wp8/BarcodeScannerResult.cs" />
<source-file src="src/wp8/Scan.xaml" />
<source-file src="src/wp8/Scan.xaml.cs" />
<source-file src="src/wp8/zxing.wp8.0.dll" />
</platform>

<!-- android -->
<platform name="android">
Expand Down
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
46 changes: 0 additions & 46 deletions src/wp/BarcodeScanner.cs

This file was deleted.

22 changes: 0 additions & 22 deletions src/wp/BarcodeScannerResult.cs

This file was deleted.

40 changes: 0 additions & 40 deletions src/wp/Scan.xaml

This file was deleted.

Loading