@@ -69,11 +69,11 @@ fun detectDocumentQuad(mask: Bitmap): Quad? {
6969fun extractDocument (originalBitmap : Bitmap , quad : Quad , rotationDegrees : Int ): Bitmap {
7070 val widthTop = norm(quad.topLeft, quad.topRight)
7171 val widthBottom = norm(quad.bottomLeft, quad.bottomRight)
72- val maxWidth = max (widthTop, widthBottom).toInt()
72+ val targetWidth = (widthTop + widthBottom) / 2
7373
7474 val heightLeft = norm(quad.topLeft, quad.bottomLeft)
7575 val heightRight = norm(quad.topRight, quad.bottomRight)
76- val maxHeight = max (heightLeft, heightRight).toInt()
76+ val targetHeight = (heightLeft + heightRight) / 2
7777
7878 val srcPoints = MatOfPoint2f (
7979 quad.topLeft.toCv(),
@@ -83,16 +83,16 @@ fun extractDocument(originalBitmap: Bitmap, quad: Quad, rotationDegrees: Int): B
8383 )
8484 val dstPoints = MatOfPoint2f (
8585 org.opencv.core.Point (0.0 , 0.0 ),
86- org.opencv.core.Point (maxWidth .toDouble(), 0.0 ),
87- org.opencv.core.Point (maxWidth .toDouble(), maxHeight .toDouble()),
88- org.opencv.core.Point (0.0 , maxHeight .toDouble())
86+ org.opencv.core.Point (targetWidth .toDouble(), 0.0 ),
87+ org.opencv.core.Point (targetWidth .toDouble(), targetHeight .toDouble()),
88+ org.opencv.core.Point (0.0 , targetHeight .toDouble())
8989 )
9090 val transform = Imgproc .getPerspectiveTransform(srcPoints, dstPoints)
9191
9292 val inputMat = Mat ()
9393 Utils .bitmapToMat(originalBitmap, inputMat)
9494 val outputMat = Mat ()
95- val outputSize = Size (maxWidth .toDouble(), maxHeight .toDouble())
95+ val outputSize = Size (targetWidth .toDouble(), targetHeight .toDouble())
9696 Imgproc .warpPerspective(inputMat, outputMat, transform, outputSize)
9797
9898 val enhanced = enhanceCapturedImage(outputMat)
0 commit comments