【发布时间】:2014-09-21 08:55:54
【问题描述】:
我想创建一个自定义键盘,用作条形码扫描仪。 我已经完成了整个编码,但输出结果不如预期:我被要求提供相机权限(第一次),但相机没有向视图发送视频。
我认为,出于安全原因,使用键盘可能会有一些限制?!?
1.) 打开手电筒
-(void) turnFlashOn
{
AVCaptureDevice *flashLight = [AVCaptureDevice
defaultDeviceWithMediaType:AVMediaTypeVideo];
if([flashLight isTorchAvailable] && [flashLight
isTorchModeSupported:AVCaptureTorchModeOn])
{
BOOL success = [flashLight lockForConfiguration:nil];
if(success){
NSError *error;
[flashLight setTorchMode:AVCaptureTorchModeOn];
[flashLight setTorchModeOnWithLevel:1.0 error:&error];
NSLog(@"Error: %@", error);
[flashLight unlockForConfiguration];
NSLog(@"flash turned on -> OK");
}
else
{
NSLog(@"flash turn on -> ERROR");
}
}
}
这给了我这个日志输出,但闪存没有任何反应:
Error: (null)
flash turned on -> OK
2.) 扫描条形码(viewDidLoad 的一部分)
// SCANNER PART
self.captureSession = [[AVCaptureSession alloc] init];
AVCaptureDevice *videoCaptureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
NSError *error = nil;
AVCaptureDeviceInput *videoInput = [AVCaptureDeviceInput deviceInputWithDevice:videoCaptureDevice error:&error];
if(videoInput)
[self.captureSession addInput:videoInput];
else
NSLog(@"Error: %@", error);
AVCaptureMetadataOutput *metadataOutput = [[AVCaptureMetadataOutput alloc] init];
[self.captureSession addOutput:metadataOutput];
[metadataOutput setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
[metadataOutput setMetadataObjectTypes:@[AVMetadataObjectTypeQRCode, AVMetadataObjectTypeEAN13Code]];
AVCaptureVideoPreviewLayer *previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.captureSession];
camView = [[UIView alloc] initWithFrame: [[UIScreen mainScreen] bounds]];
previewLayer.frame = camView.layer.bounds;
[camView.layer addSublayer:previewLayer];
self.keyboard.barcodeView.clipsToBounds=YES;
camView.center = CGPointMake(self.keyboard.barcodeView.frame.size.width/2, self.keyboard.barcodeView.frame.size.height/2);
[self.keyboard.barcodeView addSubview:camView];
如果我按下键盘上的一个特殊键,就会调用这个键:
-(void)scanBarcodeNow{
AudioServicesPlaySystemSound(systemSoundTock);
NSLog(@"Start scanning...");
self.keyboard.barcodeView.hidden=false;
[self.keyboard.barcodeView addSubview:camView];
[self.keyboard.barcodeView setBackgroundColor:[UIColor redColor]];
[self.captureSession startRunning];
}
唯一发生的事情是,keyboard.barcodeView 将其背景颜色更改为红色。我做这个是为了看看,我所做的所有接线都应该没问题。但是没有显示来自摄像头的视频....
谁能帮帮我?
【问题讨论】:
-
你在创建这个键盘方面有什么进展吗?我想创建相同的
标签: ios keyboard ios8 barcode avcapturesession