Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Use Zbar barcode reader with PhoneGap on iPhone
function onBodyLoad()
{
// this is standard with phonegap. body onload="onBodyLoad()"
document.addEventListener("deviceready",onDeviceReady,false);
}
function onDeviceReady()
{
// once device is ready, you can execute Zbar.
// this fires the Obj-C directly. I am trying to figure out the JS layer.
PhoneGap.exec("ZbarPlug.showZbar");
}
//
// ZbarPlug.h
// Phun
//
// Created by Jeff Lee on 12/12/10.
// Copyright 2010 __MyCompanyName__. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "PhoneGapCommand.h"
#import "ZBarSDK.h"
@interface ZbarPlug : PhoneGapCommand <ZBarReaderViewDelegate>
{
}
- (void)showZbar:(NSArray*)arguments withDict:(NSDictionary*)options;
@end
function ZbarPlug() {
}
ZbarPlug.prototype.getQrCode = function () {
PhoneGap.exec("ZbarPlug.showZbar");
};
PhoneGap.addConstructor( function ()
{
if (typeof window.plugins == 'undefined')
window.plugins = {};
if( typeof window.plugins.ZbarPlug == 'undefined' )
window.plugins.ZbarPlug = new ZbarPlug();
});
//
// ZbarPlug.m
// Phun
//
// Created by Jeff Lee on 12/12/10.
// Copyright 2010 __MyCompanyName__. All rights reserved.
//
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
#import "ZbarPlug.h"
@implementation ZbarPlug
- (void)showZbar:(NSArray*)arguments withDict:(NSDictionary*)options
{
// Zbar provides you a fully functional UI that automatically scans!
ZBarReaderViewController *reader = [ZBarReaderViewController new];
reader.readerDelegate = self;
ZBarImageScanner *scanner = reader.scanner;
[scanner setSymbology: ZBAR_I25
config: ZBAR_CFG_ENABLE
to: 0];
// present and release the controller
// [super appViewController] piece is where the magic happens
// PhoneGap will pass control and open the Zbar scanner!
[[super appViewController] presentModalViewController:reader animated:YES];
[reader release];
}
// This is inherited from the ZBarReaderViewDelegate
- (void) imagePickerController: (UIImagePickerController*) reader
didFinishPickingMediaWithInfo: (NSDictionary*) info
{
// ADD: get the decode results
id<NSFastEnumeration> results = [info objectForKey: ZBarReaderControllerResults];
//UIImage *image = [info objectForKey: UIImagePickerControllerOriginalImage];
ZBarSymbol *symbol = nil;
for(symbol in results)
// EXAMPLE: just grab the first barcode
break;
// This is where the magic happens to send back the
// decoded information to PhoneGap.
NSString* retStr = [ NSString stringWithFormat:@"alert(\"%@\");", symbol.data];
[ webView stringByEvaluatingJavaScriptFromString:retStr ];
[info objectForKey: UIImagePickerControllerOriginalImage];
// ADD: dismiss the controller (NB dismiss from the *reader*!)
[[super appViewController] dismissModalViewControllerAnimated:YES];
}
@end

Always get error "PhoneGapCommand.h file not found", is this plugin outdated or someone is still working on it, I need functionalities like PhoneGap Barcode Scanner, but unfortunately it does not support for interleaved 2of5 bar code :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment