ios - iOS 对象检测横向缩放检测
问题描述
我目前正在使用对象检测尝试在实时相机预览中检测某些事物并在它们周围绘制矩形。
这在 Portraitmode 中运行良好,但是当进入横向时,我无法计算出正确缩放矩形的数学。
代码如下所示:
//
func updateGeometry() {
//This is the size of the actual camera frame
let bounds = rootLayer.frame
//To make things easier to read, bufferSize is actually the Size of captureSession?.sessionPreset = .vga640x480
let bufferSize = CGSize(640, 480)
self.previewLayer?.frame.size = bounds.size
let multiplierX = bufferSize.width / bounds.size.width > 1 ? bufferSize.width / bounds.size.width : 1
let multiplierY = bufferSize.height / bounds.size.height > 1 ? bufferSize.height / bounds.size.height : 1
let xScale: CGFloat = bounds.size.width < bounds.size.height ?
(bounds.size.width / bufferSize.width) * multiplierX:
(bounds.size.width / bufferSize.height) * multiplierX
let yScale: CGFloat = bounds.size.width < bounds.size.height ?
(bounds.size.height / bufferSize.height) * multiplierY:
(bounds.size.height / bufferSize.width) * multiplierY
// rotate the layer into screen orientation and scale and mirror
detectionOverlay.setAffineTransform(CGAffineTransform(
rotationAngle: .pi/1.0)
.scaledBy(x: -xScale, y: yScale))
// center the layer
detectionOverlay.position = CGPoint(x: bounds.midX, y: bounds.midY)
}
正如您在横向模式中看到的那样,它有一个奇怪的偏移量,我真的无法弄清楚正确的数学。
解决方案
推荐阅读
- javascript - 如何使用我的 css 示例制作单选按钮?js
- python - 如何在python中使用win32在xlsx文件中间插入行
- python - Pandas Dataframe, easier syntax to drop rows by condition on values
- sharepoint - 让用户在线登录共享点
- ios - 使用 Swift 方法的 Crashlytics 报告很难阅读
- java - Android在“卡拉OK”应用中只录制语音
- python - 我如何创建一个用户名和密码系统,每次使用代码时都能记住不同的用户名?
- git - 合并和删除分支后在特定功能分支上显示提交
- python-3.x - 将文本转换为十进制python3
- javascript - 在 Aframe 中的小地图上反映用户在运行时的位置和旋转