Skip to content

Commit

Permalink
Merge pull request #305 from firemaples/fix/fix-screenshot-issues
Browse files Browse the repository at this point in the history
Fix screenshot issues
  • Loading branch information
firemaples authored Sep 9, 2023
2 parents 753c8b9 + 1240f85 commit d35a493
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ class ScreenCirclingView(context: Context) : FloatingView(context) {

viewModel.lastSelectedArea.observe(lifecycleOwner) { selected ->
selected ?: return@observe
circlingView.selectedBox = selected
onAreaSelected?.invoke(circlingView.getViewRect(), selected)
if (circlingView.getViewRect().contains(selected)) {
circlingView.selectedBox = selected
onAreaSelected?.invoke(circlingView.getViewRect(), selected)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ object ScreenExtractor {
// width = screenWidth + rowPadding / pixelStride,
height = screenHeight,
config = Bitmap.Config.ARGB_8888,
fixedSize = true,
).apply {
// rewind() before using the buffer to fix:
// RuntimeException: Buffer not large enough for pixels
Expand Down
39 changes: 29 additions & 10 deletions main/src/main/java/tw/firemaples/onscreenocr/utils/BitmapCache.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,26 @@ object BitmapCache {
fun getBitmapFromReusableSet(options: BitmapFactory.Options): Bitmap? {
val width = options.outWidth / options.inSampleSize
val height = options.outHeight / options.inSampleSize
return getBitmapFromReusableSet(width = width, height = height, config = null)
return getBitmapFromReusableSet(
width = width,
height = height,
config = null,
fixedSize = false,
)
}

fun getReusableBitmapOrCreate(width: Int, height: Int, config: Bitmap.Config): Bitmap =
getBitmapFromReusableSet(width = width, height = height, config = config)
?: Bitmap.createBitmap(width, height, config)
fun getReusableBitmapOrCreate(
width: Int, height: Int, config: Config, fixedSize: Boolean,
): Bitmap = getBitmapFromReusableSet(
width = width,
height = height,
config = config,
fixedSize = fixedSize,
) ?: Bitmap.createBitmap(width, height, config)

fun getBitmapFromReusableSet(width: Int, height: Int, config: Bitmap.Config?): Bitmap? {
fun getBitmapFromReusableSet(
width: Int, height: Int, config: Config?, fixedSize: Boolean,
): Bitmap? {
reusableBitmaps
.takeIf { it.isNotEmpty() }
?.also { reusableBitmaps ->
Expand All @@ -46,6 +58,7 @@ object BitmapCache {
width = width,
height = height,
config = config,
fixedSize = fixedSize,
)
) {
iterator.remove()
Expand All @@ -63,17 +76,23 @@ object BitmapCache {
return null
}

private fun Bitmap.canUseForInBitmap(width: Int, height: Int, config: Config?): Boolean {
private fun Bitmap.canUseForInBitmap(
width: Int, height: Int, config: Config?, fixedSize: Boolean,
): Boolean {
if (fixedSize && this.width != width && this.height != height) {
return false
}

val targetConfig = config ?: this.config
val byteCount = width * height * targetConfig.getBytesPerPixel()
return byteCount <= this.allocationByteCount
}

private fun Bitmap.Config.getBytesPerPixel(): Int =
private fun Config.getBytesPerPixel(): Int =
when (this) {
Bitmap.Config.ARGB_8888 -> 4
Bitmap.Config.RGB_565, Bitmap.Config.ARGB_4444 -> 2
Bitmap.Config.ALPHA_8 -> 1
Config.ARGB_8888 -> 4
Config.RGB_565, Config.ARGB_4444 -> 2
Config.ALPHA_8 -> 1
else -> 1
}
}

0 comments on commit d35a493

Please sign in to comment.