有没有更简单的方法从iOS设备的相机捕获字节?

时间:2012-03-09 11:37:01

标签: objective-c ios

我正在iOS 5.1中使用OpenCV进行一些图像处理。我有一些库来检测标记和其他东西。

我现在需要做的是从相机中获取每帧视频,以便我可以使用OpenCV进一步处理它。我找到了一些示例代码,例如在https://developer.apple.com/library/ios/#qa/qa1702/_index.html,但似乎非常困难。我的意思是,我真的需要编写500行代码来捕获帧并将其推回到视图中吗?

有人可以给我一个提示从哪里开始?

// UPDATE 这是最简单的captureOutput:didOutputSampleBuffer:fromConnection:方法我已经能够编码了。

- (void)captureOutput:(AVCaptureOutput *)captureOutput
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
      fromConnection:(AVCaptureConnection *)connection {
  @autoreleasepool {
      CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
      CVPixelBufferLockBaseAddress(imageBuffer, 0);
      uint8_t *baseAddress = (uint8_t *)CVPixelBufferGetBaseAddress(imageBuffer);

      CGRect videoRect = CGRectMake(0.0f, 0.0f, CVPixelBufferGetWidth(imageBuffer), CVPixelBufferGetHeight(imageBuffer));
      cv::Mat mat(videoRect.size.height, videoRect.size.width, CV_8UC1, baseAddress, 0);
      [self processFrame:mat videoRect:videoRect]; //which is to be implemented in subclass - which means it's gonna do something with openCV and then openGL it to view :)
      mat.release();
      CVPixelBufferUnlockBaseAddress(imageBuffer, 0);
  }
}

0 个答案:

没有答案