Fix Portrait Blur Viewfinder and Captured photo: enable software blur on viewfinder preview when blurIntensity > 0, sync updateBackgroundMode, and scale blur radius based on width

This commit is contained in:
2026-07-06 22:10:39 +07:00
parent 50ab58ae3e
commit 2de35196db
@@ -89,8 +89,8 @@ class MainActivity : AppCompatActivity() {
private fun updateBackgroundMode() { private fun updateBackgroundMode() {
currentBackgroundMode = when { currentBackgroundMode = when {
selectedBgAssetPath != null -> BackgroundMode.AI_BLUR selectedBgAssetPath != null || blurIntensity > 0f -> BackgroundMode.AI_BLUR
blurIntensity > 0f || isPortraitBlurModeOpen -> BackgroundMode.HARDWARE_DOF isPortraitBlurModeOpen -> BackgroundMode.HARDWARE_DOF
else -> BackgroundMode.DEFAULT else -> BackgroundMode.DEFAULT
} }
} }
@@ -1090,6 +1090,7 @@ class MainActivity : AppCompatActivity() {
private fun setCurrentAttributeValue(value: Float) { private fun setCurrentAttributeValue(value: Float) {
if (currentAttribute == EditAttribute.PORTRAIT_BLUR) { if (currentAttribute == EditAttribute.PORTRAIT_BLUR) {
blurIntensity = value.coerceIn(0f, 1f) blurIntensity = value.coerceIn(0f, 1f)
updateBackgroundMode()
return return
} }
if (currentAttribute == EditAttribute.BG_REPLACE) return if (currentAttribute == EditAttribute.BG_REPLACE) return
@@ -1767,13 +1768,6 @@ class MainActivity : AppCompatActivity() {
cachedMaskHeight = h cachedMaskHeight = h
categoryMaskBuffer.rewind() categoryMaskBuffer.rewind()
// Ở chế độ Portrait Blur thấu kính vật lý (selectedBgAssetPath == null),
// sau khi đã lưu cache mask để hỗ trợ xác định đối tượng chạm,
// ta bỏ qua hoàn toàn việc tạo mặt nạ alpha và làm mờ phông nền nhân tạo.
if (selectedBgAssetPath == null) {
return capturedImage
}
// Create and blur alpha mask with Touch-to-Focus class selection // Create and blur alpha mask with Touch-to-Focus class selection
val focusClassId = if (userSelectedClassFocus >= 0) userSelectedClassFocus else -1 val focusClassId = if (userSelectedClassFocus >= 0) userSelectedClassFocus else -1
val alphaMask = FloatArray(w * h) val alphaMask = FloatArray(w * h)
@@ -1815,8 +1809,8 @@ class MainActivity : AppCompatActivity() {
val bgPixels = IntArray(w * h) val bgPixels = IntArray(w * h)
var blurredBg: android.graphics.Bitmap? = null var blurredBg: android.graphics.Bitmap? = null
if (blurIntensity > 0f) { if (blurIntensity > 0f) {
val opticalIntensity = Math.pow(blurIntensity.toDouble(), 2.5).toFloat() val maxRadius = if (w <= 480) 15f else 75f
val radius = (25 * opticalIntensity).toInt().coerceIn(1, 25) val radius = (2f + blurIntensity * maxRadius).toInt().coerceIn(1, 120)
blurredBg = blurBitmap(baseBg, radius) blurredBg = blurBitmap(baseBg, radius)
blurredBg.getPixels(bgPixels, 0, w, 0, 0, w, h) blurredBg.getPixels(bgPixels, 0, w, 0, 0, w, h)
} else { } else {
@@ -1943,8 +1937,8 @@ class MainActivity : AppCompatActivity() {
val bgPixels = IntArray(w * h) val bgPixels = IntArray(w * h)
var blurredBg: android.graphics.Bitmap? = null var blurredBg: android.graphics.Bitmap? = null
if (blurIntensity > 0f) { if (blurIntensity > 0f) {
val opticalIntensity = Math.pow(blurIntensity.toDouble(), 2.5).toFloat() val maxRadius = if (w <= 480) 15f else 75f
val radius = (40 * opticalIntensity).toInt().coerceIn(1, 40) val radius = (2f + blurIntensity * maxRadius).toInt().coerceIn(1, 120)
blurredBg = blurBitmap(baseBg, radius) blurredBg = blurBitmap(baseBg, radius)
blurredBg.getPixels(bgPixels, 0, w, 0, 0, w, h) blurredBg.getPixels(bgPixels, 0, w, 0, 0, w, h)
} else { } else {
@@ -2381,8 +2375,8 @@ class MainActivity : AppCompatActivity() {
val bgPixels = IntArray(w * h) val bgPixels = IntArray(w * h)
var blurredBg: android.graphics.Bitmap? = null var blurredBg: android.graphics.Bitmap? = null
if (blurIntensity > 0f) { if (blurIntensity > 0f) {
val opticalIntensity = Math.pow(blurIntensity.toDouble(), 2.5).toFloat() val maxRadius = if (w <= 480) 15f else 75f
val radius = (40 * opticalIntensity).toInt().coerceIn(1, 40) val radius = (2f + blurIntensity * maxRadius).toInt().coerceIn(1, 120)
blurredBg = blurBitmap(baseBg, radius) blurredBg = blurBitmap(baseBg, radius)
blurredBg.getPixels(bgPixels, 0, w, 0, 0, w, h) blurredBg.getPixels(bgPixels, 0, w, 0, 0, w, h)
} else { } else {