AVFoundation Focus Mode iPad2
-
21-04-2021 - |
Domanda
I am trying to make auto focus work in my app, tested in iPad2. My problem is that when I called check methods like isFocusModeSupported:AVCaptureFocusModeContinuousAutoFocus
, it always return NO. However, I could tap to focus with other camera applications in my device
devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
for (AVCaptureDevice *tempDevice in devices) {
[tempDevice lockForConfiguration:nil];
if ([tempDevice isFocusModeSupported:AVCaptureFocusModeAutoFocus]) {
NSLog(@"Here");
}
if ([tempDevice isFocusModeSupported:AVCaptureFocusModeContinuousAutoFocus]) {
NSLog(@"Here");
}
if ([tempDevice isFocusModeSupported:AVCaptureFocusModeLocked]) {
NSLog(@"Here");
}
if ([tempDevice isFocusPointOfInterestSupported]) {
NSLog(@"Here");
}
[tempDevice unlockForConfiguration];
}
Soluzione
I was just looking into this tonight. From what I have gleaned, the iPad 2 doesn't do focus - it does exposure adjustment - so in the default Camera app, tapping the screen brings up a rect where the tap occurred indicating the area to do white point adjustment on.
Perhaps I am wrong but this is what I have turned up, and seems to be confirmed by your API (isFocusModeSupported:AVCaptureFocusModeContinuousAutoFocus
returning NO) tests.
The cameras in the iPad 2 are pretty weak - especially the front-facing one. I am surprised Apple shipped with that.
UPDATE: This is the code from the more recent AVCamDemo Apple sample that handles focus + exposure + white point. I don't think this is in the AVCam example. AVCamDemo may onlly be available as a download of the WWDC code .dmg package from the developer center - not as a single download from the web. On my iPad 2 the exposure code never gets called. //-from AVCamDemo:
- (BOOL) hasExposure
{
AVCaptureDevice *device = [[self videoInput] device];
return [device isExposureModeSupported:AVCaptureExposureModeLocked] ||
[device isExposureModeSupported:AVCaptureExposureModeAutoExpose] ||
[device isExposureModeSupported:AVCaptureExposureModeContinuousAutoExposure];
}
- (AVCaptureExposureMode) exposureMode
{
return [[[self videoInput] device] exposureMode];
}
- (void) setExposureMode:(AVCaptureExposureMode)exposureMode
{
if (exposureMode == 1) {
exposureMode = 2;
}
AVCaptureDevice *device = [[self videoInput] device];
if ([device isExposureModeSupported:exposureMode] && [device exposureMode] != exposureMode) {
NSError *error;
if ([device lockForConfiguration:&error]) {
[device setExposureMode:exposureMode];
[device unlockForConfiguration];
} else {
id delegate = [self delegate];
if ([delegate respondsToSelector:@selector(acquiringDeviceLockFailedWithError:)]) {
[delegate acquiringDeviceLockFailedWithError:error];
}
}
}
}
- (BOOL) hasWhiteBalance
{
AVCaptureDevice *device = [[self videoInput] device];
return [device isWhiteBalanceModeSupported:AVCaptureWhiteBalanceModeLocked] ||
[device isWhiteBalanceModeSupported:AVCaptureWhiteBalanceModeAutoWhiteBalance];
}
- (AVCaptureWhiteBalanceMode) whiteBalanceMode
{
return [[[self videoInput] device] whiteBalanceMode];
}
- (void) setWhiteBalanceMode:(AVCaptureWhiteBalanceMode)whiteBalanceMode
{
if (whiteBalanceMode == 1) {
whiteBalanceMode = 2;
}
AVCaptureDevice *device = [[self videoInput] device];
if ([device isWhiteBalanceModeSupported:whiteBalanceMode] && [device whiteBalanceMode] != whiteBalanceMode) {
NSError *error;
if ([device lockForConfiguration:&error]) {
[device setWhiteBalanceMode:whiteBalanceMode];
[device unlockForConfiguration];
} else {
id delegate = [self delegate];
if ([delegate respondsToSelector:@selector(acquiringDeviceLockFailedWithError:)]) {
[delegate acquiringDeviceLockFailedWithError:error];
}
}
}
}
- (BOOL) hasFocus
{
AVCaptureDevice *device = [[self videoInput] device];
return [device isFocusModeSupported:AVCaptureFocusModeLocked] ||
[device isFocusModeSupported:AVCaptureFocusModeAutoFocus] ||
[device isFocusModeSupported:AVCaptureFocusModeContinuousAutoFocus];
}
- (AVCaptureFocusMode) focusMode
{
return [[[self videoInput] device] focusMode];
}
- (void) setFocusMode:(AVCaptureFocusMode)focusMode
{
AVCaptureDevice *device = [[self videoInput] device];
if ([device isFocusModeSupported:focusMode] && [device focusMode] != focusMode) {
NSError *error;
printf(" setFocusMode \n");
if ([device lockForConfiguration:&error]) {
[device setFocusMode:focusMode];
[device unlockForConfiguration];
} else {
id delegate = [self delegate];
if ([delegate respondsToSelector:@selector(acquiringDeviceLockFailedWithError:)]) {
[delegate acquiringDeviceLockFailedWithError:error];
}
}
}
}