1. 新增iOS原生插件CameraFocusHelper,实现连续自动对焦和曝光控制 2. 适配iOS平台的扫码对焦逻辑,替换原生Unity的自动对焦实现 3. 新增跨平台自动对焦维护协程,区分iOS和其他平台的对焦处理逻辑 4. 优化扫码对焦体验,限制对焦范围为近距并开启低光增强
70 lines
2.7 KiB
Plaintext
70 lines
2.7 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;
|
|
}
|
|
}
|
|
|
|
// 启用低光增强(昏暗环境提高识别率)
|
|
if ([device respondsToSelector:@selector(setAutomaticallyEnablesLowLightBoostWhenAvailable:)]) {
|
|
device.automaticallyEnablesLowLightBoostWhenAvailable = YES;
|
|
}
|
|
|
|
[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];
|
|
}
|
|
});
|
|
}
|
|
|
|
}
|