@ -160,15 +160,15 @@ struct ScannerView: View {
. onReceive ( scannerViewModel . $ detectedCodes ) { codes in
if ! codes . isEmpty {
print ( " 检测到条码数量: \( codes . count ) " )
logInfo ( " 检测到条码数量: \( codes . count ) " , className : " ScannerView " )
if codes . count = = 1 {
// 只 有 一 个 码 , 显 示 标 记 后 一 秒 自 动 显 示 结 果
print ( " 单个条码,准备自动选择 " )
logInfo ( " 单个条码,准备自动选择 " , className : " ScannerView " )
pauseForPreview ( )
autoSelectSingleCode ( code : codes [ 0 ] )
} else {
// 多 个 码 , 暂 停 预 览 并 引 导 用 户 选 择
print ( " 多个条码,等待用户选择 " )
logInfo ( " 多个条码,等待用户选择 " , className : " ScannerView " )
pauseForPreview ( )
}
}
@ -176,7 +176,7 @@ struct ScannerView: View {
. onReceive ( NotificationCenter . default . publisher ( for : UIDevice . orientationDidChangeNotification ) ) { _ in
// 屏 幕 方 向 变 化 时 更 新 状 态
screenOrientation = UIDevice . current . orientation
print ( " Screen orientation changed to: \( screenOrientation . rawValue ) " )
logInfo ( " Screen orientation changed to: \( screenOrientation ) " , className : " ScannerView " )
}
}
@ -192,22 +192,22 @@ struct ScannerView: View {
}
private func autoSelectSingleCode ( code : DetectedCode ) {
print ( " 开始自动选择定时器,条码类型: \( code . type ) " )
logInfo ( " 开始自动选择定时器,条码类型: \( code . type ) " , className : " ScannerView " )
// 一 秒 后 自 动 选 择 单 个 条 码
DispatchQueue . main . asyncAfter ( deadline : . now ( ) + 1.0 ) {
print ( " 自动选择定时器触发 " )
print ( " 当前状态 - showPreviewPause: \( self . showPreviewPause ) " )
print ( " 当前条码数量: \( self . scannerViewModel . detectedCodes . count ) " )
logInfo ( " 自动选择定时器触发 " , className : " ScannerView " )
logInfo ( " 当前状态 - showPreviewPause: \( self . showPreviewPause ) " , className : " ScannerView " )
logInfo ( " 当前条码数量: \( self . scannerViewModel . detectedCodes . count ) " , className : " ScannerView " )
if self . showPreviewPause && self . scannerViewModel . detectedCodes . count = = 1 {
print ( " 条件满足,执行自动选择 " )
logInfo ( " 条件满足,执行自动选择 " , className : " ScannerView " )
// 确 保 仍 然 只 有 一 个 条 码 且 处 于 预 览 暂 停 状 态
let selectedCode = " 类型: \( code . type ) \n 内容: \( code . content ) "
print ( " 发送通知: \( selectedCode ) " )
logInfo ( " 发送通知: \( selectedCode ) " , className : " ScannerView " )
NotificationCenter . default . post ( name : . scannerDidScanCode , object : selectedCode )
self . dismiss ( )
} else {
print ( " 条件不满足,取消自动选择 " )
logInfo ( " 条件不满足,取消自动选择 " , className : " ScannerView " )
}
}
}
@ -321,7 +321,7 @@ class ScannerViewModel: NSObject, ObservableObject, AVCaptureMetadataOutputObjec
didOutput metadataObjects : [ AVMetadataObject ] ,
from connection : AVCaptureConnection ) {
print ( " metadataOutput 被调用,检测到 \( metadataObjects . count ) 个对象 " )
logInfo ( " metadataOutput 被调用,检测到 \( metadataObjects . count ) 个对象 " , className : " ScannerViewModel " )
// 震 动 反 馈
AudioServicesPlaySystemSound ( SystemSoundID ( kSystemSoundID_Vibrate ) )
@ -346,15 +346,15 @@ class ScannerViewModel: NSObject, ObservableObject, AVCaptureMetadataOutputObjec
)
codes . append ( detectedCode )
print ( " 创建 DetectedCode: 类型= \( codeType ) , 内容= \( stringValue ) " )
logInfo ( " 创建 DetectedCode: 类型= \( codeType ) , 内容= \( stringValue ) " , className : " ScannerViewModel " )
}
}
print ( " 准备更新 detectedCodes, 数量: \( codes . count ) " )
logInfo ( " 准备更新 detectedCodes, 数量: \( codes . count ) " , className : " ScannerViewModel " )
// 更 新 检 测 到 的 条 码 列 表
DispatchQueue . main . async {
print ( " 在主线程更新 detectedCodes " )
logInfo ( " 在主线程更新 detectedCodes " , className : " ScannerViewModel " )
self . detectedCodes = codes
}
}
@ -424,9 +424,9 @@ struct CodePositionMarker: View {
onCodeSelected ( selectedCode )
}
. onAppear {
print ( " CodePositionMarker appeared at: x= \( position . x ) , y= \( position . y ) " )
print ( " Screen size: \( geometry . size ) " )
print ( " Code bounds: \( code . bounds ) " )
logDebug ( " CodePositionMarker appeared at: x= \( position . x ) , y= \( position . y ) " , className : " CodePositionMarker " )
logDebug ( " Screen size: \( geometry . size ) " , className : " CodePositionMarker " )
logDebug ( " Code bounds: \( code . bounds ) " , className : " CodePositionMarker " )
}
}
}
@ -434,13 +434,13 @@ struct CodePositionMarker: View {
private func calculatePosition ( screenSize : CGSize ) -> CGPoint {
guard let previewLayer = previewLayer else {
// 如 果 没 有 预 览 层 , 使 用 屏 幕 中 心
print ( " No preview layer available, using screen cent er" )
logWarning ( " No preview layer available, using screen cent er" , className : " CodePositionMark er" )
return CGPoint ( x : screenSize . width / 2 , y : screenSize . height / 2 )
}
// 检 查 预 览 层 是 否 有 效
guard previewLayer . session ? . isRunning = = true else {
print ( " Preview layer session not running, using screen cent er" )
logWarning ( " Preview layer session not running, using screen cent er" , className : " CodePositionMark er" )
return CGPoint ( x : screenSize . width / 2 , y : screenSize . height / 2 )
}
@ -453,7 +453,7 @@ struct CodePositionMarker: View {
// 验 证 转 换 结 果 是 否 有 效
guard convertedPoint . x . isFinite && convertedPoint . y . isFinite else {
print ( " Invalid converted point: \( convertedPoint ) , using screen cent er" )
logWarning ( " Invalid converted point: \( convertedPoint ) , using screen cent er" , className : " CodePositionMark er" )
return CGPoint ( x : screenSize . width / 2 , y : screenSize . height / 2 )
}
@ -461,10 +461,10 @@ struct CodePositionMarker: View {
let clampedX = max ( 20 , min ( screenSize . width - 20 , convertedPoint . x ) )
let clampedY = max ( 20 , min ( screenSize . height - 20 , convertedPoint . y ) )
print ( " AVFoundation bounds: \( code . bounds ) " )
print ( " Converted point: \( convertedPoint ) " )
print ( " Screen size: \( screenSize ) " )
print ( " Clamped: x= \( clampedX ) , y= \( clampedY ) " )
logDebug ( " AVFoundation bounds: \( code . bounds ) " , className : " CodePositionMarker " )
logDebug ( " Converted point: \( convertedPoint ) " , className : " CodePositionMarker " )
logDebug ( " Screen size: \( screenSize ) " , className : " CodePositionMarker " )
logDebug ( " Clamped: x= \( clampedX ) , y= \( clampedY ) " , className : " CodePositionMarker " )
return CGPoint ( x : clampedX , y : clampedY )
}