0
我將相機視圖用作掃描條形碼並使用webview啓動其他視圖控制器的條形碼掃描器。這工作正常,我可以從webview導航到掃描儀並掃描另一個條形碼,而不會出現問題。但是,如果我用相機遠離視圖控制器並返回到視圖控制器,攝像機視圖將加載,但不再檢測條形碼。iOS:AVCaptureOutput在導航後停止工作
@implementation ProductScanViewController
NSString *loadUrl;
AVCaptureSession *_captureSession;
AVCaptureDevice *_videoDevice;
AVCaptureDeviceInput *_videoInput;
AVCaptureVideoPreviewLayer *_previewLayer;
BOOL _running;
AVCaptureMetadataOutput *_metadataOutput;
@synthesize mWebView;
- (void)viewDidLoad {
[super viewDidLoad];
NSURL *url = [NSURL URLWithString:loadUrl];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSOperationQueue *queue = [[NSOperationQueue alloc]init];
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{if([data length] > 0 && error == nil)[mWebView loadRequest:request]; else if (error != nil) NSLog(@"Error: %", error);}
];
[self setupCaptureSession];
}
- (void)setupCaptureSession {
// 1
if (_captureSession){
return;
}
// 2
_videoDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if (!_videoDevice) {
return;
}
// 3
_captureSession = [[AVCaptureSession alloc] init];
// 4
_videoInput = [[AVCaptureDeviceInput alloc] initWithDevice:_videoDevice error:nil];
// 5
if ([_captureSession canAddInput:_videoInput]) {
[_captureSession addInput:_videoInput];
}
// 6
_previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:_captureSession];
_previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
// capture and process the metadata
_metadataOutput = [[AVCaptureMetadataOutput alloc] init];
dispatch_queue_t metadataQueue =
dispatch_queue_create("com.1337labz.featurebuild.metadata", 0);
[_metadataOutput setMetadataObjectsDelegate:self
queue:metadataQueue];
if ([_captureSession canAddOutput:_metadataOutput]) {
[_captureSession addOutput:_metadataOutput];
}
}
#pragma mark - Delegate functions
- (void)captureOutput:(AVCaptureOutput *)captureOutput
didOutputMetadataObjects:(NSArray *)metadataObjects
fromConnection:(AVCaptureConnection *)connection {
[metadataObjects enumerateObjectsUsingBlock:^(AVMetadataObject *obj,
NSUInteger idx,
BOOL *stop) {
if ([obj isKindOfClass: [AVMetadataMachineReadableCodeObject class]]) {
//NSLog(@"Capture Output started");
// 3
AVMetadataMachineReadableCodeObject *code = (AVMetadataMachineReadableCodeObject*)
[_previewLayer transformedMetadataObjectForMetadataObject:obj];
// 4
Barcode * barcode = [Barcode processMetadataObject:code];
for (NSString * str in self.allowedBarcodeTypes) {
if([barcode.getBarcodeType isEqualToString:str]){
[self validBarcodeFound:(barcode)];
return;
}
}
}
}];
}
- (void) validBarcodeFound:(Barcode *)barcode{
NSLog(@"Found Barcode");
[self stopRunning];
[self.foundBarcodes addObject:barcode];
//[self showBarcodeAlert:barcode];
NSString *alertMessage = @"";
alertMessage = [alertMessage stringByAppendingString:[barcode getBarcodeType]];
NSLog([barcode getBarcodeData]);
NSLog(alertMessage);
NSLog([NSString stringWithFormat:@"%@", barcode.getBarcodeData]);
if ([barcode.getBarcodeType isEqualToString:@"org.iso.QRCode"])
{
if ([[NSString stringWithFormat:@"%lu",(unsigned long)[self.foundBarcodes count]-1] length] > 0){
NSString *input = [barcode getBarcodeData];
[NSString stringWithFormat:@"%lu",(unsigned long)[self.foundBarcodes count]-1];
NSLog(input);
if ([input length] >= 13)
{
input = [input substringToIndex:12];
}
loadUrl = [[@"http://www.mywebsite.co.uk/" stringByAppendingString:input] stringByAppendingString:@"?utm_source=iphone"];
NSLog(loadUrl);
dispatch_sync(dispatch_get_main_queue(), ^{
[self performSegueWithIdentifier:@"toWebView" sender:self];
});
}
}
當你找到條形碼?你是做什麼? – Rajesh
你是否將視圖控制器導航到另一個視圖控制器? – Rajesh
是的,它啓動了嵌入到另一個視圖控制器中的webview的segue。 – tonyony