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

Commit

Permalink
Merge branch '2.0-wp8'
Browse files Browse the repository at this point in the history
  • Loading branch information
hypery2k committed Mar 1, 2015
2 parents 9e7b480 + 281156a commit 3e916a9
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 16 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
THE SOFTWARE.
2 changes: 1 addition & 1 deletion plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -315,4 +315,4 @@
<source-file src="src/wp8/BarcodeScannerUI.xaml.cs" />

</platform>
</plugin>
</plugin>
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
public void scan() {
Intent intentScan = new Intent(SCAN_INTENT);
intentScan.addCategory(Intent.CATEGORY_DEFAULT);
// avoid calling other phonegap apps
intentScan.setPackage(this.cordova.getActivity().getApplicationContext().getPackageName());

this.cordova.startActivityForResult((CordovaPlugin) this, intentScan, REQUEST_CODE);
}
Expand Down Expand Up @@ -163,6 +165,8 @@ public void encode(String type, String data) {
Intent intentEncode = new Intent(ENCODE_INTENT);
intentEncode.putExtra(ENCODE_TYPE, type);
intentEncode.putExtra(ENCODE_DATA, data);
// avoid calling other phonegap apps
intentEncode.setPackage(this.cordova.getActivity().getApplicationContext().getPackageName());

this.cordova.getActivity().startActivity(intentEncode);
}
Expand Down
24 changes: 24 additions & 0 deletions src/browser/BarcodeScannerProxy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
function scan(success, error) {
var code = window.prompt("Enter barcode value (empty value will fire the error handler):");
if(code) {
var result = {
text:code,
format:"Fake",
cancelled:false
};
success(result);
} else {
error("No barcode");
}
}

function encode(type, data, success, errorCallback) {
success();
}

module.exports = {
scan: scan,
encode: encode
};

require("cordova/exec/proxy").add("BarcodeScanner",module.exports);
73 changes: 60 additions & 13 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 @@ -399,7 +433,7 @@ - (void)captureOutput:(AVCaptureOutput*)captureOutput didOutputSampleBuffer:(CMS
decodeHints.addFormat(BarcodeFormat_EAN_13);
decodeHints.addFormat(BarcodeFormat_CODE_128);
decodeHints.addFormat(BarcodeFormat_CODE_39);
// decodeHints.addFormat(BarcodeFormat_ITF); causing crashes
decodeHints.addFormat(BarcodeFormat_ITF);

// here's the meat of the decode process
Ref<LuminanceSource> luminanceSource ([self getLuminanceSourceFromSample: sampleBuffer imageBytes:&imageBytes]);
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
2 changes: 1 addition & 1 deletion src/wp8/BarcodeScannerUI.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
* Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
Expand Down
Binary file added src/wp8/assets/cancel.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 3e916a9

Please sign in to comment.