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

App*_*Dev 9 camera ios zbar-sdk ios8

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

谢谢

Vla*_*lov 0

如果您只需要扫描二维码,使用原生方法会更容易:

在 VC 的 .h 中添加:

#import <AVFoundation/AVFoundation.h>
@interface FEQRViewController : UIViewController <AVCaptureMetadataOutputObjectsDelegate>
Run Code Online (Sandbox Code Playgroud)

并以 .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
Run Code Online (Sandbox Code Playgroud)

  • 那么与原始问题无关的答案比没有答案更好?您还没有回答OP的问题,没有提到QR扫描仪或对其他解决方案的请求。问题是关于“Zbar”的。我不需要发布答案来让我的评论变得不那么相关或不正确 (2认同)