問題是我使用預覽框架來翻譯屏幕上的點。但是通過邊界框出現的點與預覽視圖無關,它們與VideoFrame相關(正如catchoom.com的支持人員指出的那樣)。 VideoFrame的大小由capturePreset設置,它只接受兩個值AVCaptureSessionPreset1280x720
和AVCaptureSessionPreset640x480
。默認的是AVCaptureSessionPreset1280x720
所以在我的情況下,我不得不使用1280x720大小的計算,然後從這些座標轉換到我的預覽視圖大小的座標。
所以它結束了看起來像這樣:
let box = bestResult.matchBoundingBox
let wVideoFrame:CGFloat = 1080.0;
let hVideoFrame:CGFloat = 720.0;
let wRelativePreview = wVideoFrame/CGFloat(preview.frame.size.height)
let hRelativePreview = wVideoFrame/CGFloat(preview.frame.size.width)
var tl = CGPoint(x: wVideoFrame * CGFloat(box.topLeftX),y: hVideoFrame * CGFloat(box.topLeftY));
var tr = CGPoint(x: wVideoFrame * CGFloat(box.topRightX) ,y: hVideoFrame * CGFloat(box.topRightY));
var br = CGPoint(x: wVideoFrame * CGFloat(box.bottomRightX),y: hVideoFrame * CGFloat(box.bottomRightY));
var bl = CGPoint(x: wVideoFrame * CGFloat(box.bottomLeftX),y: hVideoFrame * CGFloat(box.bottomLeftY));
tl = CGPoint(x: tl.x/wRelativePreview, y: tl.y/hRelativePreview)
tr = CGPoint(x: tr.x/wRelativePreview, y: tr.y/hRelativePreview)
br = CGPoint(x: br.x/wRelativePreview, y: br.y/hRelativePreview)
bl = CGPoint(x: bl.x/wRelativePreview, y: bl.y/hRelativePreview)
// 4 square visualize top-left, top.right, bottom-left and bottom-right points
var fr = vTL.frame;
fr.origin = tl;
vTL.frame = fr;
fr.origin = tr;
vTR.frame = fr;
fr.origin = br;
vBR.frame = fr;
fr.origin = bl;
vBL.frame = fr;
現在點看着屏幕上相當確定的,但他們看上去有些怎樣旋轉的。所以我旋轉角度90度:
// overlay is the container of the 3 squares to visualize the points in screen
overlay.transform = CGAffineTransform(rotationAngle: CGFloat(M_PI/2.0))
注意,這不是從catchoom支持官方迴應,這可能不是100%正確的,但它的工作對我來說相當不錯。