3

I am developing an app that detects text real-time in portrait mode. However, on switching the camera to landscape mode it does not adapts to the changed orientation.

Screenshot in portrait mode:

enter image description here

Screenshot in landscape mode:

enter image description here

Camera Setup code:

fileprivate func initSetupLiveVideo() {
    session.sessionPreset = .high
    let captureDevice = AVCaptureDevice.default(for: AVMediaType.video)

    let deviceInput = try! AVCaptureDeviceInput(device: captureDevice!)
    let deviceOutput = AVCaptureVideoDataOutput()
    deviceOutput.videoSettings = [kCVPixelBufferPixelFormatTypeKey as String: Int(kCVPixelFormatType_32BGRA)]
    deviceOutput.setSampleBufferDelegate(self, queue: DispatchQueue.global(qos: DispatchQoS.QoSClass.default))
    session.addInput(deviceInput)
    session.addOutput(deviceOutput)

    imageView.videoPreviewLayer.session = session
}

Code to highlight word:

func highlightWord(box: VNTextObservation) {
    guard let boxes = box.characterBoxes else {
        return
    }

    var minX: CGFloat = 0.0
    var minY: CGFloat = 0.0
    var maxY: CGFloat = CGFloat.greatestFiniteMagnitude
    var maxX: CGFloat = CGFloat.greatestFiniteMagnitude

    for char in boxes {
        if char.bottomLeft.x < maxX {
            maxX = char.bottomLeft.x
        }
        if char.bottomRight.x > minX {
            minX = char.bottomRight.x
        }
        if char.bottomRight.y < maxY {
            maxY = char.bottomRight.y
        }
        if char.topRight.y > minY {
            minY = char.topRight.y
        }
    }

    let xCord = maxX * imageView.frame.size.width
    let yCord = (1 - minY) * imageView.frame.size.height
    let width = (minX - maxX) * imageView.frame.size.width
    let height = (minY - maxY) * imageView.frame.size.height

    let outline = CALayer()
    outline.frame = CGRect(x: xCord, y: yCord, width: width, height: height)
    outline.borderWidth = 2.0
    outline.borderColor = UIColor.red.cgColor

    imageView.layer.addSublayer(outline)
}

Expected that on rotating the device, the view should adapt.

Nazim Kerimbekov
  • 4,712
  • 8
  • 34
  • 58
low_queue
  • 65
  • 3
  • is https://stackoverflow.com/a/39012767/4757272 what you are looking for? – Vollan Jul 18 '19 at 19:34
  • I think the problem is this line: `imageView.videoPreviewLayer.session = session` You do not set an existing video preview layer's session to your session. You _make_ a video preview layer and put it in the interface. – matt Jul 18 '19 at 19:42
  • @matt I am not sure since I have just started iOS development as a hobby. Is there any place you can direct where I can see the modification you are suggesting. – low_queue Jul 18 '19 at 19:57

0 Answers0