WeTransfer / WeScan

Document Scanning Made Easy for iOS
MIT License
2.83k stars 550 forks source link

Quad drawing off #383

Closed nichita-stefanita closed 2 months ago

nichita-stefanita commented 3 months ago

Disclaimer: it's most likely not an issue with the library but I hope someone could give me an idea of what the issue is, with a bit of context.

Note: My project written on SwiftUI, but camera module is using UIKit and integrated via UIViewControllerRepresentable.

I didn't add weScan as a dependency, but only added the Rectangle detection and quad drawing features. And somehow my quad coordinates are way off and do not represent what the user sees from the videoPreviewLayer.

I've only managed to "correct" that by changing the rotation angle in CGAffineTransform from 90 to 180 degrees and swapping imageSize.width and imageSize.height:

let portraitImageSize = CGSize(width: imageSize.width, height: imageSize.height)

let scaleTransform = CGAffineTransform.scaleTransform(forSize: portraitImageSize, aspectFillInSize: quadView.bounds.size)

let scaledImageSize = imageSize.applying(scaleTransform)

let rotationTransform = CGAffineTransform(rotationAngle: CGFloat.pi * 2)

let imageBounds = CGRect(origin: .zero, size: scaledImageSize).applying(rotationTransform)
let translationTransform = CGAffineTransform.translateTransform(fromCenterOfRect: imageBounds, toCenterOfRect: quadView.bounds)

let transforms = [scaleTransform, rotationTransform, translationTransform]

let transformedQuad = quad.applyTransforms(transforms)

quadView.drawQuadrilateral(quad: transformedQuad, animated: true)

It now displays the quad as expected, but I still don't understand what causes that deviation and why the original calculus doesn't apply in my case.

github-actions[bot] commented 2 months ago

This issue is stale because it has been open for 30 days with no activity. Remove the Stale label or comment or this will be closed in 10 days.