【问题标题】:Camera live scanning iOS相机实时扫描 iOS
【发布时间】:2013-01-10 05:25:54
【问题描述】:

我正在开发一个 iOS 应用程序,我需要在其中进行一些对象实时扫描。为此,我需要以每秒 3 或 4 帧为例。这是我创建捕获会话的代码:

// Create an AVCaptureSession
    AVCaptureSession *captureSession = [[AVCaptureSession alloc] init];
    captureSession.sessionPreset = AVCaptureSessionPresetHigh;

    // Find a suitable AVCaptureDevice
    AVCaptureDevice *photoCaptureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

    // Create and add an AVCaptureDeviceInput
    NSError *error = nil;
    AVCaptureDeviceInput *videoInput = [AVCaptureDeviceInput deviceInputWithDevice:photoCaptureDevice error:&error];
    if(videoInput){
        [captureSession addInput:videoInput];
    }

    // Create and add an AVCaptureVideoDataOutput
    AVCaptureVideoDataOutput *videoOutput = [[AVCaptureVideoDataOutput alloc] init];
    // we want BGRA, both CoreGraphics and OpenGL work well with 'BGRA'
    NSDictionary *rgbOutputSettings = [NSDictionary dictionaryWithObject:
                                       [NSNumber numberWithInt:kCMPixelFormat_32BGRA] forKey:(id)kCVPixelBufferPixelFormatTypeKey];
    [videoOutput setVideoSettings:rgbOutputSettings];

    // Configure your output, and start the session
    dispatch_queue_t queue = dispatch_queue_create("myQueue", NULL);
    [videoOutput setSampleBufferDelegate:self queue:queue];

    if(videoOutput){
        [captureSession addOutput:videoOutput];
    }

    [captureSession startRunning];


    // Setting up the preview layer for the camera
    AVCaptureVideoPreviewLayer *previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:captureSession];
    previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
    previewLayer.frame = cameraViewCanvas.bounds;

    // ADDING FINAL VIEW layer TO THE MAIN VIEW sublayer
    [cameraViewCanvas.layer addSublayer:previewLayer];

以及在队列中调用的委托方法:

-(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection{
    if(isCapturing){
        NSLog(@"output");

        CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
        CFDictionaryRef attachments = CMCopyDictionaryOfAttachments(kCFAllocatorDefault, sampleBuffer, kCMAttachmentMode_ShouldPropagate);
        CIImage *ciImage = [[CIImage alloc] initWithCVPixelBuffer:pixelBuffer options:(NSDictionary *)attachments];

        UIImage *newFrame = [[UIImage alloc] initWithCIImage:ciImage];
        [self showImage:newFrame];
    }
}

问题是我在屏幕上看不到图像,没有错误和警告,但没有显示图像。我的问题是 - 我是否走在正确的道路上,需要在我的代码中修复什么以在屏幕上显示图像?

【问题讨论】:

  • 您能提供更多关于您的cameraViewCanvas 的信息吗?它在哪里定义和初始化等等......
  • 它只是一个@property (nonatomic, retain) IBOutlet UIView *cameraViewCanvas;来显示相机的视频输出,它不是窗口的整个大小,所以我可以添加一些其他对象,即 UIImageView 来预览我拍摄的帧还没有。
  • 所以你在InterfaceBuilder中将它添加到主视图中?
  • 是的,不要认为问题是由它引起的,因为相机层看起来不错,因为我建议从 CIImage 到 UIImage 的转换有关联,关于这个主题有一些问题,我使用了他们的一些建议,但无论如何它都不起作用。

标签: ios


【解决方案1】:

迟到了,但问题可能是由于未在主线程中设置图像(很可能在您创建的单独调度队列中调用 captureOutput)。

dispatch_async(dispatch_get_main_queue(), ^{
  [self showImage:newFrame];
});

[self performSelectorOnMainThread:@selector(showImage:) newFrame waitUntilDone:YES];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-11
    • 1970-01-01
    • 2019-03-04
    • 2017-05-11
    • 2018-06-17
    • 1970-01-01
    相关资源
    最近更新 更多