-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainActivity.kt
294 lines (260 loc) · 11.3 KB
/
MainActivity.kt
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
package com.example.richardstallmaneye
import android.Manifest
import android.content.Context
import android.content.pm.PackageManager
import android.graphics.*
import android.graphics.SurfaceTexture
import android.hardware.camera2.*
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.provider.MediaStore
import android.util.Log
import android.util.Size
import android.view.Surface
import android.view.TextureView
import android.widget.FrameLayout
import androidx.activity.ComponentActivity
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.compose.setContent
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.foundation.Image
import androidx.compose.foundation.gestures.detectTransformGestures
import androidx.compose.foundation.layout.*
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.graphics.asImageBitmap
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import androidx.compose.ui.viewinterop.AndroidView
import androidx.core.content.ContextCompat
class MainActivity : ComponentActivity() {
private lateinit var cameraManager: CameraManager
private var cameraDevice: CameraDevice? = null
private var textureView: TextureView? = null
private var lensFacing = CameraCharacteristics.LENS_FACING_BACK
private val requestPermissionLauncher = registerForActivityResult(
ActivityResultContracts.RequestPermission()
) { isGranted: Boolean ->
if (isGranted) {
openCamera()
} else {
Log.e("MainActivity", "Camera permission denied")
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
cameraManager = getSystemService(Context.CAMERA_SERVICE) as CameraManager
setContent {
MaterialTheme {
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
CameraPreviewWithOverlay()
}
}
}
}
@Composable
fun CameraPreviewWithOverlay() {
val context = LocalContext.current
var selectedImageUri by remember { mutableStateOf<Uri?>(null) }
var selectedBitmap by remember { mutableStateOf<Bitmap?>(null) }
var offsetX by remember { mutableStateOf(0f) }
var offsetY by remember { mutableStateOf(0f) }
var scale by remember { mutableStateOf(1f) }
var rotation by remember { mutableStateOf(0f) }
var transparency by remember { mutableStateOf(0.5f) }
val imagePicker = rememberLauncherForActivityResult(
contract = ActivityResultContracts.GetContent(),
onResult = { uri: Uri? ->
selectedImageUri = uri
uri?.let {
selectedBitmap = if (Build.VERSION.SDK_INT < 28) {
MediaStore.Images.Media.getBitmap(context.contentResolver, it)
} else {
val source = ImageDecoder.createSource(context.contentResolver, it)
ImageDecoder.decodeBitmap(source)
}
}
}
)
Box(modifier = Modifier.fillMaxSize()) {
AndroidView(
factory = { ctx ->
TextureView(ctx).also { view ->
textureView = view
}
},
modifier = Modifier.fillMaxSize()
)
LaunchedEffect(textureView) {
textureView?.let {
if (ContextCompat.checkSelfPermission(context, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
openCamera()
} else {
requestPermissionLauncher.launch(Manifest.permission.CAMERA)
}
}
}
selectedBitmap?.let { bitmap ->
Image(
bitmap = bitmap.asImageBitmap(),
contentDescription = "Selected Image",
modifier = Modifier
.fillMaxSize()
.offset(offsetX.dp, offsetY.dp)
.graphicsLayer(
scaleX = scale,
scaleY = scale,
rotationZ = rotation
)
.alpha(transparency)
.pointerInput(Unit) {
detectTransformGestures { _, pan, zoom, _ ->
offsetX += pan.x
offsetY += pan.y
scale *= zoom
}
},
contentScale = ContentScale.Fit
)
}
Column(
modifier = Modifier
.align(Alignment.BottomCenter)
.padding(16.dp)
) {
Button(onClick = { imagePicker.launch("image/*") }) {
Text("Select Image")
}
Button(onClick = { switchCamera() }) {
Text("Switch Camera")
}
Slider(
value = transparency,
onValueChange = { transparency = it },
valueRange = 0f..1f,
modifier = Modifier.padding(top = 16.dp)
)
Slider(
value = rotation,
onValueChange = { rotation = it },
valueRange = 0f..360f,
modifier = Modifier.padding(top = 16.dp)
)
}
}
}
private fun switchCamera() {
cameraDevice?.close()
lensFacing = if (lensFacing == CameraCharacteristics.LENS_FACING_BACK) {
CameraCharacteristics.LENS_FACING_FRONT
} else {
CameraCharacteristics.LENS_FACING_BACK
}
openCamera()
}
private fun openCamera() {
try {
val cameraId = cameraManager.cameraIdList.find { id ->
val characteristics = cameraManager.getCameraCharacteristics(id)
characteristics.get(CameraCharacteristics.LENS_FACING) == lensFacing
} ?: throw Exception("Camera not found")
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
cameraManager.openCamera(cameraId, object : CameraDevice.StateCallback() {
override fun onOpened(camera: CameraDevice) {
cameraDevice = camera
createCameraPreviewSession()
}
override fun onDisconnected(camera: CameraDevice) {
cameraDevice?.close()
cameraDevice = null
}
override fun onError(camera: CameraDevice, error: Int) {
Log.e("MainActivity", "Camera error: $error")
cameraDevice?.close()
cameraDevice = null
}
}, null)
}
} catch (e: Exception) {
Log.e("MainActivity", "Failed to open camera", e)
}
}
private fun createCameraPreviewSession() {
try {
val texture = textureView?.surfaceTexture
val characteristics = cameraManager.getCameraCharacteristics(cameraDevice?.id ?: "")
val streamConfigurationMap = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP)
val previewSizes = streamConfigurationMap?.getOutputSizes(SurfaceTexture::class.java)
val displaySize = Point()
windowManager.defaultDisplay.getSize(displaySize)
val screenAspectRatio = displaySize.x.toFloat() / displaySize.y
val optimalSize = chooseOptimalSize(previewSizes, screenAspectRatio)
optimalSize?.let { size ->
texture?.setDefaultBufferSize(size.width, size.height)
configureTransform(size.width, size.height)
}
val surface = texture?.let { Surface(it) }
val previewRequestBuilder = cameraDevice?.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW)
surface?.let { previewRequestBuilder?.addTarget(it) }
surface?.let {
cameraDevice?.createCaptureSession(listOf(it), object : CameraCaptureSession.StateCallback() {
override fun onConfigured(session: CameraCaptureSession) {
try {
previewRequestBuilder?.let { builder ->
session.setRepeatingRequest(builder.build(), null, null)
}
} catch (e: Exception) {
Log.e("MainActivity", "Failed to start camera preview", e)
}
}
override fun onConfigureFailed(session: CameraCaptureSession) {
Log.e("MainActivity", "Failed to configure camera session")
}
}, null)
}
} catch (e: Exception) {
Log.e("MainActivity", "Failed to create camera preview session", e)
}
}
private fun chooseOptimalSize(sizes: Array<Size>?, targetAspectRatio: Float): Size? {
return sizes
?.filter {
val ratio = it.width.toFloat() / it.height
Math.abs(ratio - targetAspectRatio) < 0.1 // Tolerancia de aspecto
}
?.maxByOrNull { it.width * it.height }
?: sizes?.maxByOrNull { it.width * it.height }
}
private fun configureTransform(previewWidth: Int, previewHeight: Int) {
val matrix = Matrix()
val viewRect = RectF(0f, 0f, textureView?.width?.toFloat() ?: 0f, textureView?.height?.toFloat() ?: 0f)
val bufferRect = RectF(0f, 0f, previewHeight.toFloat(), previewWidth.toFloat())
val centerX = viewRect.centerX()
val centerY = viewRect.centerY()
bufferRect.offset(centerX - bufferRect.centerX(), centerY - bufferRect.centerY())
matrix.setRectToRect(viewRect, bufferRect, Matrix.ScaleToFit.FILL)
val scale = Math.max(
viewRect.width() / bufferRect.width(),
viewRect.height() / bufferRect.height()
)
matrix.postScale(scale, scale, centerX, centerY)
if (Surface.ROTATION_90 == windowManager.defaultDisplay.rotation || Surface.ROTATION_270 == windowManager.defaultDisplay.rotation) {
matrix.postRotate(90 * (windowManager.defaultDisplay.rotation - 2).toFloat(), centerX, centerY)
}
textureView?.setTransform(matrix)
}
override fun onDestroy() {
super.onDestroy()
cameraDevice?.close()
}
}