Skip to content
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
113 changes: 93 additions & 20 deletions src/ios/CDVBarcodeScanner.mm
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,16 @@ @interface CDVbcsProcessor : NSObject <AVCaptureVideoDataOutputSampleBufferDeleg
@property (nonatomic, retain) CDVBarcodeScanner* plugin;
@property (nonatomic, retain) NSString* callback;
@property (nonatomic, retain) UIViewController* parentViewController;
@property (nonatomic, retain) CDVbcsViewController* viewController;
@property (nonatomic, retain) CDVbcsViewController* viewController;
@property (nonatomic, retain) AVCaptureSession* captureSession;
@property (nonatomic, retain) AVCaptureVideoPreviewLayer* previewLayer;
@property (nonatomic, retain) NSString* alternateXib;
@property (nonatomic) CGFloat effectiveScale;
@property (nonatomic) BOOL is1D;
@property (nonatomic) BOOL is2D;
@property (nonatomic) BOOL capturing;

- (id)initWithPlugin:(CDVBarcodeScanner*)plugin callback:(NSString*)callback parentViewController:(UIViewController*)parentViewController alterateOverlayXib:(NSString *)alternateXib;
- (id)initWithPlugin:(CDVBarcodeScanner*)plugin callback:(NSString*)callback parentViewController:(UIViewController*)parentViewController alterateOverlayXib:(NSString *)alternateXib alterateZoomValue:(CGFloat)effectiveScale;
- (void)scanBarcode;
- (void)barcodeScanSucceeded:(NSString*)text format:(NSString*)format;
- (void)barcodeScanFailed:(NSString*)message;
Expand Down Expand Up @@ -126,13 +127,25 @@ - (void)scan:(CDVInvokedUrlCommand*)command {
NSString* capabilityError;

callback = command.callbackId;

// We allow the user to define an alternate xib file for loading the overlay.
NSString *overlayXib = nil;

// We allow the user to define a custom zoom into for the acquisition.
NSString *zoomLiteral = nil;
CGFloat zoom = 1.0;
if ( [command.arguments count] >= 1 )
{
overlayXib = [command.arguments objectAtIndex:0];
zoomLiteral = [command.arguments objectAtIndex:0];
zoom = [zoomLiteral floatValue];

}
// We allow the user to define an alternate xib file for loading the overlay
NSString *overlayXib = nil;
if ( [command.arguments count] >= 2 )
{
overlayXib = [command.arguments objectAtIndex:1];
}

NSLog(@" print zoomLiteral %@", zoomLiteral);
NSLog(@" print overlayXib %@", overlayXib);

capabilityError = [self isScanNotPossible];
if (capabilityError) {
Expand All @@ -145,6 +158,7 @@ - (void)scan:(CDVInvokedUrlCommand*)command {
callback:callback
parentViewController:self.viewController
alterateOverlayXib:overlayXib
alterateZoomValue:zoom
];

// queue [processor scanBarcode] to run on the event loop
Expand Down Expand Up @@ -204,25 +218,29 @@ @implementation CDVbcsProcessor
@synthesize is1D = _is1D;
@synthesize is2D = _is2D;
@synthesize capturing = _capturing;
@synthesize effectiveScale = _effectiveScale;

//--------------------------------------------------------------------------
- (id)initWithPlugin:(CDVBarcodeScanner*)plugin
callback:(NSString*)callback
parentViewController:(UIViewController*)parentViewController
alterateOverlayXib:(NSString *)alternateXib {
alterateOverlayXib:(NSString *)alternateXib
alterateZoomValue:(CGFloat)effectiveScale {
self = [super init];
if (!self) return self;

self.plugin = plugin;
self.callback = callback;
self.parentViewController = parentViewController;
self.effectiveScale = effectiveScale;
self.alternateXib = alternateXib;

self.is1D = YES;
self.is2D = YES;
self.capturing = NO;

return self;

}

//--------------------------------------------------------------------------
Expand All @@ -234,6 +252,7 @@ - (void)dealloc {
self.captureSession = nil;
self.previewLayer = nil;
self.alternateXib = nil;
self.effectiveScale = nil;

self.capturing = NO;

Expand Down Expand Up @@ -273,7 +292,7 @@ - (void)barcodeScanDone {
// viewcontroller holding onto a reference to us, release them so they
// will release us
self.viewController = nil;

// delayed [self release];
[self performSelector:@selector(release) withObject:nil afterDelay:1];
}
Expand Down Expand Up @@ -321,13 +340,29 @@ - (NSString*)setUpCaptureSession {
output.videoSettings = videoOutputSettings;

[output setSampleBufferDelegate:self queue:dispatch_get_main_queue()];

// Fallback to get the highest supported resolution
if([captureSession canSetSessionPreset:AVCaptureSessionPresetHigh]){

if (![captureSession canSetSessionPreset:AVCaptureSessionPresetMedium]) {
return @"unable to preset medium quality video capture";
}
[captureSession setSessionPreset:AVCaptureSessionPresetHigh];
NSLog(@"AVCaptureSessionPresetHigh!");

}else if([captureSession canSetSessionPreset:AVCaptureSessionPresetMedium]){

[captureSession setSessionPreset:AVCaptureSessionPresetMedium];
NSLog(@"AVCaptureSessionPresetMedium!");

}else if([captureSession canSetSessionPreset:AVCaptureSessionPresetLow]){

captureSession.sessionPreset = AVCaptureSessionPresetMedium;
[captureSession setSessionPreset:AVCaptureSessionPresetLow];
NSLog(@"AVCaptureSessionPresetLow!");

}else{

return @"unable to preset medium quality video capture";

}

if ([captureSession canAddInput:input]) {
[captureSession addInput:input];
}
Expand All @@ -344,10 +379,11 @@ - (NSString*)setUpCaptureSession {

// setup capture preview layer
self.previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:captureSession];

// run on next event loop pass [captureSession startRunning]
[captureSession performSelector:@selector(startRunning) withObject:nil afterDelay:0];



return nil;
}

Expand Down Expand Up @@ -378,8 +414,34 @@ - (void)captureOutput:(AVCaptureOutput*)captureOutput didOutputSampleBuffer:(CMS

// [self dumpImage: [[self getImageFromSample:sampleBuffer] autorelease]];
#endif



CGFloat effectiveScale = self.effectiveScale;

// Check the scale is in the allowed range
if (effectiveScale < 1.0)
effectiveScale = 1.0;

try {

// Apply here the scale passed to the plugin by value
[CATransaction begin];
[CATransaction setAnimationDuration:.025];
[self.previewLayer setAffineTransform:CGAffineTransformMakeScale(effectiveScale, effectiveScale)];
[CATransaction commit];

} catch (NSException * e) {

[self.previewLayer setAffineTransform:CGAffineTransformMakeScale(1.0, 1.0)];
NSLog(@"Max zoom value reached, rest to the default value!");

}

// Apply here the scale passed to the plugin by value
[CATransaction begin];
[CATransaction setAnimationDuration:.025];
[self.previewLayer setAffineTransform:CGAffineTransformMakeScale(self.effectiveScale, self.effectiveScale)];
[CATransaction commit];

using namespace zxing;

// LuminanceSource is pretty dumb; we have to give it a pointer to
Expand Down Expand Up @@ -606,6 +668,7 @@ @implementation CDVbcsViewController
@synthesize alternateXib = _alternateXib;
@synthesize overlayView = _overlayView;


//--------------------------------------------------------------------------
- (id)initWithProcessor:(CDVbcsProcessor*)processor alternateOverlay:(NSString *)alternateXib {
self = [super init];
Expand Down Expand Up @@ -636,11 +699,21 @@ - (void)loadView {
AVCaptureVideoPreviewLayer* previewLayer = self.processor.previewLayer;
previewLayer.frame = self.view.bounds;
previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;

if ([previewLayer isOrientationSupported]) {
[previewLayer setOrientation:AVCaptureVideoOrientationPortrait];

if([[previewLayer connection] isVideoOrientationSupported]){

[[previewLayer connection] setVideoOrientation: AVCaptureVideoOrientationPortrait];

}


// Added support for stabilizer
if([[previewLayer connection] isVideoStabilizationSupported]){

[previewLayer connection].enablesVideoStabilizationWhenAvailable = YES;
NSLog(@"Video stabilization activated");

}

[self.view.layer insertSublayer:previewLayer below:[[self.view.layer sublayers] objectAtIndex:0]];

[self.view addSubview:[self buildOverlayView]];
Expand Down
8 changes: 5 additions & 3 deletions www/barcodescanner.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
cordova.define("com.phonegap.plugins.barcodescanner.BarcodeScanner", function(require, exports, module) { /**
* cordova is available under *either* the terms of the modified BSD license *or* the
* MIT License (2008). See http://opensource.org/licenses/alphabetical for full text.
*
Expand Down Expand Up @@ -41,7 +41,7 @@
* }
* @param {Function} errorCallback
*/
BarcodeScanner.prototype.scan = function (successCallback, errorCallback) {
BarcodeScanner.prototype.scan = function (successCallback, errorCallback, options) {
if (errorCallback == null) {
errorCallback = function () {
};
Expand All @@ -57,7 +57,7 @@
return;
}

exec(successCallback, errorCallback, 'BarcodeScanner', 'scan', []);
exec(successCallback, errorCallback, 'BarcodeScanner', 'scan', options);
};

//-------------------------------------------------------------------
Expand Down Expand Up @@ -85,3 +85,5 @@
var barcodeScanner = new BarcodeScanner();
module.exports = barcodeScanner;


});