Resize the captured image before displaying it
This commit is contained in:
@@ -96,8 +96,25 @@ fun extractDocument(originalBitmap: Bitmap, quad: Quad, rotationDegrees: Int): B
|
||||
Imgproc.warpPerspective(inputMat, outputMat, transform, outputSize)
|
||||
|
||||
val enhanced = enhanceCapturedImage(outputMat)
|
||||
val rotated = rotate(enhanced, rotationDegrees)
|
||||
val resized = resize(rotated, 1500.0)
|
||||
|
||||
return toBitmap(rotate(enhanced, rotationDegrees))
|
||||
return toBitmap(resized)
|
||||
}
|
||||
|
||||
fun resize(original: Mat, targetMax: Double): Mat {
|
||||
val origSize = original.size()
|
||||
if (max(origSize.width, origSize.height) < targetMax)
|
||||
return original;
|
||||
var targetWidth = targetMax
|
||||
var targetHeight = origSize.height * targetWidth / origSize.width
|
||||
if (origSize.width < origSize.height) {
|
||||
targetHeight = targetMax
|
||||
targetWidth = origSize.width * targetHeight / origSize.height
|
||||
}
|
||||
val result = Mat()
|
||||
Imgproc.resize(original, result, Size(targetWidth, targetHeight), 0.0, 0.0, Imgproc.INTER_AREA)
|
||||
return result
|
||||
}
|
||||
|
||||
fun rotate(input: Mat, degrees: Int): Mat {
|
||||
|
||||
@@ -150,9 +150,8 @@ class MainViewModel(
|
||||
if (bitmap == null) {
|
||||
return
|
||||
}
|
||||
val resized = resizeImage(bitmap)
|
||||
val outputStream = ByteArrayOutputStream()
|
||||
resized.compress(Bitmap.CompressFormat.JPEG, quality, outputStream)
|
||||
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream)
|
||||
val jpegBytes = outputStream.toByteArray()
|
||||
imageRepository.add(jpegBytes)
|
||||
_pageIds.value = imageRepository.imageIds()
|
||||
|
||||
@@ -38,16 +38,3 @@ fun writePdfFromJpegs(jpegs: Sequence<ByteArray>, outputStream: OutputStream) {
|
||||
document.save(outputStream)
|
||||
}
|
||||
}
|
||||
|
||||
fun resizeImage(original: Bitmap): Bitmap {
|
||||
val targetMax = 1500
|
||||
if (max(original.width, original.height) < targetMax)
|
||||
return original;
|
||||
var targetWidth = targetMax
|
||||
var targetHeight = original.height * targetWidth / original.width
|
||||
if (original.width < original.height) {
|
||||
targetHeight = targetMax
|
||||
targetWidth = original.width * targetHeight / original.height
|
||||
}
|
||||
return original.scale(targetWidth, targetHeight)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user