我正在努力解决缩放问题。我尝试使用AVCaptureSession按照link1和link2中的代码获取图像。
这一切都很好用,除非我尝试实现相机变焦,但事情并没有按照我的意愿执行。
我尝试在AVCaptureDevice对象上使用videoZoomFactor方法,如下面的代码所示。
你有什么建议为什么videoZoomFactor没有任何影响??
如何使用AVCaptureSession按预期为静态图像采集创建缩放?
- (void)addVideoInput {
AVCaptureDevice *videoDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if (videoDevice) {
// SOMEHOW NOT HAVING AN EFFECT AT ALL - WHY ?????
if ([videoDevice lockForConfiguration:nil]) {
float zoomFactor = videoDevice.activeFormat.videoZoomFactorUpscaleThreshold;
[videoDevice setVideoZoomFactor:zoomFactor];
// [videoDevice setVideoZoomFactor:3];
// videoDevice.videoZoomFactor = 3;
[videoDevice unlockForConfiguration];
}
// that all works again nicely...
NSError *error;
AVCaptureDeviceInput *videoIn = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice error:&error];
if (!error) {
if ([[self captureSession] canAddInput:videoIn]) {
[[self captureSession] addInput:videoIn];
}
else
NSLog(@"Couldn't add video input");
}
else
NSLog(@"Couldn't create video input");
}
else
NSLog(@"Couldn't create video capture device");
}
答案 0 :(得分:2)
并非所有格式都支持缩放。 如果您打印当前格式
NSLog(@"Current format: %@, max zoom factor: %f", videoDevice.activeFormat, videoDevice.activeFormat.videoMaxZoomFactor);
你可能会得到这样的东西(没有缩放级别)。
Current format: <AVCaptureDeviceFormat: 0x1559b3a0 'vide'/'420v' 1280x 720, { 1- 30 fps}, fov:49.240>, max zoom level: 1.000000
然后如果你打印所有格式
NSLog(@"All formats: %@", videoDevice.formats);
你会注意到他们中的一些人有一些没有缩放因子
All formats: (
"<AVCaptureDeviceFormat: 0x1556aef0 'vide'/'420v' 192x 144, { 1- 30 fps}, fov:64.000, binned>",
"<AVCaptureDeviceFormat: 0x1556f4e0 'vide'/'420f' 192x 144, { 1- 30 fps}, fov:64.000, binned>",
"<AVCaptureDeviceFormat: 0x1556f3e0 'vide'/'420v' 352x 288, { 1- 30 fps}, fov:61.260, binned>",
"<AVCaptureDeviceFormat: 0x1556f3f0 'vide'/'420f' 352x 288, { 1- 30 fps}, fov:61.260, binned>",
"<AVCaptureDeviceFormat: 0x1556f360 'vide'/'420v' 480x 360, { 1- 30 fps}, fov:64.000, binned>",
"<AVCaptureDeviceFormat: 0x1556f370 'vide'/'420f' 480x 360, { 1- 30 fps}, fov:64.000, binned>",
"<AVCaptureDeviceFormat: 0x1556ae30 'vide'/'420v' 640x 480, { 1- 30 fps}, fov:64.000, binned>",
"<AVCaptureDeviceFormat: 0x1556ae40 'vide'/'420f' 640x 480, { 1- 30 fps}, fov:64.000, binned>",
"<AVCaptureDeviceFormat: 0x1556f5c0 'vide'/'420v' 960x 540, { 1- 30 fps}, fov:58.460, binned>",
"<AVCaptureDeviceFormat: 0x1556f5d0 'vide'/'420f' 960x 540, { 1- 30 fps}, fov:58.460, binned>",
"<AVCaptureDeviceFormat: 0x1556f3a0 'vide'/'420v' 1280x 720, { 1- 30 fps}, fov:49.240>",
"<AVCaptureDeviceFormat: 0x1556f3b0 'vide'/'420f' 1280x 720, { 1- 30 fps}, fov:49.240>",
"<AVCaptureDeviceFormat: 0x1556aa60 'vide'/'420v' 2592x1936, { 1- 15 fps}, fov:64.000, max zoom:121.00 (upscales @1.00)>",
"<AVCaptureDeviceFormat: 0x1556aa70 'vide'/'420f' 2592x1936, { 1- 15 fps}, fov:64.000, max zoom:121.00 (upscales @1.00)>"
在我的情况下,最后两个有最大缩放。在这种情况下,我将我的activeFormat设置为最后一个(最大缩放大于1(1 =无缩放))。您不应该像我的示例(lastObject)那样使用它,您应该首先检查哪个格式的缩放大于1。
videoDevice.activeFormat = [videoDevice.formats lastObject];
现在您可以在1.0和videoMaxZoomFactor之间设置任意数字。
[videoDevice setVideoZoomFactor:5.0];
答案 1 :(得分:0)
现在使用以下代码使用它:
dispatch_async(sessionQueue, ^{
[self setBackgroundRecordingID:UIBackgroundTaskInvalid];
NSError *error = nil;
AVCaptureDevice *videoDevice = [iKKAvCamViewController deviceWithMediaType:AVMediaTypeVideo preferringPosition:AVCaptureDevicePositionBack];
AVCaptureDeviceInput *videoDeviceInput = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice error:&error];
if (error)
{
NSLog(@"%@", error);
}
if ([session canAddInput:videoDeviceInput])
{
[session addInput:videoDeviceInput];
[self setVideoDeviceInput:videoDeviceInput];
dispatch_async(dispatch_get_main_queue(), ^{
// Why are we dispatching this to the main queue?
// Because AVCaptureVideoPreviewLayer is the backing layer for AVCamPreviewView and UIView can only be manipulated on main thread.
// Note: As an exception to the above rule, it is not necessary to serialize video orientation changes on the AVCaptureVideoPreviewLayer’s connection with other session manipulation.
[[(AVCaptureVideoPreviewLayer *)[[self previewView] layer] connection] setVideoOrientation:(AVCaptureVideoOrientation)[self interfaceOrientation]];
// Apply initial VideoZoomFactor to the device
NSNumber *DefaultZoomFactor = [[NSNumber alloc] initWithFloat:3.0];
NSError *error = nil;
if ([[[self videoDeviceInput] device] lockForConfiguration:&error])
{
[[[self videoDeviceInput] device] setVideoZoomFactor:[DefaultZoomFactor floatValue]];
self.zoomFactorLabel.text = [NSString stringWithFormat:@"%.1f", [DefaultZoomFactor floatValue]];
[[[self videoDeviceInput] device] unlockForConfiguration];
}
else
{
NSLog(@"%@", error);
}
});
}
});