开发者

How to use iPhone Camera as Bar Code Scanner?

How can I use the iPhone camera to scan bar codes? I have gone through many of 开发者_如何学Cthe links but I'm not able to figure out a proper way to implement this.


I have used the ZBar SDK successfully in a few projects, and it is quite easy to get up and running by following the tutorial on their site.


Unfortunately I can't comment because I'm newly registered on Stack Overflow. Anyhow I want to also say that ZBar is a great library. I have implemented it successfully with a custom UI and in my application and all seems to be working quite well.

The only issue I have seen is that some more detailed implementation guidelines could be provided in the form of examples - which I'm sure would be especially useful for those who are n00bs to iOS dev. I ran into issues along the way, and had a really hard time tracking down full working examples of how to implement it beyond very basic usage, and not everybody can easily sift through class reference documents and still get anything done at a reasonable pace. I'm sure this will change as its user-base grows, and when I wrap up this project I'll try to write a good tutorial.

For now here's a basic implementation based on the correct information on the ZBar site given the question:

for testing you may just want to have your initial view controller launch it if you don't yet understand navigation and all of that stuff (though if not you really need to get all of that out of the way first).

your (UIViewController)'s .h file should contain something like this (I've called my controller "ScanView"):

#import <UIKit/UIKit.h>
#import "ZBarSDK.h"

@interface ScanView : UIViewController < ZBarReaderDelegate >{

}
@end

and in viewDidLoad or viewDidAppear in your .m file (with "ZBarSDK.h" imported) you might do the following:

- (void)viewWillAppear:(BOOL)animated {

    [super viewWillAppear];

    //initialize the reader and provide some config instructions
    ZBarReaderViewController *reader = [ZBarReaderViewController new];
    reader.readerDelegate = self;

    //in this case I set it to be sure to scan UPC-A which helped with 
    //one of my first issues. This doesn't make it only scan as upc-a, but 
    //seemed to allow it to use this first as it initially added extra
    //zeros to all UPC-A's
    [reader.scanner setSymbology: ZBAR_UPCA
                      config: ZBAR_CFG_ENABLE
                          to: 1];
    //I haven't messed with zoom settings yet so I used this as a default
    reader.readerView.zoom = 1.0;

    //show the scanning/camera mode
    [self presentModalViewController:reader animated:YES];
}

Now the scanner will run immediately upon visiting this view, but you need to implement didFinishPickingMediaWithInfo in this same controller to do something once a barcode has been scanned and recognized:

- (void) imagePickerController: (UIImagePickerController*) reader
didFinishPickingMediaWithInfo: (NSDictionary*) info {

    //this contains your result from the scan
    id<NSFastEnumeration> results = [info objectForKey: ZBarReaderControllerResults];

    //create a symbol object to attach the response data to
    ZBarSymbol *symbol = nil;

    //add the symbol properties from the result
    //so you can access it
    for(symbol in results){

        //symbol.data holds the UPC value        
        NSString *upcString = symbol.data;

        //all we will do for now is print out the UPC 
        //to the console
        NSLog(@"the value of the scanned UPC is: %@",upcString);

        //make the reader view go away
        [reader dismissModalViewControllerAnimated: YES];

        //now lets release the reader since we are done using it
        [reader release];

    }

}

That's really all there is to it. Now you have the value of the scanned UPC to do what you like with. It's up to you (and all of us) to determine what to do once it returns the UPC number.


You should take a look at using a third party library. Try: http://redlaser.com/.

-- Oak


As of iOS 7.0, barcode scanning is now part of the AV Foundation framework.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜