iOS 5 - AVCaptureDevice setting focus point and focus mode freezes the live camera picture

前端 未结 2 1681
长情又很酷
长情又很酷 2021-02-04 21:18

I\'m using the following method to set point of focus since iOS 4:

- (void) focusAtPoint:(CGPoint)point
{
    AVCaptureDevice *device = [[self captureInput] devi         


        
相关标签:
2条回答
  • 2021-02-04 21:33

    The point you've given the setFocusPointOfInterest: function is incorrect. It's the reason why it's crashing.

    Add this method to your program and use the value returned by this function

        - (CGPoint)convertToPointOfInterestFromViewCoordinates:(CGPoint)viewCoordinates 
    {
        CGPoint pointOfInterest = CGPointMake(.5f, .5f);
        CGSize frameSize = [[self videoPreviewView] frame].size;
    
        AVCaptureVideoPreviewLayer *videoPreviewLayer = [self prevLayer];
    
        if ([[self prevLayer] isMirrored]) {
            viewCoordinates.x = frameSize.width - viewCoordinates.x;
        }    
    
        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 captureSession] inputs] lastObject] 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;
    }
    
    0 讨论(0)
  • 2021-02-04 21:47

    I want to give some additional info to @Louis 's answer.

    According to Apple's documents (Please pay attention to the bold part):

    In addition, a device may support a focus point of interest. You test for support using focusPointOfInterestSupported. If it’s supported, you set the focal point using focusPointOfInterest. You pass a CGPoint where {0,0} represents the top left of the picture area, and {1,1} represents the bottom right in landscape mode with the home button on the right—this applies even if the device is in portrait mode.

    We should involve the orientation when calculate FocusPointOfInterest.

    0 讨论(0)
提交回复
热议问题