-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainActivity.kt
More file actions
340 lines (297 loc) · 13.3 KB
/
Copy pathMainActivity.kt
File metadata and controls
340 lines (297 loc) · 13.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
package com.example.photoeditor
import android.Manifest
import android.content.ContentValues
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Matrix
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.provider.MediaStore
import android.widget.*
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.FileProvider
import androidx.lifecycle.lifecycleScope
import com.bumptech.glide.Glide
import jp.co.cyberagent.android.gpuimage.GPUImage
import jp.co.cyberagent.android.gpuimage.filter.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.io.File
import java.io.FileOutputStream
import java.text.SimpleDateFormat
import java.util.*
class MainActivity : AppCompatActivity() {
private lateinit var imageView: ImageView
private lateinit var brightnessSeekBar: SeekBar
private lateinit var filterSpinner: Spinner
private lateinit var btnSelect: Button
private lateinit var btnRotate: Button
private lateinit var btnFlip: Button
private lateinit var btnFilter: Button
private lateinit var btnSave: Button
private lateinit var btnShare: Button
private lateinit var btnSwitchAccount: Button
private lateinit var imgAccountProfile: ImageView
private lateinit var txtAccountName: TextView
private var imageUri: Uri? = null
private var lastBitmap: Bitmap? = null
private lateinit var gpuImage: GPUImage
private val requestPermissionLauncher = registerForActivityResult(
ActivityResultContracts.RequestPermission()
) { isGranted ->
if (isGranted) pickImageFromGallery()
else Toast.makeText(this, "Permission denied, cannot select image", Toast.LENGTH_SHORT).show()
}
private val pickImageLauncher = registerForActivityResult(
ActivityResultContracts.GetContent()
) { uri: Uri? ->
uri?.let {
imageUri = it
Glide.with(this).load(it).into(imageView)
lifecycleScope.launch {
lastBitmap = withContext(Dispatchers.IO) {
MediaStore.Images.Media.getBitmap(contentResolver, it)
}
gpuImage.setImage(lastBitmap)
}
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Initialize UI elements
imageView = findViewById(R.id.imageView)
brightnessSeekBar = findViewById(R.id.seekBarBrightness)
filterSpinner = findViewById(R.id.spinnerFilters)
btnSelect = findViewById(R.id.btnSelect)
btnRotate = findViewById(R.id.btnRotate)
btnFlip = findViewById(R.id.btnFlip)
btnFilter = findViewById(R.id.btnFilter)
btnSave = findViewById(R.id.btnSave)
btnShare = findViewById(R.id.btnShare)
btnSwitchAccount = findViewById(R.id.btnSwitchAccount)
imgAccountProfile = findViewById(R.id.imgAccountProfile)
txtAccountName = findViewById(R.id.txtAccountName)
gpuImage = GPUImage(this)
val prefs = getSharedPreferences("user", MODE_PRIVATE)
val username = prefs.getString("username", "User") ?: "User"
txtAccountName.text = username
val profileImg = prefs.getString("profile_img", null)
if (profileImg != null && File(profileImg).exists()) {
imgAccountProfile.setImageBitmap(BitmapFactory.decodeFile(profileImg))
}
btnSwitchAccount.setOnClickListener {
prefs.edit().clear().apply()
startActivity(Intent(this, LoginActivity::class.java))
finish()
}
txtAccountName.setOnClickListener {
showAccountDetailsDialog(username)
}
btnSelect.setOnClickListener {
if (hasStoragePermission()) pickImageFromGallery()
else requestStoragePermission()
}
brightnessSeekBar.max = 200
brightnessSeekBar.progress = 100
brightnessSeekBar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
lastBitmap?.let {
val brightness = (progress - 100) / 100.0f
gpuImage.setImage(it)
gpuImage.setFilter(GPUImageBrightnessFilter(brightness))
imageView.setImageBitmap(gpuImage.bitmapWithFilterApplied)
}
}
override fun onStartTrackingTouch(seekBar: SeekBar?) {}
override fun onStopTrackingTouch(seekBar: SeekBar?) {}
})
val filters = listOf("None", "Grayscale", "Invert", "Sketch", "Gaussian Blur")
filterSpinner.adapter = ArrayAdapter(this, android.R.layout.simple_spinner_dropdown_item, filters)
filterSpinner.setSelection(0)
filterSpinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(parent: AdapterView<*>?, view: android.view.View?, position: Int, id: Long) {
lastBitmap?.let {
val filter = when (filters[position]) {
"None" -> GPUImageFilter()
"Grayscale" -> GPUImageGrayscaleFilter()
"Invert" -> GPUImageColorInvertFilter()
"Sketch" -> GPUImageSketchFilter()
"Gaussian Blur" -> GPUImageGaussianBlurFilter()
else -> GPUImageFilter()
}
gpuImage.setImage(it)
gpuImage.setFilter(filter)
imageView.setImageBitmap(gpuImage.bitmapWithFilterApplied)
}
}
override fun onNothingSelected(parent: AdapterView<*>?) {}
}
btnRotate.setOnClickListener {
lastBitmap = lastBitmap?.let { rotateBitmap(it, 90f) }
lastBitmap?.let { imageView.setImageBitmap(it) }
}
btnFlip.setOnClickListener {
lastBitmap = lastBitmap?.let { flipBitmap(it) }
lastBitmap?.let { imageView.setImageBitmap(it) }
}
btnFilter.setOnClickListener {
// Add additional filter actions if required
}
btnSave.setOnClickListener {
val editedBitmap = gpuImage.bitmapWithFilterApplied
if (editedBitmap != null) {
saveImageToGallery(editedBitmap)
saveEditedPhotoToAccountPerUser(editedBitmap, username)
Toast.makeText(this, "Photo saved successfully", Toast.LENGTH_SHORT).show()
}
}
btnShare.setOnClickListener {
val bitmap = gpuImage.bitmapWithFilterApplied
if (bitmap != null) {
shareImage(bitmap)
} else {
Toast.makeText(this, "No image available to share", Toast.LENGTH_SHORT).show()
}
}
}
private fun showAccountDetailsDialog(username: String) {
val prefs = getSharedPreferences("user", MODE_PRIVATE)
val name = prefs.getString("name", "Name not set")
val dob = prefs.getString("dob", "DOB not set")
val email = prefs.getString("email", "Email not set")
val photosKey = "${username}_photos"
val userPhotoPaths = prefs.getStringSet(photosKey, emptySet()) ?: emptySet()
val container = LinearLayout(this).apply {
orientation = LinearLayout.VERTICAL
setPadding(24, 24, 24, 24)
}
val headerText = "Name: $name\nDOB: $dob\nEmail: $email\nUsername: $username"
val tvHeader = TextView(this).apply {
text = headerText
setPadding(0, 0, 0, 16)
}
container.addView(tvHeader)
val scrollView = ScrollView(this)
val photosContainer = LinearLayout(this).apply {
orientation = LinearLayout.VERTICAL
}
if (userPhotoPaths.isEmpty()) {
photosContainer.addView(TextView(this).apply { text = "No photos available." })
} else {
userPhotoPaths.forEach { path ->
val file = File(path)
if (file.exists()) {
val imageViewItem = ImageView(this).apply {
val bmp = BitmapFactory.decodeFile(path)
setImageBitmap(bmp)
adjustViewBounds = true
layoutParams = LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
(200 * resources.displayMetrics.density).toInt()
).apply { bottomMargin = 8 }
scaleType = ImageView.ScaleType.CENTER_CROP
setOnClickListener {
val iv = ImageView(this@MainActivity).apply {
setImageBitmap(bmp)
}
AlertDialog.Builder(this@MainActivity)
.setView(iv)
.setPositiveButton("Close", null)
.show()
}
}
photosContainer.addView(imageViewItem)
}
}
}
scrollView.addView(photosContainer)
container.addView(scrollView)
AlertDialog.Builder(this)
.setTitle("Account Photos")
.setView(container)
.setPositiveButton("Close", null)
.show()
}
private fun saveImageToGallery(bitmap: Bitmap) {
val filename = "IMG_${SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(Date())}.jpg"
val values = ContentValues().apply {
put(MediaStore.Images.Media.DISPLAY_NAME, filename)
put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg")
put(MediaStore.Images.Media.RELATIVE_PATH, "Pictures/photoeditor")
}
val uri = contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values)
if (uri != null) {
contentResolver.openOutputStream(uri)?.use { out ->
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out)
}
Toast.makeText(this, "Saved to gallery", Toast.LENGTH_SHORT).show()
}
}
private fun saveEditedPhotoToAccountPerUser(bitmap: Bitmap, username: String) {
try {
val userDir = File(filesDir, username)
if (!userDir.exists()) userDir.mkdirs()
val filename = "edited_${SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(Date())}.jpg"
val file = File(userDir, filename)
FileOutputStream(file).use { out ->
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out)
}
val prefs = getSharedPreferences("user", MODE_PRIVATE)
val photosKey = "${username}_photos"
val existingSet = prefs.getStringSet(photosKey, mutableSetOf()) ?: mutableSetOf()
val list = existingSet.toMutableSet()
list.add(file.absolutePath)
prefs.edit().putStringSet(photosKey, list).apply()
Toast.makeText(this, "Photo saved to your account", Toast.LENGTH_SHORT).show()
} catch (e: Exception) {
Toast.makeText(this, "Error saving photo: ${e.localizedMessage}", Toast.LENGTH_LONG).show()
}
}
private fun shareImage(bitmap: Bitmap) {
val file = File(cacheDir, "share_image.jpg")
FileOutputStream(file).use { out ->
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out)
}
val shareUri = FileProvider.getUriForFile(this, "$packageName.fileprovider", file)
val shareIntent = Intent().apply {
action = Intent.ACTION_SEND
putExtra(Intent.EXTRA_STREAM, shareUri)
type = "image/jpeg"
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
}
startActivity(Intent.createChooser(shareIntent, "Share Image"))
}
private fun hasStoragePermission(): Boolean {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
checkSelfPermission(Manifest.permission.READ_MEDIA_IMAGES) == android.content.pm.PackageManager.PERMISSION_GRANTED
} else {
checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) == android.content.pm.PackageManager.PERMISSION_GRANTED
}
}
private fun requestStoragePermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
requestPermissionLauncher.launch(Manifest.permission.READ_MEDIA_IMAGES)
} else {
requestPermissionLauncher.launch(Manifest.permission.READ_EXTERNAL_STORAGE)
}
}
private fun pickImageFromGallery() {
pickImageLauncher.launch("image/*")
}
private fun rotateBitmap(src: Bitmap, degree: Float): Bitmap {
val matrix = Matrix()
matrix.postRotate(degree)
return Bitmap.createBitmap(src, 0, 0, src.width, src.height, matrix, true)
}
private fun flipBitmap(src: Bitmap): Bitmap {
val matrix = Matrix()
matrix.preScale(-1f, 1f)
return Bitmap.createBitmap(src, 0, 0, src.width, src.height, matrix, true)
}
}