2026-07-23 14:44:41 +08:00
|
|
|
#import <AVFoundation/AVFoundation.h>
|
|
|
|
|
#import <UIKit/UIKit.h>
|
|
|
|
|
|
2026-07-23 14:56:53 +08:00
|
|
|
/// 通过名称查找摄像头设备
|
|
|
|
|
static AVCaptureDevice *FindCameraByName(const char *cameraName)
|
|
|
|
|
{
|
|
|
|
|
if (cameraName == NULL || strlen(cameraName) == 0) {
|
|
|
|
|
return [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NSString *name = [NSString stringWithUTF8String:cameraName];
|
|
|
|
|
|
|
|
|
|
// iOS 10+ DiscoverySession
|
|
|
|
|
if (@available(iOS 10.0, *)) {
|
|
|
|
|
AVCaptureDeviceDiscoverySession *session = [AVCaptureDeviceDiscoverySession
|
|
|
|
|
discoverySessionWithDeviceTypes:@[AVCaptureDeviceTypeBuiltInWideAngleCamera,
|
|
|
|
|
AVCaptureDeviceTypeBuiltInTelephotoCamera,
|
|
|
|
|
AVCaptureDeviceTypeBuiltInUltraWideCamera]
|
|
|
|
|
mediaType:AVMediaTypeVideo
|
|
|
|
|
position:AVCaptureDevicePositionUnspecified];
|
|
|
|
|
for (AVCaptureDevice *device in session.devices) {
|
|
|
|
|
if ([device.localizedName containsString:name] ||
|
|
|
|
|
[device.modelID containsString:name]) {
|
|
|
|
|
return device;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 没找到精确匹配,返回第一个活跃的
|
|
|
|
|
for (AVCaptureDevice *device in session.devices) {
|
|
|
|
|
if (device.isConnected && !device.isSuspended) {
|
|
|
|
|
return device;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-23 14:44:41 +08:00
|
|
|
extern "C" {
|
|
|
|
|
|
2026-07-23 14:56:53 +08:00
|
|
|
/// 启用连续自动对焦和自动曝光(传入 C# 选中的摄像头名称)
|
|
|
|
|
void _EnableContinuousAutoFocus(const char *cameraName)
|
2026-07-23 14:44:41 +08:00
|
|
|
{
|
|
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
2026-07-23 14:56:53 +08:00
|
|
|
AVCaptureDevice *device = FindCameraByName(cameraName);
|
|
|
|
|
if (!device) {
|
|
|
|
|
NSLog(@"[CameraFocusHelper] 未找到摄像头设备");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-07-23 14:44:41 +08:00
|
|
|
|
2026-07-23 14:56:53 +08:00
|
|
|
NSLog(@"[CameraFocusHelper] 配置设备: %@ (name=%@, model=%@)",
|
|
|
|
|
device.localizedName, device.localizedName, device.modelID);
|
2026-07-23 14:44:41 +08:00
|
|
|
|
2026-07-23 14:56:53 +08:00
|
|
|
NSError *error = nil;
|
2026-07-23 14:44:41 +08:00
|
|
|
if ([device lockForConfiguration:&error]) {
|
|
|
|
|
|
2026-07-23 14:56:53 +08:00
|
|
|
// 连续自动对焦
|
2026-07-23 14:44:41 +08:00
|
|
|
if ([device isFocusModeSupported:AVCaptureFocusModeContinuousAutoFocus]) {
|
|
|
|
|
device.focusMode = AVCaptureFocusModeContinuousAutoFocus;
|
2026-07-23 14:56:53 +08:00
|
|
|
NSLog(@"[CameraFocusHelper] 已设为连续自动对焦");
|
|
|
|
|
} else {
|
|
|
|
|
NSLog(@"[CameraFocusHelper] 设备不支持连续自动对焦");
|
2026-07-23 14:44:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 连续自动曝光
|
|
|
|
|
if ([device isExposureModeSupported:AVCaptureExposureModeContinuousAutoExposure]) {
|
|
|
|
|
device.exposureMode = AVCaptureExposureModeContinuousAutoExposure;
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-23 14:56:53 +08:00
|
|
|
// 对焦范围限制为近距
|
|
|
|
|
if ([device respondsToSelector:@selector(isAutoFocusRangeRestrictionSupported)] &&
|
|
|
|
|
[device isAutoFocusRangeRestrictionSupported]) {
|
|
|
|
|
device.autoFocusRangeRestriction = AVCaptureAutoFocusRangeRestrictionNear;
|
|
|
|
|
NSLog(@"[CameraFocusHelper] 已设对焦范围为近距");
|
2026-07-23 14:44:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[device unlockForConfiguration];
|
2026-07-23 14:56:53 +08:00
|
|
|
NSLog(@"[CameraFocusHelper] 摄像头配置完成");
|
2026-07-23 14:44:41 +08:00
|
|
|
} else {
|
2026-07-23 14:56:53 +08:00
|
|
|
NSLog(@"[CameraFocusHelper] lockForConfiguration 失败: %@", error.localizedDescription);
|
2026-07-23 14:44:41 +08:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-23 14:56:53 +08:00
|
|
|
/// 确保连续自动对焦仍在生效(传入 C# 选中的摄像头名称)
|
|
|
|
|
void _EnsureContinuousAutoFocus(const char *cameraName)
|
2026-07-23 14:44:41 +08:00
|
|
|
{
|
|
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
2026-07-23 14:56:53 +08:00
|
|
|
AVCaptureDevice *device = FindCameraByName(cameraName);
|
2026-07-23 14:44:41 +08:00
|
|
|
if (!device) return;
|
|
|
|
|
|
|
|
|
|
NSError *error = nil;
|
|
|
|
|
if ([device lockForConfiguration:&error]) {
|
2026-07-23 14:56:53 +08:00
|
|
|
if (device.focusMode != AVCaptureFocusModeContinuousAutoFocus &&
|
|
|
|
|
[device isFocusModeSupported:AVCaptureFocusModeContinuousAutoFocus]) {
|
|
|
|
|
device.focusMode = AVCaptureFocusModeContinuousAutoFocus;
|
|
|
|
|
NSLog(@"[CameraFocusHelper] 重新设回连续自动对焦");
|
2026-07-23 14:44:41 +08:00
|
|
|
}
|
|
|
|
|
[device unlockForConfiguration];
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|