2

我在http://zbar.sourceforge.net/download.html下载 ZBarSDK 1.2 当我将 ZBarReaderViewController 的 sourceType 设置为 UIImagePickerControllerSourceTypeCamera 时效果很好。但是当我将 sourceType 设置为 UIImagePickerControllerSourceTypePhotoLibrary 或 UIImagePickerControllerSourceTypeSavedPhotosAlbum 时,应用程序崩溃了,我收到如下错误:

2012-05-28 17:23:03.476 Wow[4137:10703] * 断言失败 -[ZBarReaderViewController setSourceType:], /Users/spadix/zbar/hg/sdk/iphone/ZBarReaderViewController.m:650 2012-05-28 17:23:03.626 Wow[4137:10703] *由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“尝试为 sourceType 属性设置不受支持的值 (1)”

我有谷歌这个问题,但找不到解决方案。谁能告诉我如何解决这个问题?剂量 1.2 版仅支持 UIImagePickerControllerSourceTypeCamera?剂量我下载的源女巫不好?

谢谢。

4

1 回答 1

2

如果您想选择保存的图像,基本上呈现标准的 iOS 媒体视图控制器。用户从标准 iOS 媒体控制器中选择图像后,您实例化 ZBarImageScanner 的副本以扫描图像。如果这有意义,请跳到第 5 步,看看我如何使用 ZBarImageScanner 扫描 UIImage。否则,请尝试按照以下步骤操作。希望这可以帮助。

  1. 首先向用户显示带有选项的警报。

    UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"" delegate:self  
                                              cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Camera", @"Camera   Roll", @"Photo Library", nil];
    [sheet showInView:self.view];
    
  2. 选择时,

    - (void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex {
        UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
        switch (buttonIndex) {
            case 0: sourceType = UIImagePickerControllerSourceTypeCamera; break;
            case 1: sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum; break;
            case 2: sourceType = UIImagePickerControllerSourceTypePhotoLibrary; break;
            default: break;
        }
        if ( buttonIndex <= 2 ) {
            [self presentBarcodeReader:sourceType];
        }
    }
    
  3. 当且仅当 sourceType == UIImagePickerControllerSourceTypeCamera 时才使用 ZBar 来呈现图像捕获界面。

    - (void)presentBarcodeReader:(UIImagePickerControllerSourceType)sourceType {
        if ( sourceType == UIImagePickerControllerSourceTypeCamera ) {
            ZBarReaderViewController *reader = [ZBarReaderViewController new];
            reader.readerDelegate = self;
            reader.showsZBarControls = YES;
            reader.supportedOrientationsMask = ZBarOrientationMaskAll;
    
            ZBarImageScanner *scanner = reader.scanner;
            [scanner setSymbology: ZBAR_I25
                           config: ZBAR_CFG_ENABLE
                               to: 0];
            [self presentModalViewController:reader animated:YES];
        } else {
            UIImagePickerController *mediaUI = [[UIImagePickerController alloc] init];
            mediaUI.sourceType = sourceType;
            mediaUI.mediaTypes = [UIImagePickerController         availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeSavedPhotosAlbum];
            mediaUI.allowsEditing = NO;
            mediaUI.delegate = self;
            [self presentModalViewController:mediaUI animated:YES];
        }
    }
    
  4. 我们可以通过几种方法检测使用了哪个媒体选择器

    - (void)imagePickerController:(UIImagePickerController*)reader didFinishPickingMediaWithInfo:(NSDictionary*)info {
        id<NSFastEnumeration> results = [info objectForKey: ZBarReaderControllerResults];
        if ( results ) {
            ZBarSymbol *symbol = nil;
            for (symbol in results) break;
    
            [reader dismissViewControllerAnimated:YES completion:^{
                [self partLookup:symbol.data];
            }];
        } else {
            UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage] ?         [info objectForKey:UIImagePickerControllerEditedImage] : [info         objectForKey:UIImagePickerControllerOriginalImage];
            [self scanImage:image];
        }
    }
    
  5. 和多汁的部分

    - (void)scanImage:(UIImage*)image {
        ZBarImage *zImage = [[ZBarImage alloc] initWithCGImage:image.CGImage];
        ZBarImageScanner *scanner = [[ZBarImageScanner alloc] init];
        [scanner setSymbology: ZBAR_I25
                       config: ZBAR_CFG_ENABLE
                           to: 0];
        [scanner scanImage:zImage];
        ZBarSymbolSet *set = [scanner results];
    
        for (ZBarSymbol *symbol in set) {
            NSLog(@"%@", symbol.data);
            // process symbol.data however you please.
        }
    }
    
于 2013-05-24T03:01:49.510 回答