GPUImage前置摄像头点击无法工作为什么?

时间:2016-01-12 12:22:34

标签: ios objective-c iphone avfoundation gpuimage

我已经实现了以下代码,以启用水龙头来对焦相机。它与后置摄像头工作正常,但由于使用前置摄像头而失败 _videoCamera.inputCamera.isFocusPointOfInterestSupported是假的。

GOT FALSE为什么?请帮帮我。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *touch = [touches anyObject];
    CGPoint touchPoint = [touch locationInView:self.view];

    if([_videoCamera.inputCamera isFocusPointOfInterestSupported]&&[_videoCamera.inputCamera isFocusModeSupported:AVCaptureFocusModeAutoFocus])
    {

        if([_videoCamera.inputCamera lockForConfiguration :nil])
        {
            [_videoCamera.inputCamera setFocusPointOfInterest :touchPoint];
            [_videoCamera.inputCamera setFocusMode :AVCaptureFocusModeLocked];

            if([_videoCamera.inputCamera isExposurePointOfInterestSupported])
            {    CGRect screenRect = [[UIScreen mainScreen] bounds];
                CGFloat screenWidth = screenRect.size.width;
                CGFloat screenHeight = screenRect.size.height;
                double focus_x = touchPoint.x/screenWidth;
                double focus_y = touchPoint.y/screenHeight;
                [_videoCamera.inputCamera setExposurePointOfInterest:CGPointMake(focus_x,focus_y)];
                [_videoCamera.inputCamera setFocusMode:AVCaptureFocusModeAutoFocus];
                if ([_videoCamera.inputCamera isExposureModeSupported:AVCaptureExposureModeAutoExpose]){
                    [_videoCamera.inputCamera setExposureMode:AVCaptureExposureModeAutoExpose];
                }

            }
            [_videoCamera.inputCamera unlockForConfiguration];
        }
    }

}

2 个答案:

答案 0 :(得分:1)

前置摄像头是固定焦点。没有自动对焦,没有关注兴趣点。这正是iOS在您致电NO时返回isFocusPointOfInterestSupported所告诉您的。

答案 1 :(得分:0)

我认为就在这里 [_videoCamera.inputCamera setFocusPointOfInterest :touchPoint];

FocusPointOfInterest的x和y点必须在[0:1]范围内

你可以添加这两种方法

- (CGPoint)convertToPointOfInterestFromViewCoordinates:(CGPoint)viewCoordinates inFrame:(CGRect)frame withOrientation:(UIDeviceOrientation)orientation andFillMode:(GPUImageFillModeType)fillMode mirrored:(BOOL)mirrored{
CGSize frameSize = frame.size;
CGPoint pointOfInterest = CGPointMake(0.5, 0.5);

if (mirrored)
{
    viewCoordinates.x = frameSize.width - viewCoordinates.x;
}

if (fillMode == kGPUImageFillModeStretch) {
    pointOfInterest = CGPointMake(viewCoordinates.y / frameSize.height, 1.f - (viewCoordinates.x / frameSize.width));
} else {
    CGSize apertureSize = CGSizeMake(CGRectGetHeight(frame), CGRectGetWidth(frame));
    if (!CGSizeEqualToSize(apertureSize, CGSizeZero)) {
        CGPoint point = viewCoordinates;
        CGFloat apertureRatio = apertureSize.height / apertureSize.width;
        CGFloat viewRatio = frameSize.width / frameSize.height;
        CGFloat xc = .5f;
        CGFloat yc = .5f;

        if (fillMode == kGPUImageFillModePreserveAspectRatio) {
            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 (fillMode == kGPUImageFillModePreserveAspectRatioAndFill) {
            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);
    }
}

return pointOfInterest;
}


- (void)setFocus:(CGPoint)focus forDevice:(AVCaptureDevice *)device
{
    if ([device isFocusPointOfInterestSupported] && [device isFocusModeSupported:AVCaptureFocusModeAutoFocus])
    {
        NSError *error;
        if ([device lockForConfiguration:&error])
        {
            [device setFocusPointOfInterest:focus];
            [device setFocusMode:AVCaptureFocusModeAutoFocus];
            [device unlockForConfiguration];
        }
    }

    if ([device isExposurePointOfInterestSupported] && [device isExposureModeSupported:AVCaptureExposureModeAutoExpose])
    {
        NSError *error;
        if ([device lockForConfiguration:&error])
        {
            [device setExposurePointOfInterest:focus];
            [device setExposureMode:AVCaptureExposureModeAutoExpose];
            [device unlockForConfiguration];
        }
    }
}

现在你这样打电话

CGPoint pointOfInterest = [self convertToPointOfInterestFromViewCoordinates:tapPoint inFrame:cameraView.bounds withOrientation:self.currentOrientation andFillMode:cameraView.fillMode mirrored:currentVideoCamera == frontVideoCamera];

[self setFocus:pointOfInterest forDevice:currentVideoCamera.inputCamera];

希望我能提供帮助:)