1

enter image description hereI am trying to make boarders about the frames of lines[Got from the google cloud vision text-recognition] on image in iOS Swift.

I've got lines from block and got the frames of line by using line.frame code provided by google.

I saw some Transformation algo which I applied :

private func createScaledFrame(
    featureFrame: CGRect,
    imageSize: CGSize, viewFrame: CGRect)
    -> CGRect {
    let viewSize = viewFrame.size
      
// 2
let resolutionView = viewSize.width / viewSize.height
let resolutionImage = imageSize.width / imageSize.height
  
// 3
var scale: CGFloat
if resolutionView > resolutionImage {
  scale = viewSize.height / imageSize.height
} else {
  scale = viewSize.width / imageSize.width
}
  
// 4
let featureWidthScaled = featureFrame.size.width * scale
let featureHeightScaled = featureFrame.size.height * scale
  
// 5
let imageWidthScaled = imageSize.width * scale
let imageHeightScaled = imageSize.height * scale
let imagePointXScaled = (viewSize.width - imageWidthScaled) / 2
let imagePointYScaled = (viewSize.height - imageHeightScaled) / 2
  
// 6
let featurePointXScaled = imagePointXScaled + featureFrame.origin.x * scale
let featurePointYScaled = imagePointYScaled + featureFrame.origin.y * scale
  
// 7
return CGRect(x: featurePointXScaled,
              y: featurePointYScaled,
              width: featureWidthScaled,
              height: featureHeightScaled)
}

This is causes when lines and blocks increases It becomes small like shown In Image. Problem with scale is about 2.0...2.25 I dont know where I am wrong the issue with the aspect or something i missed here.

The Code where I am applying the above method:

    let r = self.createScaledFrame(featureFrame: line.frame, imageSize: imageSize, viewFrame: self.imageView.frame)                                
    let lyr = self.createShapeLayer(frame: r)
self.imageView.layer.addSublayer(lyr)

Create shape layer method :

private func createShapeLayer(frame: CGRect) -> CAShapeLayer {
     // 1
     let bpath = UIBezierPath(rect: frame)
     let shapeLayer = CAShapeLayer()
     shapeLayer.path = bpath.cgPath
     // 2
     shapeLayer.strokeColor = Constants.lineColor
     shapeLayer.fillColor = Constants.fillColor
     shapeLayer.lineWidth = Constants.lineWidth
     return shapeLayer
   }

Thanks In advance

0 Answers0