【问题标题】:iOS Custom Keyboard - camera not workingiOS 自定义键盘 - 相机不工作
【发布时间】: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


【解决方案1】:

您返回 null 的原因是您无权访问它。它实际上不是一个错误。根据 Apple 指南,某些 API 不适用于 iOS 8 扩展(参见下面的第 3 条)。

这很糟糕,但我总是鼓励人们阅读新功能,看看他们想做的事情是否可行,然后再深入思考(节省大量时间)。一定要查看App Extension Programming Guide 了解更多信息。

【讨论】:

    猜你喜欢
    • 2014-08-11
    • 1970-01-01
    • 2014-11-25
    • 1970-01-01
    • 2012-09-12
    • 2017-12-21
    • 2020-12-04
    • 2015-06-09
    • 2018-01-02
    相关资源
    最近更新 更多