Skip to content

Commit

Permalink
added privacy blur paint by #454
Browse files Browse the repository at this point in the history
  • Loading branch information
T8RIN committed Sep 27, 2023
1 parent 80e6f29 commit 7a7dc15
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ fun DrawScreen(
strokeWidth = strokeWidth,
onChangeStrokeWidth = { strokeWidth = it }
)
AnimatedVisibility(visible = drawMode !is DrawMode.Highlighter) {
AnimatedVisibility(visible = drawMode !is DrawMode.Highlighter && drawMode !is DrawMode.PrivacyBlur) {
BlurRadiusSelector(
modifier = Modifier
.padding(top = 16.dp, end = 16.dp, start = 16.dp),
Expand All @@ -328,11 +328,13 @@ fun DrawScreen(
} else {
Spacer(Modifier.height(16.dp))
}
DrawColorSelector(
drawColor = drawColor,
onColorChange = { drawColor = it }
)
AnimatedVisibility(visible = drawMode !is DrawMode.Neon) {
AnimatedVisibility(visible = drawMode !is DrawMode.PrivacyBlur) {
DrawColorSelector(
drawColor = drawColor,
onColorChange = { drawColor = it }
)
}
AnimatedVisibility(visible = drawMode !is DrawMode.Neon && drawMode !is DrawMode.PrivacyBlur) {
DrawAlphaSelector(
alpha = alpha,
onAlphaChange = { alpha = it }
Expand Down Expand Up @@ -428,6 +430,7 @@ fun DrawScreen(
scaffoldState = scaffoldState,
isBitmapChanged = viewModel.isBitmapChanged,
onBack = onBack,
imageManager = viewModel.getImageManager(),
onShare = { viewModel.shareBitmap { showConfetti() } },
paths = viewModel.paths,
isEraserOn = isEraserOn,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.SideEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
Expand All @@ -26,6 +27,7 @@ import androidx.compose.ui.graphics.BlendMode
import androidx.compose.ui.graphics.Canvas
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.graphics.ImageShader
import androidx.compose.ui.graphics.Paint
import androidx.compose.ui.graphics.PaintingStyle
import androidx.compose.ui.graphics.Path
Expand All @@ -38,21 +40,25 @@ import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.graphics.nativeCanvas
import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import com.smarttoolfactory.gesture.MotionEvent
import com.smarttoolfactory.gesture.pointerMotionEvents
import com.smarttoolfactory.image.util.update
import com.smarttoolfactory.image.zoom.animatedZoom
import com.smarttoolfactory.image.zoom.rememberAnimatedZoomState
import kotlinx.coroutines.launch
import ru.tech.imageresizershrinker.domain.image.ImageManager
import ru.tech.imageresizershrinker.presentation.erase_background_screen.components.PathPaint
import ru.tech.imageresizershrinker.presentation.erase_background_screen.components.transparencyChecker
import ru.tech.imageresizershrinker.presentation.root.theme.outlineVariant
import ru.tech.imageresizershrinker.presentation.root.transformation.filter.StackBlurFilter


@Composable
fun BitmapDrawer(
imageBitmap: ImageBitmap,
imageManager: ImageManager<Bitmap, *>,
paths: List<PathPaint>,
blurRadius: Float,
onAddPath: (PathPaint) -> Unit,
Expand All @@ -67,6 +73,7 @@ fun BitmapDrawer(
) {
val zoomState = rememberAnimatedZoomState(maxZoom = 30f)
val scope = rememberCoroutineScope()
val context = LocalContext.current

Box(
modifier = Modifier
Expand Down Expand Up @@ -114,6 +121,28 @@ fun BitmapDrawer(
.asImageBitmap()
}

var blurredBitmap by remember {
mutableStateOf<ImageBitmap?>(null)
}

LaunchedEffect(imageBitmap, drawBitmap) {
blurredBitmap = imageManager.transform(
image = drawImageBitmap.overlay(drawBitmap).asAndroidBitmap(),
transformations = listOf(StackBlurFilter(context, 0.3f to 50))
)?.asImageBitmap()
}

val shaderBitmap = remember(blurredBitmap) {
blurredBitmap?.asAndroidBitmap()?.let {
Bitmap.createScaledBitmap(
it,
imageWidth,
imageHeight,
false
).asImageBitmap()
}
}

SideEffect {
onDraw(drawImageBitmap.overlay(drawBitmap).asAndroidBitmap())
}
Expand All @@ -129,6 +158,9 @@ fun BitmapDrawer(
strokeCap =
if (drawMode is DrawMode.Highlighter) StrokeCap.Square else StrokeCap.Round
color = drawColor
shader = if (drawMode is DrawMode.PrivacyBlur) {
shaderBitmap?.let { ImageShader(it) }
} else shader
alpha = drawColor.alpha
this.strokeWidth = strokeWidth
strokeJoin = StrokeJoin.Round
Expand Down Expand Up @@ -197,7 +229,6 @@ fun BitmapDrawer(
drawColor(Color.Transparent.toArgb(), PorterDuff.Mode.CLEAR)
drawColor(backgroundColor.toArgb())


paths.forEach { (path, stroke, radius, drawColor, isErasing, effect) ->
this.drawPath(
path.asAndroidPath(),
Expand All @@ -206,6 +237,9 @@ fun BitmapDrawer(
style = PaintingStyle.Stroke
strokeCap =
if (effect is DrawMode.Highlighter) StrokeCap.Square else StrokeCap.Round
shader = if (effect is DrawMode.PrivacyBlur) shaderBitmap?.let {
ImageShader(it)
} else shader
this.strokeWidth = stroke
strokeJoin = StrokeJoin.Round
isAntiAlias = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ import dev.olshevski.navigation.reimagined.AnimatedNavHost
import dev.olshevski.navigation.reimagined.NavController
import kotlinx.coroutines.launch
import ru.tech.imageresizershrinker.R
import ru.tech.imageresizershrinker.domain.image.ImageManager
import ru.tech.imageresizershrinker.presentation.erase_background_screen.components.PathPaint
import ru.tech.imageresizershrinker.presentation.root.utils.helper.ImageUtils.restrict
import ru.tech.imageresizershrinker.presentation.root.utils.modifier.container
Expand All @@ -102,6 +103,7 @@ import ru.tech.imageresizershrinker.presentation.root.widget.text.TitleItem
@Composable
fun DrawHost(
modifier: Modifier,
imageManager: ImageManager<Bitmap, *>,
navController: NavController<DrawBehavior>,
portrait: Boolean,
zoomEnabled: Boolean,
Expand Down Expand Up @@ -157,6 +159,7 @@ fun DrawHost(
.fillMaxSize(),
zoomEnabled = zoomEnabled,
onDraw = onDraw,
imageManager = imageManager,
backgroundColor = backgroundColor
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
package ru.tech.imageresizershrinker.presentation.draw_screen.components

sealed class DrawMode(val ordinal: Int) {
data object Neon : DrawMode(1)
data object Highlighter : DrawMode(2)
data object Neon : DrawMode(2)
data object Highlighter : DrawMode(3)
data object Pen : DrawMode(0)

data object PrivacyBlur : DrawMode(1)

companion object {
val entries by lazy {
listOf(
Pen, Neon, Highlighter
Pen, PrivacyBlur, Neon, Highlighter
)
}

operator fun invoke(ordinal: Int) = when (ordinal) {
1 -> Neon
2 -> Highlighter
0 -> Pen
1 -> PrivacyBlur
2 -> Neon
3 -> Highlighter
else -> Pen
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import androidx.compose.foundation.layout.width
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.BlurCircular
import androidx.compose.material.icons.rounded.Brush
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
Expand Down Expand Up @@ -104,6 +105,7 @@ fun DrawModeSelector(
DrawMode.Highlighter -> Icons.Rounded.Highlighter
DrawMode.Neon -> Icons.Rounded.Laser
DrawMode.Pen -> Icons.Rounded.Brush
is DrawMode.PrivacyBlur -> Icons.Rounded.BlurCircular
},
contentDescription = null
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class DrawViewModel @Inject constructor(
private val imageManager: ImageManager<Bitmap, ExifInterface>
) : ViewModel() {

fun getImageManager(): ImageManager<Bitmap, ExifInterface> = imageManager

private val _bitmap: MutableState<Bitmap?> = mutableStateOf(null)
val bitmap: Bitmap? by _bitmap

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.rounded.ArrowBack
import androidx.compose.material.icons.outlined.Share
import androidx.compose.material.icons.rounded.ArrowBack
import androidx.compose.material.icons.rounded.Compare
import androidx.compose.material.icons.rounded.History
import androidx.compose.material.icons.rounded.RestartAlt
Expand Down Expand Up @@ -453,7 +453,7 @@ fun SingleEditScreen(
IconButton(
onClick = onBack
) {
Icon(Icons.Rounded.ArrowBack, null)
Icon(Icons.AutoMirrored.Rounded.ArrowBack, null)
}
},
actions = {
Expand Down Expand Up @@ -591,6 +591,7 @@ fun SingleEditScreen(
)

DrawEditOption(
imageManager = viewModel.getImageManager(),
visible = showDrawing,
onDismiss = {
showDrawing = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import androidx.compose.ui.graphics.asImageBitmap
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import ru.tech.imageresizershrinker.R
import ru.tech.imageresizershrinker.domain.image.ImageManager
import ru.tech.imageresizershrinker.presentation.draw_screen.components.BitmapDrawer
import ru.tech.imageresizershrinker.presentation.draw_screen.components.BlurRadiusSelector
import ru.tech.imageresizershrinker.presentation.draw_screen.components.DrawAlphaSelector
Expand Down Expand Up @@ -73,6 +74,7 @@ import ru.tech.imageresizershrinker.presentation.root.widget.utils.LocalSettings
@Composable
fun DrawEditOption(
visible: Boolean,
imageManager: ImageManager<Bitmap, *>,
onDismiss: () -> Unit,
useScaffold: Boolean,
bitmap: Bitmap?,
Expand Down Expand Up @@ -202,7 +204,7 @@ fun DrawEditOption(
strokeWidth = strokeWidth,
onChangeStrokeWidth = { strokeWidth = it }
)
AnimatedVisibility(visible = drawMode !is DrawMode.Highlighter) {
AnimatedVisibility(visible = drawMode !is DrawMode.Highlighter && drawMode !is DrawMode.PrivacyBlur) {
BlurRadiusSelector(
modifier = Modifier
.padding(top = 16.dp, end = 16.dp, start = 16.dp),
Expand All @@ -211,11 +213,13 @@ fun DrawEditOption(
)
}
Spacer(modifier = Modifier.height(16.dp))
DrawColorSelector(
drawColor = drawColor,
onColorChange = { drawColor = it }
)
AnimatedVisibility(visible = drawMode !is DrawMode.Neon) {
AnimatedVisibility(visible = drawMode !is DrawMode.PrivacyBlur) {
DrawColorSelector(
drawColor = drawColor,
onColorChange = { drawColor = it }
)
}
AnimatedVisibility(visible = drawMode !is DrawMode.Neon && drawMode !is DrawMode.PrivacyBlur) {
DrawAlphaSelector(
alpha = alpha,
onAlphaChange = { alpha = it }
Expand Down Expand Up @@ -280,6 +284,7 @@ fun DrawEditOption(
BitmapDrawer(
imageBitmap = imageBitmap,
paths = paths,
imageManager = imageManager,
strokeWidth = strokeWidth,
blurRadius = blurRadius,
drawColor = drawColor.copy(alpha),
Expand Down

0 comments on commit 7a7dc15

Please sign in to comment.