如何使用iPhone相机作为条形码扫描仪?

时间:2011-03-17 17:28:45

标签: iphone

如何使用iPhone相机扫描条形码?我已经浏览了很多链接,但是我无法找到实现它的正确方法。

4 个答案:

答案 0 :(得分:11)

我已经在一些项目中成功使用了ZBar SDK,并且可以通过tutorial on their site轻松启动并运行。

答案 1 :(得分:5)

不幸的是我无法评论,因为我在Stack Overflow上新注册了。无论如何我还想说ZBar是一个很棒的图书馆。我已经使用自定义UI成功实现了它,并且在我的应用程序中,所有内容似乎都运行良好。

我看到的唯一问题是可以以示例的形式提供一些更详细的实现指南 - 我确信这对于那些对iOS开发者来说非常有用。我一路上遇到了问题,并且非常难以追踪完整的工作示例,除了非常基本的用法之外如何实现它,并不是每个人都可以轻松地筛选类参考文档,并且仍能以合理的速度完成任何工作。我确信这会随着用户群的增长而改变,当我结束这个项目时,我会尝试编写一个很好的教程。

目前,这是基于correct information on the ZBar site given the question的基本实现:

进行测试你可能只想让你的初始视图控制器启动它,如果你还不了解导航和所有这些东西(尽管如果不是你真的需要首先完成所有这些)。< / p>

你的(UIViewController)的.h文件应该包含这样的东西(我称之为我的控制器“ScanView”):

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

@interface ScanView : UIViewController < ZBarReaderDelegate >{

}
@end

并在.m文件的viewDidLoad或viewDidAppear中(导入“ZBarSDK.h”),您可以执行以下操作:

- (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];
}

现在扫描仪会在访问此视图时立即运行,但是您需要在同一个控制器中实现didFinishPickingMediaWithInfo,以便在扫描并识别条形码后执行某些操作:

- (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];

    }

}

这就是它的全部内容。现在,您可以使用扫描的UPC的值来执行您喜欢的操作。一旦它返回UPC号码,由你(以及我们所有人)决定做什么。

答案 2 :(得分:0)

您应该看一下使用第三方库。尝试:http://redlaser.com/

- Oak

答案 3 :(得分:0)

从iOS 7.0开始,条形码扫描为now part of the AV Foundation framework