1
+ package com.engineer.compose.ui.gpufilter
2
+
3
+ import android.graphics.BitmapFactory
4
+ import android.os.Bundle
5
+ import android.util.Log
6
+ import androidx.activity.ComponentActivity
7
+ import androidx.activity.compose.setContent
8
+ import androidx.activity.enableEdgeToEdge
9
+ import androidx.compose.foundation.Image
10
+ import androidx.compose.foundation.background
11
+ import androidx.compose.foundation.clickable
12
+ import androidx.compose.foundation.layout.Arrangement
13
+ import androidx.compose.foundation.layout.Box
14
+ import androidx.compose.foundation.layout.Column
15
+ import androidx.compose.foundation.layout.Spacer
16
+ import androidx.compose.foundation.layout.aspectRatio
17
+ import androidx.compose.foundation.layout.fillMaxSize
18
+ import androidx.compose.foundation.layout.fillMaxWidth
19
+ import androidx.compose.foundation.layout.height
20
+ import androidx.compose.foundation.layout.padding
21
+ import androidx.compose.foundation.layout.size
22
+ import androidx.compose.foundation.lazy.LazyRow
23
+ import androidx.compose.foundation.lazy.items
24
+ import androidx.compose.foundation.shape.CircleShape
25
+ import androidx.compose.foundation.shape.RoundedCornerShape
26
+ import androidx.compose.material3.Scaffold
27
+ import androidx.compose.material3.Text
28
+ import androidx.compose.runtime.Composable
29
+ import androidx.compose.runtime.getValue
30
+ import androidx.compose.runtime.mutableStateOf
31
+ import androidx.compose.runtime.remember
32
+ import androidx.compose.runtime.setValue
33
+ import androidx.compose.ui.Alignment
34
+ import androidx.compose.ui.Modifier
35
+ import androidx.compose.ui.draw.clip
36
+ import androidx.compose.ui.graphics.Color
37
+ import androidx.compose.ui.graphics.asImageBitmap
38
+ import androidx.compose.ui.graphics.painter.BitmapPainter
39
+ import androidx.compose.ui.layout.ContentScale
40
+ import androidx.compose.ui.text.font.FontWeight
41
+ import androidx.compose.ui.tooling.preview.Preview
42
+ import androidx.compose.ui.unit.dp
43
+ import androidx.compose.ui.unit.sp
44
+ import com.engineer.compose.ui.gpufilter.ui.theme.AndroidAnimationExerciseTheme
45
+ import com.pixpark.gpupixel.GPUPixel
46
+ import com.pixpark.gpupixel.GPUPixelSourceImage
47
+ import com.pixpark.gpupixel.filter.GPUPixelFilter
48
+
49
+ class GPUFilterActivity : ComponentActivity () {
50
+ private lateinit var gpuPixelSourceImage: GPUPixelSourceImage
51
+
52
+ override fun onCreate (savedInstanceState : Bundle ? ) {
53
+ super .onCreate(savedInstanceState)
54
+ enableEdgeToEdge()
55
+ val url = intent.getStringExtra(" url" ) ? : " "
56
+
57
+ gpuPixelSourceImage = GPUPixelSourceImage (BitmapFactory .decodeFile(url))
58
+
59
+ setContent {
60
+ AndroidAnimationExerciseTheme {
61
+ Scaffold (modifier = Modifier .fillMaxSize()) { innerPadding ->
62
+ Log .i(" " , innerPadding.toString())
63
+ PhotoFilterScreen (url) {
64
+ Log .i(" zyq" , " filter is $it " )
65
+ val result = it.replace(" " , " " )
66
+ // val gpuFilter = GPUPixelFilter(result)
67
+ // gpuPixelSourceImage.removeAllTargets()
68
+
69
+ // gpuPixelSourceImage.addTarget(gpuFilter)
70
+ }
71
+ }
72
+ }
73
+ }
74
+ GPUPixel .setContext(this )
75
+ }
76
+ }
77
+
78
+ @Composable
79
+ fun PhotoFilterScreen (url : String , cb : (String ) -> Unit ) {
80
+ var selectedFilter by remember { mutableStateOf(Filter (" Color Invert Filter" , " Inverts image colors" )) }
81
+ val imageModifier = Modifier
82
+ .fillMaxWidth()
83
+ .aspectRatio(16f / 9f )
84
+ .clip(RoundedCornerShape (8 .dp))
85
+
86
+ Column (modifier = Modifier .fillMaxSize()) {
87
+ // TopAppBar(
88
+ // title = { Text("photo filter") },
89
+ // navigationIcon = {
90
+ // IconButton(onClick = { /* 返回逻辑 */ }) {
91
+ // Icon(Icons.Default.ArrowBack, contentDescription = "Back")
92
+ // }
93
+ // },
94
+ // backgroundColor = MaterialTheme.colors.primary,
95
+ // contentColor = Color.White
96
+ // )
97
+
98
+ Spacer (modifier = Modifier .height(8 .dp))
99
+
100
+ Box (
101
+ modifier = Modifier
102
+ .fillMaxWidth()
103
+ .weight(1f )
104
+ .background(Color .Gray , RoundedCornerShape (8 .dp)),
105
+ contentAlignment = Alignment .Center
106
+ ) {
107
+ val bitmap = BitmapFactory .decodeFile(url)
108
+ val bitmapPainter = BitmapPainter ((bitmap.asImageBitmap()))
109
+ Image (
110
+ painter = bitmapPainter,
111
+ contentDescription = " Filtered Image" ,
112
+ modifier = imageModifier,
113
+ contentScale = ContentScale .Crop
114
+ )
115
+ }
116
+
117
+ Spacer (modifier = Modifier .height(8 .dp))
118
+
119
+ LazyRow (
120
+ modifier = Modifier
121
+ .fillMaxWidth()
122
+ .padding(8 .dp), horizontalArrangement = Arrangement .spacedBy(8 .dp)
123
+ ) {
124
+ // val filters = listOf("Blur", "Brightness", "CircleCrop", "ColorFilter", "Contrast")
125
+ items(filters) { filter ->
126
+ FilterItem (filter.name, isSelected = selectedFilter == filter) {
127
+ selectedFilter = filter
128
+ cb(selectedFilter.name)
129
+ }
130
+ }
131
+ }
132
+ }
133
+ }
134
+
135
+ data class Filter (val name : String , val desc : String )
136
+
137
+ val filters = listOf (
138
+ Filter (" Color Invert Filter" , " Inverts image colors" ),
139
+ Filter (" Color Matrix Filter" , " Applies color matrix transformation" ),
140
+ Filter (" Crosshatch Filter" , " Creates crosshatch drawing effect" ),
141
+ Filter (" Emboss Filter" , " Creates embossed effect" ),
142
+ Filter (" Glass Sphere Filter" , " Spherical glass distortion effect" ),
143
+ Filter (" Halftone Filter" , " Creates halftone pattern effect" ),
144
+ Filter (" Pixellation Filter" , " Creates pixelated effect" ),
145
+ Filter (" Posterize Filter" , " Reduces image to limited colors" ),
146
+ Filter (" Sketch Filter" , " Creates sketch-like effect" ),
147
+ Filter (" Smooth Toon Filter" , " Cartoon effect with smoothing" ),
148
+ Filter (" Sphere Refraction Filter" , " Spherical refraction effect" ),
149
+ Filter (" Toon Filter" , " Cartoon/comic effect" )
150
+ )
151
+
152
+ @Composable
153
+ fun FilterItem (filterName : String , isSelected : Boolean , onClick : () -> Unit ) {
154
+ Column (
155
+ horizontalAlignment = Alignment .CenterHorizontally ,
156
+ modifier = Modifier
157
+ .clickable(onClick = onClick)
158
+ .padding(8 .dp)
159
+ ) {
160
+ Box (
161
+ modifier = Modifier
162
+ .size(60 .dp)
163
+ .clip(CircleShape )
164
+ .background(if (isSelected) Color .Blue else Color .LightGray ), contentAlignment = Alignment .Center
165
+ ) {
166
+ Text (filterName.first().toString(), color = Color .White , fontWeight = FontWeight .Bold )
167
+ }
168
+ Text (filterName, fontSize = 12 .sp)
169
+ }
170
+ }
171
+
172
+
173
+ @Preview(showBackground = true )
174
+ @Composable
175
+ fun GreetingPreview () {
176
+ AndroidAnimationExerciseTheme {
177
+ PhotoFilterScreen (" " ) {}
178
+ }
179
+ }
0 commit comments