72 lines
2.7 KiB
Plaintext
72 lines
2.7 KiB
Plaintext
|
|
#import <AVFoundation/AVFoundation.h>
|
|||
|
|
#import <UIKit/UIKit.h>
|
|||
|
|
|
|||
|
|
/// 获取当前正在使用的后置摄像头设备
|
|||
|
|
static AVCaptureDevice *GetActiveRearCamera(void)
|
|||
|
|
{
|
|||
|
|
// 遍历所有正在运行的 AVCaptureSession,找到后置摄像头的输入
|
|||
|
|
if (@available(iOS 10.0, *)) {
|
|||
|
|
AVCaptureDeviceDiscoverySession *session = [AVCaptureDeviceDiscoverySession
|
|||
|
|
discoverySessionWithDeviceTypes:@[AVCaptureDeviceTypeBuiltInWideAngleCamera]
|
|||
|
|
mediaType:AVMediaTypeVideo
|
|||
|
|
position:AVCaptureDevicePositionBack];
|
|||
|
|
for (AVCaptureDevice *d in session.devices) {
|
|||
|
|
if (d.deviceType == AVCaptureDeviceTypeBuiltInWideAngleCamera) {
|
|||
|
|
return d;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
extern "C" {
|
|||
|
|
|
|||
|
|
/// 开启连续自动对焦
|
|||
|
|
void _EnableContinuousAutoFocus(void)
|
|||
|
|
{
|
|||
|
|
AVCaptureDevice *device = GetActiveRearCamera();
|
|||
|
|
if (!device) return;
|
|||
|
|
|
|||
|
|
NSError *error = nil;
|
|||
|
|
if ([device lockForConfiguration:&error]) {
|
|||
|
|
if ([device isFocusModeSupported:AVCaptureFocusModeContinuousAutoFocus]) {
|
|||
|
|
device.focusMode = AVCaptureFocusModeContinuousAutoFocus;
|
|||
|
|
NSLog(@"[CameraFocusHelper] 已开启连续自动对焦");
|
|||
|
|
}
|
|||
|
|
if ([device isExposureModeSupported:AVCaptureExposureModeContinuousAutoExposure]) {
|
|||
|
|
device.exposureMode = AVCaptureExposureModeContinuousAutoExposure;
|
|||
|
|
}
|
|||
|
|
[device unlockForConfiguration];
|
|||
|
|
} else {
|
|||
|
|
NSLog(@"[CameraFocusHelper] 配置失败: %@", error.localizedDescription);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// 触发单次自动对焦(点击对焦用)
|
|||
|
|
void _TriggerAutoFocus(float normalizedX, float normalizedY)
|
|||
|
|
{
|
|||
|
|
AVCaptureDevice *device = GetActiveRearCamera();
|
|||
|
|
if (!device) return;
|
|||
|
|
|
|||
|
|
NSError *error = nil;
|
|||
|
|
if ([device lockForConfiguration:&error]) {
|
|||
|
|
if ([device isFocusPointOfInterestSupported]) {
|
|||
|
|
device.focusPointOfInterest = CGPointMake(normalizedX, normalizedY);
|
|||
|
|
device.focusMode = AVCaptureFocusModeAutoFocus;
|
|||
|
|
}
|
|||
|
|
if ([device isExposurePointOfInterestSupported]) {
|
|||
|
|
device.exposurePointOfInterest = CGPointMake(normalizedX, normalizedY);
|
|||
|
|
device.exposureMode = AVCaptureExposureModeAutoExpose;
|
|||
|
|
}
|
|||
|
|
[device unlockForConfiguration];
|
|||
|
|
|
|||
|
|
// 1.5秒后恢复连续对焦
|
|||
|
|
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)),
|
|||
|
|
dispatch_get_main_queue(), ^{
|
|||
|
|
_EnableContinuousAutoFocus();
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|