wgpubs
wgpubs

Reputation: 8261

How to calculate focusPointOfInterest for AVCaptureDevice?

How do you calculate the focusPointOfInterest (a CGPoint value between 0,0 and 1,1) for an given AVCaptureDevice?

I've been following the code samples from the latest WWDC but I really don't understand how to calculation is being made. Also, my application is sitting in landscape vs. portrait (as in the sample) ... so in addition to not understanding how things are being calculated, I'm not sure what adjustments I need to make in order to account for landscape orientation.

Any help would be appreciated.

Thanks - wg

Upvotes: 5

Views: 3467

Answers (3)

Megabits
Megabits

Reputation: 606

You can rotate the point from screen coordinate to camera coordinate by doing this: (focusPoint is from 0,0 to 1,1)

let interfaceOrientation = (UIApplication.shared.connectedScenes.first as? UIWindowScene)?.interfaceOrientation
switch interfaceOrientation {
case .landscapeLeft:
    return focusPoint
case .landscapeRight:
    return .init(x: 1 - focusPoint.x, y: 1 - focusPoint.y)
case .portraitUpsideDown:
    return .init(x: 1 - focusPoint.y, y: focusPoint.x)
default:
    return .init(x: focusPoint.y, y: 1 - focusPoint.x)
}

Upvotes: 0

HHK
HHK

Reputation: 1467

If you have an AVCaptureVideoPreviewLayer, you can use captureDevicePointOfInterestForPoint in order to convert the CGPointyou get with a tap gesture.

That way you just don't have to worry about orientation.

Upvotes: 4

Josh Bleecher Snyder
Josh Bleecher Snyder

Reputation: 8432

According to http://developer.apple.com/library/ios/#documentation/AudioVideo/Conceptual/AVFoundationPG/Articles/03_MediaCapture.html:

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.

Upvotes: 4

Related Questions