killapp/Assets/Plugins/iOS/CameraFocusHelper.mm
“虞渠成” 432b80dfa3 fix: 修复多处UI返回逻辑、优化扫码对焦与UI布局
1.  优化HomePage加载逻辑,修复加载弹窗重复显示问题
2.  重构蓝牙分享页、重命名页的返回事件绑定逻辑
3.  优化iOS/Android扫码对焦功能,支持多后置摄像头、软触发对焦
4.  调整扫码预览布局与参数,提升扫码识别率与适配性
5.  删除冗余的Android状态栏管理脚本与无用文件
6.  新增返回回调调试日志
2026-09-01 15:47:57 +08:00

84 lines
3.5 KiB
Plaintext
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#import <AVFoundation/AVFoundation.h>
#import <UIKit/UIKit.h>
/// 获取当前正在使用的后置摄像头设备
static AVCaptureDevice *GetActiveRearCamera(void)
{
// 遍历所有 AVCaptureDevice找到后置摄像头
// 注意Unity WebCamTexture 内部的 AVCaptureSession 不会暴露给原生代码,
// 但 AVCaptureDevice 本身可以独立 lockForConfiguration 设置对焦模式,
// 不需要成为当前 session 的输入设备。
if (@available(iOS 10.0, *)) {
AVCaptureDeviceDiscoverySession *session = [AVCaptureDeviceDiscoverySession
discoverySessionWithDeviceTypes:@[AVCaptureDeviceTypeBuiltInWideAngleCamera,
AVCaptureDeviceTypeBuiltInTelephotoCamera,
AVCaptureDeviceTypeBuiltInDualCamera,
AVCaptureDeviceTypeBuiltInDualWideCamera,
AVCaptureDeviceTypeBuiltInTripleCamera]
mediaType:AVMediaTypeVideo
position:AVCaptureDevicePositionBack];
for (AVCaptureDevice *d in session.devices) {
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();
});
}
}
/// 周期性重新触发对焦(与 Android softTrigger 思路一致)
/// 通过"点对焦 → 1.5s 后恢复连续对焦"的方式让镜头做一次完整对焦循环,无黑屏
void _TriggerContinuousRefocus(void)
{
_TriggerAutoFocus(0.5f, 0.5f);
}
}