65 lines
2.4 KiB
Plaintext
65 lines
2.4 KiB
Plaintext
#import <AVFoundation/AVFoundation.h>
|
|
#import <UIKit/UIKit.h>
|
|
|
|
extern "C" {
|
|
|
|
/// 启用连续自动对焦和自动曝光
|
|
void _EnableContinuousAutoFocus()
|
|
{
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
|
|
if (!device) return;
|
|
|
|
NSError *error = nil;
|
|
|
|
// 锁定设备开始配置
|
|
if ([device lockForConfiguration:&error]) {
|
|
|
|
// 连续自动对焦模式(持续调整,适合扫描二维码)
|
|
if ([device isFocusModeSupported:AVCaptureFocusModeContinuousAutoFocus]) {
|
|
device.focusMode = AVCaptureFocusModeContinuousAutoFocus;
|
|
}
|
|
|
|
// 连续自动曝光
|
|
if ([device isExposureModeSupported:AVCaptureExposureModeContinuousAutoExposure]) {
|
|
device.exposureMode = AVCaptureExposureModeContinuousAutoExposure;
|
|
}
|
|
|
|
// 对焦范围限制为近距(适合二维码)
|
|
if ([device respondsToSelector:@selector(setAutoFocusRangeRestriction:)]) {
|
|
if ([device isAutoFocusRangeRestrictionSupported]) {
|
|
device.autoFocusRangeRestriction = AVCaptureAutoFocusRangeRestrictionNear;
|
|
}
|
|
}
|
|
|
|
[device unlockForConfiguration];
|
|
|
|
NSLog(@"[CameraFocusHelper] 已启用连续自动对焦");
|
|
} else {
|
|
NSLog(@"[CameraFocusHelper] 锁定设备失败: %@", error.localizedDescription);
|
|
}
|
|
});
|
|
}
|
|
|
|
/// 触发一次自动对焦(用于手动点按时调用)
|
|
void _TriggerAutoFocus()
|
|
{
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
|
|
if (!device) return;
|
|
|
|
NSError *error = nil;
|
|
if ([device lockForConfiguration:&error]) {
|
|
if ([device isFocusModeSupported:AVCaptureFocusModeAutoFocus]) {
|
|
device.focusMode = AVCaptureFocusModeAutoFocus;
|
|
}
|
|
if ([device isExposureModeSupported:AVCaptureExposureModeAutoExpose]) {
|
|
device.exposureMode = AVCaptureExposureModeAutoExpose;
|
|
}
|
|
[device unlockForConfiguration];
|
|
}
|
|
});
|
|
}
|
|
|
|
}
|