gpt4 book ai didi

ios - ZBar SDK 和摄像头在 iOS 8 中无法正常工作

转载 作者:可可西里 更新时间:2023-11-01 03:00:45 25 4
gpt4 key购买 nike

我的应用程序已与 ZBar 集成。它在 iOS 7.1 及以下版本中运行良好,但在 iOS 8.0 设备中我发现相机 View 首先显示为黑色。但是,如果我将应用程序发送到后台状态,然后再次将其发送到前台,打开相机 View ,那么它就可以工作了。有人经历过吗?

谢谢

最佳答案

如果您只需要扫描二维码,使用原生方式更容易做到这一点:

在您的 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

关于ios - ZBar SDK 和摄像头在 iOS 8 中无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25969189/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com