More work on bg detection

This commit is contained in:
Jay 2019-04-13 12:29:25 -07:00
parent 83b684c37d
commit afde195534
2 changed files with 48 additions and 38 deletions

View File

@ -4,10 +4,8 @@ import android.annotation.SuppressLint
import android.content.Intent import android.content.Intent
import android.graphics.BitmapFactory import android.graphics.BitmapFactory
import android.graphics.PointF import android.graphics.PointF
import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.Drawable import android.graphics.drawable.Drawable
import android.net.Uri import android.net.Uri
import android.support.v4.graphics.ColorUtils
import android.view.GestureDetector import android.view.GestureDetector
import android.view.Gravity import android.view.Gravity
import android.view.MotionEvent import android.view.MotionEvent
@ -249,31 +247,13 @@ class PagerPageHolder(
val bytesArray = openStream!!.readBytes() val bytesArray = openStream!!.readBytes()
val imageView = initSubsamplingImageView() val imageView = initSubsamplingImageView()
if (viewer.config.imageCropBorders) { val bytesStream = bytesArray.inputStream()
val bytesStream = bytesArray.inputStream() imageView.setImage(ImageSource.inputStream(bytesStream))
imageView.setImage(ImageSource.inputStream(bytesStream)) bytesStream.close()
bytesStream.close()
}
launchUI { launchUI {
val image = async { BitmapFactory.decodeByteArray(bytesArray, 0, bytesArray.size) } val image = async { BitmapFactory.decodeByteArray(bytesArray, 0, bytesArray.size) }
val bg = ImageUtil.autoSetBackground(image.await()) imageView.background = ImageUtil.autoSetBackground(image.await())
imageView.background = bg
if (!viewer.config.imageCropBorders) {
if (bg is ColorDrawable) {
val array = FloatArray(3)
ColorUtils.colorToHSL(bg.color, array)
if (array[1] < 0.2) {
val bytesStream = bytesArray.inputStream()
imageView.setImage(ImageSource.inputStream(bytesStream))
bytesStream.close()
}
else
imageView.setImage(ImageSource.bitmap(image.await()))
}
else
imageView.setImage(ImageSource.bitmap(image.await()))
}
} }
} }
else { else {

View File

@ -62,6 +62,8 @@ object ImageUtil {
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
val offsetX = (image.width * 0.01).toInt()
val offsetY = (image.height * 0.01).toInt()
val topLeftIsDark = isDark(image.getPixel(left, top)) val topLeftIsDark = isDark(image.getPixel(left, top))
val topRightIsDark = isDark(image.getPixel(right, top)) val topRightIsDark = isDark(image.getPixel(right, top))
val midLeftIsDark = isDark(image.getPixel(left, midY)) val midLeftIsDark = isDark(image.getPixel(left, midY))
@ -87,32 +89,43 @@ object ImageUtil {
isWhite(image.getPixel(right, bot)).toInt() > 2) isWhite(image.getPixel(right, bot)).toInt() > 2)
darkBG = false darkBG = false
var blackPixel = when {
topLeftIsDark -> image.getPixel(left, top)
topRightIsDark -> image.getPixel(right, top)
botLeftIsDark -> image.getPixel(left, bot)
else -> image.getPixel(right, bot)
}
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, left - offsetX, right, right + offsetX)) {
var whitePixelsStreak = 0 var whitePixelsStreak = 0
var whitePixels = 0 var whitePixels = 0
var blackPixelsStreak = 0 var blackPixelsStreak = 0
var blackPixels = 0 var blackPixels = 0
var blackStreak = false var blackStreak = false
var whiteStrak = false var whiteStrak = false
val notOffset = x == left || x == right
for (y in (0 until image.height step image.height / 25)) { for (y in (0 until image.height step image.height / 25)) {
val pixel = image.getPixel(x, y) val pixel = image.getPixel(x, y)
val pixelOff = image.getPixel(x + (if (x == left) -offsetX else offsetX), y)
if (isWhite(pixel)) { if (isWhite(pixel)) {
blackPixelsStreak = 0 blackPixelsStreak = 0
whitePixelsStreak++ whitePixelsStreak++
whitePixels++ whitePixels++
overallWhitePixels++ if (notOffset)
overallWhitePixels++
if (whitePixelsStreak > 14) { if (whitePixelsStreak > 14) {
whiteStrak = true whiteStrak = true
} }
} else { } else {
whitePixelsStreak = 0 whitePixelsStreak = 0
if (isDark(pixel)) { if (isDark(pixel) && isDark(pixelOff)) {
blackPixels++ blackPixels++
overallBlackPixels++ if (notOffset)
overallBlackPixels++
blackPixelsStreak++ blackPixelsStreak++
if (blackPixelsStreak > 14) { if (blackPixelsStreak >= 14) {
blackStreak = true blackStreak = true
} }
} else { } else {
@ -122,19 +135,34 @@ object ImageUtil {
} }
when { when {
blackPixels > 22 -> { blackPixels > 22 -> {
darkBG = true; overallWhitePixels = 0; break@outer if (x == right || x == right + offsetX)
blackPixel = when {
topRightIsDark -> image.getPixel(right, top)
botRightIsDark -> image.getPixel(right, bot)
else -> blackPixel
}
darkBG = true;
overallWhitePixels = 0;
break@outer
}
blackStreak -> {
darkBG = true
if (x == right || x == right + offsetX)
blackPixel = when {
topRightIsDark -> image.getPixel(right, top)
botRightIsDark -> image.getPixel(right, bot)
else -> blackPixel
}
if (blackPixels > 18) {
overallWhitePixels = 0;
break@outer
}
} }
blackStreak -> darkBG = true
whiteStrak || whitePixels > 22 -> darkBG = false whiteStrak || whitePixels > 22 -> darkBG = false
} }
} }
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) { if (overallWhitePixels > 9 && overallWhitePixels > overallBlackPixels) {
darkBG = false darkBG = false
} }
@ -151,7 +179,9 @@ object ImageUtil {
if (topLeftIsDark && topRightIsDark && (topMidIsDark || overallBlackPixels > 9)) if (topLeftIsDark && topRightIsDark && (topMidIsDark || overallBlackPixels > 9))
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))
else if (botLeftIsDark && botRightIsDark && (isDark(image.getPixel(midX, bot)) || overallBlackPixels > 9)) else if (botLeftIsDark && botRightIsDark
&& isDark(image.getPixel(left - offsetX, bot)) && isDark(image.getPixel(right + offsetX, bot))
&& (isDark(image.getPixel(midX, bot)) || overallBlackPixels > 9))
return GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, return GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM,
intArrayOf(Color.WHITE, Color.WHITE, blackPixel, blackPixel)) intArrayOf(Color.WHITE, Color.WHITE, blackPixel, blackPixel))
return ColorDrawable(Color.WHITE) return ColorDrawable(Color.WHITE)