Skip to content

Commit

Permalink
fix(capture-sdk): Remove unneeded empty lines when cropping recognize…
Browse files Browse the repository at this point in the history
…d text

PIA-4617
  • Loading branch information
a-szotyori committed Oct 31, 2023
1 parent 18b5985 commit de12a85
Showing 1 changed file with 15 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ internal class CropToCameraFrameTextRecognizer(private val actualTextRecognizer:
private fun getCroppedRecognizedText(recognizedText: RecognizedText): RecognizedText {
return cameraFrameRect?.let { frameRect ->
val croppedBlocks = getCroppedBlocks(recognizedText, frameRect)
val croppedTextString = getCroppedTextString(recognizedText, frameRect)
val croppedTextString = reconstructTextFromBlocks(croppedBlocks)
RecognizedText(croppedTextString, croppedBlocks)
} ?: recognizedText
}
Expand All @@ -113,22 +113,23 @@ internal class CropToCameraFrameTextRecognizer(private val actualTextRecognizer:
* @param recognizedText the recognized text
* @param cameraFrameRect the camera frame
*/
private fun getCroppedTextString(
recognizedText: RecognizedText,
cameraFrameRect: Rect
): String = recognizedText.blocks.fold(StringBuilder()) { acc, textBlock ->
textBlock.lines.fold(acc) { acc, line ->
line.elements.fold(acc) { acc, element ->
if (element.boundingBox != null) {
if (cameraFrameRect.contains(getScaledBoundingBox(element.boundingBox))) {
acc.append(element.text)
}
private fun reconstructTextFromBlocks(
croppedBlocks: List<RecognizedTextBlock>
): String = croppedBlocks.foldIndexed(StringBuilder()) { index, acc, textBlock ->
if (index != 0) {
acc.append("\n")
}
textBlock.lines.foldIndexed(acc) { index, acc, line ->
if (index != 0) {
acc.append("\n")
}
line.elements.foldIndexed(acc) { index, acc, element ->
if (index != 0) {
acc.append(" ")
}
acc
acc.append(element.text)
}
acc.append("\n")
}
acc.append("\n")
}.toString()

/**
Expand Down

0 comments on commit de12a85

Please sign in to comment.