1. 新增iOS平台原生相机对焦辅助插件,支持开启连续自动对焦和点击单点对焦 2. 适配扫码页面,iOS平台使用原生插件实现对焦功能,修复Unity自带autoFocusPoint无效问题 3. 添加点击屏幕触发单点对焦,1.5秒后自动恢复连续对焦 4. 新增定时维护对焦状态的协程,防止系统重置对焦模式
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();
|
||
});
|
||
}
|
||
}
|
||
|
||
}
|