- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
在我的应用程序中,我显示了一个 AVCaptureVideoPreviewLayer,然后当用户使用 AVCaptureOutput 中的 captureStillImageAsynchronouslyFromConnection 函数单击按钮时捕获静止图像.在 iPhone 5 之前,这对我来说一直很有效,但它从未完成。
我的设置代码是:
...
self.imageOutput = [[AVCaptureStillImageOutput alloc] init];
NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys: AVVideoCodecJPEG, AVVideoCodecKey, nil];
[self.imageOutput setOutputSettings:outputSettings];
self.captureSession = [[[AVCaptureSession alloc] init] autorelease];
[self.captureSession addInput:self.rearFacingDeviceInput];
[self.captureSession addOutput:self.imageOutput];
[self.captureSession setSessionPreset:AVCaptureSessionPresetPhoto];
self.previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.captureSession];
self.previewLayer.frame = CGRectMake(0, 0, 320, 427);
self.previewLayer.videoGravity = AVLayerVideoGravityResizeAspect;
[self.captureSession startRunning];
[outputSettings release];
我的捕获方法是:
AVCaptureConnection *videoConnection = nil;
for (AVCaptureConnection *connection in self.imageOutput.connections){
for (AVCaptureInputPort *port in [connection inputPorts]){
if ([[port mediaType] isEqual:AVMediaTypeVideo] ){
videoConnection = connection;
break;
}
}
if (videoConnection) { break; }
}
//code to abort if not return 'soon'
...
[self.imageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error){
//use image here
}];
captureStillImageAsynchronouslyFromConnection 永远不会为我使用 iPhone5 完成
我测试过:
它不是 OS 6,因为此代码适用于已更新的 iPhone 4s 和 iPod(iPod touch(第 4 代))
captureSession 正在运行
videoConnection 不为零
imageOutput 不为零
还有:
我正在使用此方法而不是 UIImagePickerController,因为我需要将预览作为 subview 。
在捕获 Session 时调用 stopRunning 在 iPhone 5 上也需要几秒钟
最佳答案
好吧,这段代码工作正常。针对 iPhone 4 和 5 进行了测试(baseSDK 7.1,在 ARC 下)。
您必须考虑的几件事。
1) 确保正确设置 rearFacingDeviceInput,
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
[self setRearFacingDeviceInput:[AVCaptureDeviceInput deviceInputWithDevice:device error:nil]];
2) 作为 Vincent提到,会报错,尝试同时记录错误和imageSampleBuffer
3) session 的 -startRunning 和 -stopRunning 操作需要很长时间才能完成(几秒,甚至 5-6 秒),这些方法在完成所有工作之前不会返回,为避免阻塞 UI,您不应调用这些方法主线程上的方法,一种方法是使用 GCD
dispatch_queue_t serialQueue = dispatch_queue_create("queue", NULL);
dispatch_async(serialQueue, ^{
[self.captureSession startRunning];
});
如果 captureStillImageAsynchronously 仍然没有完成(为确保这一点,在 block 中添加断点,并记录所有内容),您应该检查设备的相机。我相信您的代码适用于所有 iPhone 5 设备。希望这对您有所帮助,祝您好运。
关于ios - AVCaptureOutput captureStillImageAsynchronouslyFromConnection 在 iPhone5 上永远不会完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12590330/
AVCaptureOutput 的委托(delegate)方法 didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection
我想出了一个 AVFoundation 和 ImageIO 的实现来处理我的应用程序中的照片拍摄。但是,我对此有疑问。我拍摄的图像总是很暗,即使闪光灯熄灭也是如此。这是我使用的代码:
我正在构建一个 AUGraph,并尝试通过 AVCaptureAudioDataOutput 委托(delegate)方法从输入设备获取音频。 使用 AVCaptureSession 是所解释问题的结
我有一个正在运行的 session 和一个显示在我 View 中的预览层。 我需要在我的应用程序中多次更改 AVCaptureStillImageOutput、AVCaptureMetadataOut
在我的应用程序中,我显示了一个 AVCaptureVideoPreviewLayer,然后当用户使用 AVCaptureOutput 中的 captureStillImageAsynchronousl
我是一名优秀的程序员,十分优秀!