Skip to content

Commit

Permalink
fix!: Immutable bitmap error
Browse files Browse the repository at this point in the history
  • Loading branch information
Eren Alpaslan committed Oct 4, 2023
1 parent 2cb241a commit d95d43e
Show file tree
Hide file tree
Showing 8 changed files with 220 additions and 38 deletions.
123 changes: 123 additions & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 12 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>


<service android:name="com.google.android.gms.metadata.ModuleDependencies"
android:enabled="false"
android:exported="false"
tools:ignore="MissingClass">
<intent-filter>
<action android:name="com.google.android.gms.metadata.MODULE_DEPENDENCIES" />
</intent-filter>
<meta-data android:name="photopicker_activity:0:required" android:value="" />
</service>

</application>
</manifest>
106 changes: 73 additions & 33 deletions app/src/main/java/dev/eren/removebg/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
package dev.eren.removebg

import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.os.Bundle
import android.provider.MediaStore
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.compose.setContent
import androidx.activity.result.PickVisualMediaRequest
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.MaterialTheme
Expand All @@ -31,12 +38,13 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.asImageBitmap
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.tooling.preview.Preview
import dev.eren.removebg.ui.theme.RemovebgTheme
import kotlinx.coroutines.flow.onCompletion
import kotlinx.coroutines.flow.onStart


class MainActivity : ComponentActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
Expand All @@ -61,50 +69,82 @@ fun RemoveBackground() {
mutableStateOf<Bitmap?>(null)
}

val inputImage: MutableState<Bitmap> = remember {
mutableStateOf(BitmapFactory.decodeStream(context.assets.open("remove_test5.png")))
val inputImage: MutableState<Bitmap?> = remember {
mutableStateOf(null)
}

val pickMedia = rememberLauncherForActivityResult(
contract = ActivityResultContracts.PickVisualMedia(),
onResult = { uri ->
if (uri != null) {
inputImage.value =
MediaStore.Images.Media.getBitmap(context.contentResolver, uri)
} else {
Log.d("PhotoPicker", "No media selected")
}
})

var loading: Boolean by remember {
mutableStateOf(true)
mutableStateOf(false)
}

var isReal: Boolean by remember {
mutableStateOf(false)
}

LaunchedEffect(key1 = Unit) {
val remover = RemoveBg(context)
remover.clearBackground(inputImage.value)
.onStart {
loading = true
}
.onCompletion {
loading = false
}.collect { output ->
outputImage.value = output
}
val remover = remember {
RemoveBg(context = context)
}

LaunchedEffect(key1 = inputImage.value) {
inputImage.value?.let { image ->
remover.clearBackground(image)
.onStart {
loading = true
}
.onCompletion {
loading = false
}.collect { output ->
outputImage.value = output
}
}
}

Scaffold { paddingValues ->
Column(modifier = Modifier
.fillMaxSize()
.padding(paddingValues)
.background(Color.White),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
if (loading) {
CircularProgressIndicator()
Box(modifier = Modifier.background(Color.White)) {
Row(
horizontalArrangement = Arrangement.End,
modifier = Modifier
.padding(paddingValues)
.fillMaxWidth()
) {
Button(onClick = {
pickMedia.launch(PickVisualMediaRequest(ActivityResultContracts.PickVisualMedia.ImageOnly))
}) {
Text(text = "Open Gallery")
}
}
if (outputImage.value != null) {
Image(
bitmap = if (!isReal) outputImage.value!!.asImageBitmap() else inputImage.value.asImageBitmap(),
contentDescription = "",
Modifier.fillMaxWidth().clickable {
isReal = !isReal
}
)
Column(
modifier = Modifier
.fillMaxSize()
.padding(paddingValues),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
if (loading) {
CircularProgressIndicator()
}
if (outputImage.value != null && inputImage.value != null) {
Image(
bitmap = if (!isReal) outputImage.value!!.asImageBitmap() else inputImage.value!!.asImageBitmap(),
contentDescription = "",
Modifier
.fillMaxWidth()
.clickable {
isReal = !isReal
}
)
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[versions]
agp = "8.2.0-alpha14"
agp = "8.3.0-alpha06"
androidx-lifecycle-runtime-ktx = "2.6.1"
kotlin = "1.8.10"
core-ktx = "1.9.0"
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Mon Sep 25 10:16:23 TRT 2023
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.2-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.3-rc-2-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
3 changes: 2 additions & 1 deletion removebg/src/main/java/dev/eren/removebg/RemoveBg.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ class RemoveBg(context: Context): Remover<Bitmap> {
}

override fun clearBackground(image: Bitmap): Flow<Bitmap?> = flow {
emit(removeBackground(image))
val mutableImage = image.copy(Bitmap.Config.ARGB_8888, true)
emit(removeBackground(mutableImage))
}.flowOn(Dispatchers.IO)

override fun getMaskedImage(input: Bitmap, mask: Bitmap): Bitmap {
Expand Down

0 comments on commit d95d43e

Please sign in to comment.