iPhone 5 前置摄像头 - 点击对焦?

Posted

技术标签:

【中文标题】iPhone 5 前置摄像头 - 点击对焦?【英文标题】:iPhone 5 front camera - tap to focus? 【发布时间】:2013-06-20 16:36:14 【问题描述】:

使用 AVFoundation 框架,我可以使用以下代码点击聚焦:

- (void) autoFocusAtPoint:(CGPoint)point

NSArray *devices = [AVCaptureDevice devices];    


    for (AVCaptureDevice *device in devices) 
        [device unlockForConfiguration];

        if ([device hasMediaType:AVMediaTypeVideo]) 

            if([device isFocusPointOfInterestSupported] && [device isFocusModeSupported:AVCaptureFocusModeAutoFocus]) 

                if([device lockForConfiguration:nil]) 

                    [device setFocusPointOfInterest:point];
                    [device setFocusMode:AVCaptureFocusModeAutoFocus];
                    [device setExposurePointOfInterest:point];
                    [device setExposureMode:AVCaptureExposureModeContinuousAutoExposure];

                    [device lockForConfiguration:nil];

                
            
        
    

这应该通过前后摄像头设备并聚焦它们。它确实会通过每个设备,但对于前置摄像头,它不会超过 isFocusPointOfInterestSupported 线。但是您可以使用 iPhone 5 轻按以对焦于前置摄像头。至少您可以在相机应用程序上进行操作。那是什么问题???

感谢您的帮助

【问题讨论】:

好像前置摄像头不支持对焦,至少iphone 4,4s做不到,只有后置摄像头可用 【参考方案1】:

我不知道iPhone 5是否支持前置摄像头对焦,但根据官方文档

https://developer.apple.com/library/ios/#documentation/AudioVideo/Conceptual/AVFoundationPG/Articles/04_MediaCapture.html#//apple_ref/doc/uid/TP40010188-CH5-SW2

iPhone 4 的前置摄像头没有对焦模式。也可能是 iPhone 4S

【讨论】:

我没有意识到。即使您可以点击它并出现一个蓝色方块,但它实际上只会改变曝光。【参考方案2】:

我不确定这是否是正确的答案,但 [device setFocusPointOfInterest:point]; 方法采用 CGPoint 采用从 (0,0) 到 (1,1) 范围内的点,其中 (0,0) 是左上角, (1,1) 是右下角。如果您直接从 UIView 将 CGPoint 传递给此方法,它将不会专注于正确的点。我建议你添加这个:

- (CGPoint)convertToPointOfInterestFromViewCoordinates:(CGPoint)viewCoordinates 
CGPoint pointOfInterest = CGPointMake(.5f, .5f);
CGSize frameSize = [[self videoPreviewView] frame].size;

AVCaptureVideoPreviewLayer *videoPreviewLayer = [self previewLayer];

if ([[videoPreviewLayer videoGravity] isEqualToString:AVLayerVideoGravityResize] ) 
    pointOfInterest = CGPointMake(viewCoordinates.y / frameSize.height, 1.f - (viewCoordinates.x / frameSize.width));
 else 
    CGRect cleanAperture;
    for (AVCaptureInputPort *port in self.visualInput.ports) 
        if ([port mediaType] == AVMediaTypeVideo) 
            cleanAperture = CMVideoFormatDescriptionGetCleanAperture([port formatDescription], YES);
            CGSize apertureSize = cleanAperture.size;
            CGPoint point = viewCoordinates;

            CGFloat apertureRatio = apertureSize.height / apertureSize.width;
            CGFloat viewRatio = frameSize.width / frameSize.height;
            CGFloat xc = .5f;
            CGFloat yc = .5f;

            if ([[videoPreviewLayer videoGravity] isEqualToString:AVLayerVideoGravityResizeAspect] ) 
                if (viewRatio > apertureRatio) 
                    CGFloat y2 = frameSize.height;
                    CGFloat x2 = frameSize.height * apertureRatio;
                    CGFloat x1 = frameSize.width;
                    CGFloat blackBar = (x1 - x2) / 2;
                    if (point.x >= blackBar && point.x <= blackBar + x2) 
                        xc = point.y / y2;
                        yc = 1.f - ((point.x - blackBar) / x2);
                    
                 else 
                    CGFloat y2 = frameSize.width / apertureRatio;
                    CGFloat y1 = frameSize.height;
                    CGFloat x2 = frameSize.width;
                    CGFloat blackBar = (y1 - y2) / 2;
                    if (point.y >= blackBar && point.y <= blackBar + y2) 
                        xc = ((point.y - blackBar) / y2);
                        yc = 1.f - (point.x / x2);
                    
                
             else if ([[videoPreviewLayer videoGravity] isEqualToString:AVLayerVideoGravityResizeAspectFill]) 
                if (viewRatio > apertureRatio) 
                    CGFloat y2 = apertureSize.width * (frameSize.width / apertureSize.height);
                    xc = (point.y + ((y2 - frameSize.height) / 2.f)) / y2;
                    yc = (frameSize.width - point.x) / frameSize.width;
                 else 
                    CGFloat x2 = apertureSize.height * (frameSize.height / apertureSize.width);
                    yc = 1.f - ((point.x + ((x2 - frameSize.width) / 2)) / x2);
                    xc = point.y / frameSize.height;
                

            

            pointOfInterest = CGPointMake(xc, yc);
            break;
        
    


return pointOfInterest;

要获得正确的 CGPoint,只需调用 [self convertToPointOfInterestFromViewCoordinates:point];,它将返回一个范围从 (0,0) 到 (1,1) 的 CGPoint,您可以将其用于 [device setFocusPointOfInterest:point];

【讨论】:

你是对的,但不幸的是这不是我要问的。不过还是谢谢。

以上是关于iPhone 5 前置摄像头 - 点击对焦?的主要内容,如果未能解决你的问题,请参考以下文章

即使成功切换到前置摄像头,iPhone 仍然显示后置摄像头的视图

iphone前置取消反向

如何实现Android手机摄像头的自动对焦

华为nova16手机配置

华为p10plus黑科技都有哪些

前置摄像头视频录制 iPhone 4?