You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

iPhone 13系列设备ZBarSDK扫码聚焦异常技术求助

问题描述

我们在部分iPhone设备(尤其是iPhone 13、iPhone 13 Pro等新型号)上使用ZBarSDK组件时遇到条码扫描问题。结合客户反馈的应用无法对焦条码的情况,推测问题源于设备后置多摄像头选择了不合适的相机。该问题仅在部分客户设备上出现,我方无法复现。

扫描功能的简化实现代码如下:

func showCaptureBarcode(completion: @escaping(String?) -> Void) {
    let zbar = ZBarReaderViewController.init()
    let del = ZbarDelegate.init { [weak zbar] str in
      zbar?.dismiss(animated: true) {
      }
      self.zbarDelegate = nil
      completion(str)
    }
    zbar.cameraDevice = .rear
    zbar.readerDelegate = del
    self.zbarDelegate = del
    self.topView().present(zbar, animated: true) {
      if zbar.readerView.device.isFocusModeSupported(.continuousAutoFocus) {
        zbar.readerView.device.focusMode = .continuousAutoFocus
      } else if zbar.readerView.device.isFocusModeSupported(.autoFocus) {
        zbar.readerView.device.focusMode = .autoFocus
      }
      if zbar.readerView.device.isExposureModeSupported(.continuousAutoExposure) {
        zbar.readerView.device.exposureMode = .continuousAutoExposure
      } else if zbar.readerView.device.isExposureModeSupported(.autoExpose) {
        zbar.readerView.device.exposureMode = .autoExpose
      }
    }
  }

现咨询:

  • 是否有简便方法使ZBarReaderViewController实现正确对焦?
  • 是否需在代码中指定合适相机?
  • 是否需要添加手动对焦/缩放控件?

注:扫描的条码通常尺寸不大且距离较近,涉及应用为Nest Forms - Survey builder。


解决方案建议

1. 指定广角后置摄像头

iPhone 13系列的多摄像头系统中,长焦镜头不适合近距离扫码场景。ZBar默认的.rear可能会自动切换到长焦,导致对焦失败。可以强制指定使用广角镜头:

// 替换原zbar.cameraDevice = .rear的代码
if let wideAngleDevice = AVCaptureDevice.default(.builtInWideAngleCamera, for: .video, position: .back) {
    zbar.cameraDevice = wideAngleDevice
} else {
    zbar.cameraDevice = .rear
}

广角镜头的近距离对焦能力更强,能解决大部分场景下的对焦问题。

2. 增加点击屏幕手动对焦逻辑

对于近距离小条码,自动对焦可能无法精准锁定目标,添加点击对焦功能让用户主动触发对焦:
首先开启ZBar的点击对焦支持:

zbar.allowsTapToFocus = true

然后在自定义的ZbarDelegate中实现点击处理:

func readerViewController(_ readerViewController: ZBarReaderViewController, didTapScreenAt point: CGPoint) {
    guard let device = readerViewController.readerView.device else { return }
    // 将屏幕坐标转换为相机的兴趣点坐标(0-1范围)
    let focusPoint = CGPoint(
        x: point.x / readerViewController.view.bounds.width,
        y: point.y / readerViewController.view.bounds.height
    )
    
    do {
        try device.lockForConfiguration()
        // 设置对焦点和对焦模式
        if device.isFocusPointOfInterestSupported, device.isFocusModeSupported(.autoFocus) {
            device.focusPointOfInterest = focusPoint
            device.focusMode = .autoFocus
        }
        // 同步设置曝光点
        if device.isExposurePointOfInterestSupported, device.isExposureModeSupported(.autoExpose) {
            device.exposurePointOfInterest = focusPoint
            device.exposureMode = .autoExpose
        }
        device.unlockForConfiguration()
    } catch {
        print("对焦配置失败: \(error)")
    }
}

3. 调整对焦模式优先级

连续自动对焦(.continuousAutoFocus)在近距离场景下可能频繁调整,反而导致模糊。可以优先使用单次自动对焦:

// 修改present完成后的对焦设置代码
if zbar.readerView.device.isFocusModeSupported(.autoFocus) {
    zbar.readerView.device.focusMode = .autoFocus
} else if zbar.readerView.device.isFocusModeSupported(.continuousAutoFocus) {
    zbar.readerView.device.focusMode = .continuousAutoFocus
}

4. 手动缩放控件(可选)

如果条码过小,手动缩放可以辅助相机捕捉清晰图像。可以添加滑块控件,通过调整videoZoomFactor实现:

// 初始化时设置最大缩放倍数
if zbar.readerView.device.isZoomSupported {
    zbar.readerView.device.videoZoomFactor = 1.0
    // 后续通过滑块值设置videoZoomFactor,范围建议1.0-2.0
}

总结

优先尝试指定广角摄像头和点击对焦的方案,这两个改动成本低,针对性强,能解决大部分iPhone 13系列的对焦问题。如果问题仍存在,再考虑调整对焦模式或添加手动缩放控件。

内容的提问来源于stack exchange,提问作者LetynSOFT

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 17:55:03