Skip to content

Commit 7cf8cc4

Browse files
author
izaak
committed
Run blurring in a coroutine
1 parent cdc4085 commit 7cf8cc4

File tree

1 file changed

+24
-17
lines changed
  • app/src/main/java/co/stonephone/stonecamera/plugins

1 file changed

+24
-17
lines changed

app/src/main/java/co/stonephone/stonecamera/plugins/Portrait.kt

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ import com.google.mediapipe.framework.image.BitmapImageBuilder
1717
import com.google.mediapipe.framework.image.ByteBufferExtractor
1818
import com.google.mediapipe.tasks.vision.core.RunningMode
1919
import com.google.mediapipe.tasks.vision.imagesegmenter.ImageSegmenterResult
20+
import kotlinx.coroutines.CoroutineScope
21+
import kotlinx.coroutines.Dispatchers
22+
import kotlinx.coroutines.launch
2023
import java.io.File
2124
import java.io.FileOutputStream
2225
import java.io.OutputStream
@@ -50,23 +53,27 @@ class PortraitModePlugin : IPlugin, ImageSegmenterHelper.SegmenterListener {
5053
stoneCameraViewModel: StoneCameraViewModel,
5154
outputFileResults: ImageCapture.OutputFileResults
5255
) {
53-
val contentResolver: ContentResolver = MyApplication.getAppContext().contentResolver
54-
val imageUri = outputFileResults.savedUri ?: return
55-
56-
// Open the input stream of the original image
57-
val inputStream = contentResolver.openInputStream(imageUri)
58-
val bitmap: Bitmap = BitmapFactory.decodeStream(inputStream)
59-
60-
val segmentationResults: ImageSegmenterResult =
61-
imageSegmenterHelper.segmentImageFile(BitmapImageBuilder(bitmap).build()) ?: return
62-
63-
val categoryMask: ByteBuffer =
64-
ByteBufferExtractor.extract(segmentationResults.categoryMask().get())
65-
66-
val blurred =
67-
applyBlurBasedOnMask(MyApplication.getAppContext(), imageUri, categoryMask) ?: return
68-
blurred.saveImage(MyApplication.getAppContext())
69-
inputStream?.close()
56+
CoroutineScope(Dispatchers.IO).launch {
57+
58+
val contentResolver: ContentResolver = MyApplication.getAppContext().contentResolver
59+
val imageUri = outputFileResults.savedUri ?: return@launch
60+
61+
// Open the input stream of the original image
62+
val inputStream = contentResolver.openInputStream(imageUri)
63+
val bitmap: Bitmap = BitmapFactory.decodeStream(inputStream)
64+
val segmentationResults: ImageSegmenterResult =
65+
imageSegmenterHelper.segmentImageFile(BitmapImageBuilder(bitmap).build())
66+
?: return@launch
67+
68+
val categoryMask: ByteBuffer =
69+
ByteBufferExtractor.extract(segmentationResults.categoryMask().get())
70+
71+
val blurred =
72+
applyBlurBasedOnMask(MyApplication.getAppContext(), imageUri, categoryMask)
73+
?: return@launch
74+
blurred.saveImage(MyApplication.getAppContext())
75+
inputStream?.close()
76+
}
7077
}
7178

7279
// Stolen from https://stackoverflow.com/questions/21418892/understanding-super-fast-blur-algorithm?fbclid=IwZXh0bgNhZW0CMTEAAR1w91ucNtw4nU-Z8Z9RyMYFVUHWxfgt7ivsE7foTkwR2wmdx2losQqQ0sk_aem_Zrf_8344PRxW6SFzutkE7g

0 commit comments

Comments
 (0)