Better auto background detection

This commit is contained in:
Jay 2019-03-31 18:37:12 -07:00
parent c63d46a32e
commit e2472fb9b2

View File

@ -58,7 +58,7 @@ object ImageUtil {
return ColorDrawable(Color.WHITE) return ColorDrawable(Color.WHITE)
val top = 5 val top = 5
val bot = image.height - 5 val bot = image.height - 5
val left = (image.width * 0.025).toInt() val left = (image.width * 0.0275).toInt()
val right = image.width - left val right = image.width - left
val midX = image.width / 2 val midX = image.width / 2
val midY = image.height / 2 val midY = image.height / 2
@ -72,24 +72,21 @@ object ImageUtil {
var darkBG = (topLeftIsDark && (botLeftIsDark || botRightIsDark || topRightIsDark || midLeftIsDark || topMidIsDark)) var darkBG = (topLeftIsDark && (botLeftIsDark || botRightIsDark || topRightIsDark || midLeftIsDark || topMidIsDark))
|| (topRightIsDark && (botRightIsDark || botLeftIsDark || midRightIsDark || topMidIsDark)) || (topRightIsDark && (botRightIsDark || botLeftIsDark || midRightIsDark || topMidIsDark))
if (pixelIsClose(image.getPixel(left,top), image.getPixel(midX,top)) && // top left & top mid
pixelIsClose(image.getPixel(midX,top), image.getPixel(right,top)) && //top mid & top right if (!isWhite(image.getPixel(left, top)) && pixelIsClose(image.getPixel(left, top), image.getPixel(midX, top)) &&
pixelIsClose(image.getPixel(right,top), image.getPixel(right,bot)) && // top right & bot right !isWhite(image.getPixel(midX, top)) && pixelIsClose(image.getPixel(midX, top), image.getPixel(right, top)) &&
pixelIsClose(image.getPixel(right,bot), image.getPixel(midX,bot)) && // bot right & bot mid !isWhite(image.getPixel(right, top)) && pixelIsClose(image.getPixel(right, top), image.getPixel(right, bot)) &&
pixelIsClose(image.getPixel(midX,bot), image.getPixel(left,bot))) // bot mid & bot left !isWhite(image.getPixel(right, bot)) && pixelIsClose(image.getPixel(right, bot), image.getPixel(midX, bot)) &&
!isWhite(image.getPixel(midX, bot)) && pixelIsClose(image.getPixel(midX, bot), image.getPixel(left, bot)) &&
!isWhite(image.getPixel(left, bot)) && pixelIsClose(image.getPixel(left, bot), image.getPixel(left, top)))
return ColorDrawable(image.getPixel(left, top)) return ColorDrawable(image.getPixel(left, top))
val blackPixel = when {
topLeftIsDark -> image.getPixel (left, top)
topRightIsDark -> image.getPixel(right, top)
botLeftIsDark ->image.getPixel(left, bot)
else -> image.getPixel(right, bot)
}
if (isWhite(image.getPixel(left, top)).toInt() + if (isWhite(image.getPixel(left, top)).toInt() +
isWhite(image.getPixel(right, top)).toInt() + isWhite(image.getPixel(right, top)).toInt() +
isWhite(image.getPixel(left, bot)).toInt() + isWhite(image.getPixel(left, bot)).toInt() +
isWhite(image.getPixel(right, bot)).toInt() > 2) isWhite(image.getPixel(right, bot)).toInt() > 2)
darkBG = false darkBG = false
var overallWhitePixels = 0 var overallWhitePixels = 0
var overallBlackPixels = 0 var overallBlackPixels = 0
outer@ for (x in intArrayOf(left, right)) { outer@ for (x in intArrayOf(left, right)) {
@ -109,8 +106,7 @@ object ImageUtil {
if (whitePixelsStreak > 14) { if (whitePixelsStreak > 14) {
whiteStrak = true whiteStrak = true
} }
} } else {
else {
whitePixelsStreak = 0 whitePixelsStreak = 0
if (isDark(pixel)) { if (isDark(pixel)) {
blackPixels++ blackPixels++
@ -119,22 +115,30 @@ object ImageUtil {
if (blackPixelsStreak > 14) { if (blackPixelsStreak > 14) {
blackStreak = true blackStreak = true
} }
} } else {
else {
blackPixelsStreak = 0 blackPixelsStreak = 0
} }
} }
} }
when { when {
blackPixels > 22 -> { darkBG = true; overallWhitePixels = 0; break@outer } blackPixels > 22 -> {
darkBG = true; overallWhitePixels = 0; break@outer
}
blackStreak -> darkBG = true blackStreak -> darkBG = true
whiteStrak || whitePixels > 22 -> darkBG = false whiteStrak || whitePixels > 22 -> darkBG = false
} }
} }
if (overallWhitePixels > 9 && overallWhitePixels > overallBlackPixels)
val blackPixel = when {
topLeftIsDark -> image.getPixel(left, top)
topRightIsDark -> image.getPixel(right, top)
botLeftIsDark -> image.getPixel(left, bot)
else -> image.getPixel(right, bot)
}
if (overallWhitePixels > 9 && overallWhitePixels > overallBlackPixels) {
darkBG = false darkBG = false
if (darkBG) }
{ if (darkBG) {
if (isWhite(image.getPixel(left, bot)) && isWhite(image.getPixel(right, bot))) if (isWhite(image.getPixel(left, bot)) && isWhite(image.getPixel(right, bot)))
return GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, return GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM,
intArrayOf(blackPixel, blackPixel, Color.WHITE, Color.WHITE)) intArrayOf(blackPixel, blackPixel, Color.WHITE, Color.WHITE))
@ -144,6 +148,12 @@ object ImageUtil {
else else
return ColorDrawable(blackPixel) return ColorDrawable(blackPixel)
} }
if (topLeftIsDark && topRightIsDark && (topMidIsDark || overallBlackPixels > 9))
return GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM,
intArrayOf(blackPixel, blackPixel, Color.WHITE, Color.WHITE))
else if (botLeftIsDark && botRightIsDark && (isDark(image.getPixel(midX, bot)) || overallBlackPixels > 9))
return GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM,
intArrayOf(Color.WHITE, Color.WHITE, blackPixel, blackPixel))
return ColorDrawable(Color.WHITE) return ColorDrawable(Color.WHITE)
} }