ZBar SDK和相机在iOS 8中无法正常工作

时间:2014-09-22 07:57:44

标签: ios camera ios8 zbar-sdk

我将我的应用与ZBar集成在一起。它在iOS 7.1及更低版本中运行良好,但在iOS 8.0设备中,我发现摄像机视图首先以黑色显示。但是,如果我将应用程序发送到后台状态并再次将其发送到前台,让相机视图打开,那么它可以工作。有人经历过这个吗?

由于

2 个答案:

答案 0 :(得分:0)

如果您只需要二维码扫描,那么使用本机方式更容易实现:

在VC的.h中添加:

#import <AVFoundation/AVFoundation.h>
@interface FEQRViewController : UIViewController <AVCaptureMetadataOutputObjectsDelegate>

在.m

@interface FEQRViewController ()

@property (nonatomic) BOOL isReading;

@property (nonatomic, strong) AVCaptureSession *captureSession;
@property (nonatomic, strong) AVCaptureVideoPreviewLayer *videoPreviewLayer;

-(BOOL)startReading;

-(void)stopReading;

@end

@implementation FEQRViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = ....;


    self.isReading = NO;
    self.captureSession = nil;


    // Do any additional setup after loading the view from its nib.
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    if (!self.isReading) {
        if ([self startReading]) {
            //[self.startButton setTitle:@"Stop" forState:UIControlStateNormal];
            [self.statusLabel setText:@"Scanning for QR Code..." ];
        }
    }
    else{
        [self stopReading];
        [self.startButton setTitle:@"Start!" forState:UIControlStateNormal];
    }

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(BOOL)startReading
{
    NSError *error;
    AVCaptureDevice *captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

    AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:captureDevice error:&error];

    if (!input) {
        NSLog(@"%@", [error localizedDescription]);
        return NO;
    }

    self.captureSession = [[AVCaptureSession alloc] init];
    [self.captureSession addInput:input];

    AVCaptureMetadataOutput *captureMetadataOutput = [[AVCaptureMetadataOutput alloc] init];
    [self.captureSession addOutput:captureMetadataOutput];

    dispatch_queue_t dispatchQueue;
    dispatchQueue = dispatch_queue_create("myQueue", NULL);
    [captureMetadataOutput setMetadataObjectsDelegate:self queue:dispatchQueue];
    [captureMetadataOutput setMetadataObjectTypes:[NSArray arrayWithObject:AVMetadataObjectTypeQRCode]];

    self.videoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.captureSession];
    [self.videoPreviewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
    [self.videoPreviewLayer setFrame:self.preview.layer.bounds];
    [self.preview.layer addSublayer:_videoPreviewLayer];

    [_captureSession startRunning];
    return YES;
}

-(void)stopReading
{
    [self.captureSession stopRunning];

    self.captureSession = nil;
    [self.videoPreviewLayer removeFromSuperlayer];

}


-(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection{
    if (metadataObjects != nil && [metadataObjects count] > 0) {

        AVMetadataMachineReadableCodeObject *metadataObj = [metadataObjects objectAtIndex:0];
        if ([[metadataObj type] isEqualToString:AVMetadataObjectTypeQRCode]) {
            [self.statusLabel performSelectorOnMainThread:@selector(setText:) withObject:[metadataObj stringValue] waitUntilDone:NO];
            NSURL *url = [NSURL URLWithString:[metadataObj stringValue]];
            if (url)

                [self performSelectorOnMainThread:@selector(goToURL:) withObject:url waitUntilDone:NO];

            [self performSelectorOnMainThread:@selector(stopReading) withObject:nil waitUntilDone:NO];
            //[self.startButton performSelectorOnMainThread:@selector(setTitle:) withObject:@"Start!" waitUntilDone:NO];
            _isReading = NO;
        }
    }
}

-(void)goToURL:(NSURL *)url
{
   //Handle URL...
}

- (IBAction)startButton:(id)sender {

    if (!self.isReading) {
        if ([self startReading]) {
            [self.startButton setTitle:@"Stop" forState:UIControlStateNormal];
            [self.statusLabel setText:@"Scanning for QR Code..." ];
        }
    }
    else{
        [self stopReading];
        [self.startButton setTitle:@"Start!" forState:UIControlStateNormal];
    }

    _isReading = !_isReading;
}

@end

答案 1 :(得分:0)

这适用于iOS 8

ZBarReaderViewController *reader = [ZBarReaderViewController new];
reader.readerDelegate = self;
reader.supportedOrientationsMask = ZBarOrientationMaskAll;
[self presentViewController:reader animated:YES completion:nil];
[reader viewWillAppear:NO];`