-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStoneCameraApp.kt
More file actions
289 lines (264 loc) · 11.9 KB
/
StoneCameraApp.kt
File metadata and controls
289 lines (264 loc) · 11.9 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
// StoneCameraApp.kt
package co.stonephone.stonecamera
import android.annotation.SuppressLint
import android.graphics.Rect
import android.util.Log
import androidx.annotation.OptIn
import androidx.camera.camera2.interop.ExperimentalCamera2Interop
import androidx.camera.lifecycle.ProcessCameraProvider
import androidx.compose.foundation.background
import androidx.compose.foundation.border
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.WindowInsets
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.systemBars
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.windowInsetsPadding
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.FlipCameraAndroid
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.lifecycle.compose.LocalLifecycleOwner
import androidx.lifecycle.viewmodel.compose.viewModel
import co.stonephone.stonecamera.plugins.AspectRatioPlugin
import co.stonephone.stonecamera.plugins.FlashPlugin
import co.stonephone.stonecamera.plugins.FocusBasePlugin
import co.stonephone.stonecamera.plugins.PinchToZoomPlugin
import co.stonephone.stonecamera.plugins.PortraitModePlugin
import co.stonephone.stonecamera.plugins.QRScannerPlugin
import co.stonephone.stonecamera.plugins.SettingLocation
import co.stonephone.stonecamera.plugins.ShutterFlashPlugin
import co.stonephone.stonecamera.plugins.TapToFocusPlugin
import co.stonephone.stonecamera.plugins.VolumeControlsPlugin
import co.stonephone.stonecamera.plugins.ZoomBarPlugin
import co.stonephone.stonecamera.plugins.ZoomBasePlugin
import co.stonephone.stonecamera.ui.RenderPluginSetting
import co.stonephone.stonecamera.ui.StoneCameraPreview
import co.stonephone.stonecamera.utils.calculateImageCoverageRegion
import co.stonephone.stonecamera.utils.getAllCamerasInfo
val shootModes = arrayOf("Photo", "Video")
// Order here is important, they are loaded and initialised in the order they are listed
// ZoomBar depends on ZoomBase, etc.
val PLUGINS = listOf(
PortraitModePlugin(),
QRScannerPlugin(),
ZoomBasePlugin(),
ZoomBarPlugin(),
FocusBasePlugin(),
TapToFocusPlugin(),
PinchToZoomPlugin(),
FlashPlugin(),
AspectRatioPlugin(),
ShutterFlashPlugin(),
VolumeControlsPlugin(),
// DebugPlugin()
)
@OptIn(ExperimentalCamera2Interop::class)
@SuppressLint("ClickableViewAccessibility")
@Composable
fun StoneCameraApp(
cameraProvider: ProcessCameraProvider,
) {
val context = LocalContext.current
val lifecycleOwner = LocalLifecycleOwner.current
val stoneCameraViewModel = viewModel<StoneCameraViewModel>(
factory = StoneCameraViewModelFactory(context, lifecycleOwner, PLUGINS)
)
val isRecording = stoneCameraViewModel.isRecording
val selectedMode = stoneCameraViewModel.selectedMode
val plugins by remember { stoneCameraViewModel::plugins }
val previewView = stoneCameraViewModel.previewView
val imageCapture = stoneCameraViewModel.imageCapture
var viewfinderDimensions by remember { mutableStateOf<Rect?>(null) }
LaunchedEffect(cameraProvider, lifecycleOwner) {
stoneCameraViewModel.onCameraProvider(cameraProvider)
stoneCameraViewModel.onLifecycleOwner(lifecycleOwner)
}
LaunchedEffect(previewView, imageCapture) {
if (previewView != null && imageCapture != null) {
viewfinderDimensions = calculateImageCoverageRegion(
previewView, imageCapture
)
}
}
// We can load cameras once (or whenever context changes) and pass them to the ViewModel
LaunchedEffect(Unit) {
val allCameras = getAllCamerasInfo(context)
stoneCameraViewModel.loadCameras(allCameras)
}
Box(modifier = Modifier.fillMaxSize()) {
// Camera preview behind everything
StoneCameraPreview(
onPreviewView = { pView -> stoneCameraViewModel.onPreviewView(pView) },
)
Row(
modifier = Modifier
.fillMaxWidth()
.align(Alignment.TopCenter)
.windowInsetsPadding(WindowInsets.systemBars),
horizontalArrangement = Arrangement.SpaceBetween,
verticalAlignment = Alignment.CenterVertically
) {
stoneCameraViewModel.pluginSettings.filter { it.renderLocation == SettingLocation.TOP }
.map { setting ->
RenderPluginSetting(
setting, stoneCameraViewModel, modifier = Modifier.padding(4.dp)
)
}
}
viewfinderDimensions?.let {
Box(
modifier = Modifier
.width(it.width().dp)
.height(it.height().dp)
.offset(x = it.left.toFloat().dp, y = it.top.toFloat().dp)
) {
plugins.map {
it.renderViewfinder(stoneCameraViewModel, it)
}
}
}
// Bottom controls
Column(
modifier = Modifier
.fillMaxWidth()
.align(Alignment.BottomCenter),
verticalArrangement = Arrangement.SpaceBetween
) {
plugins.map {
it.renderTray(stoneCameraViewModel, it)
}
// Translucent overlay for mode switch & shutter
Box(
modifier = Modifier
.fillMaxWidth()
.background(Color.Black.copy(alpha = 0.5f))
.padding(16.dp), contentAlignment = Alignment.BottomCenter
) {
Column(
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Bottom
) {
// Mode selector
Row(
modifier = Modifier
.fillMaxWidth()
.padding(bottom = 8.dp),
horizontalArrangement = Arrangement.SpaceEvenly
) {
shootModes.forEach { mode ->
Text(text = mode.uppercase(),
color = if (mode == selectedMode) Color(0xFFFFCC00) else Color.White,
style = MaterialTheme.typography.bodyLarge,
fontWeight = FontWeight.Bold,
modifier = Modifier.clickable {
stoneCameraViewModel.selectMode(mode)
})
}
}
// Bottom row (Flip + Shutter)
Row(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.SpaceEvenly,
verticalAlignment = Alignment.CenterVertically
) {
// Camera Switcher Button
Box(
modifier = Modifier
.size(48.dp)
.background(Color.Transparent, shape = CircleShape)
.clickable {
stoneCameraViewModel.toggleCameraFacing()
}, contentAlignment = Alignment.Center
) {
}
// Shutter button
Box(
modifier = Modifier
.size(60.dp)
.border(1.dp, Color.White, CircleShape)
.padding(4.dp),
contentAlignment = Alignment.Center
) {
Box(
modifier = if (isRecording) {
Modifier
.background(Color.Red)
.fillMaxSize(0.5f)
} else {
Modifier
.fillMaxSize()
.background(
if (selectedMode == "Video") Color(0xFFFF3B30) else Color.White,
shape = CircleShape
)
}.clickable {
when (selectedMode) {
"Photo" -> {
// Then capture the photo
stoneCameraViewModel.capturePhoto()
}
"Video" -> {
if (!isRecording) {
// Start recording
stoneCameraViewModel.startRecording(
stoneCameraViewModel.videoCapture
) { uri ->
Log.d(
"StoneCameraApp", "Video saved to: $uri"
)
}
} else {
// Stop recording
stoneCameraViewModel.stopRecording()
}
}
}
}, contentAlignment = Alignment.Center
) {}
}
// Camera Switcher Button
IconButton(
onClick = { stoneCameraViewModel.toggleCameraFacing() },
modifier = Modifier
.size(48.dp)
.padding(8.dp)
.background(Color.White.copy(alpha = 0.1f), shape = CircleShape)
) {
Icon(
imageVector = Icons.Filled.FlipCameraAndroid, // Use FlipCameraAndroid if preferred
contentDescription = "Flip Camera",
tint = Color.White, // Customize the color if needed
modifier = Modifier.fillMaxSize()
)
}
}
}
}
}
}
}