-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.rs
More file actions
275 lines (265 loc) · 10.6 KB
/
Copy pathlib.rs
File metadata and controls
275 lines (265 loc) · 10.6 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
use std::sync::Arc;
use arkit::entry;
use arkit::prelude::*;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum DemoMode {
Photo,
Scan,
}
#[entry]
fn app() -> Element {
let controller = use_hook(CameraController::new);
let mut demo_mode = use_signal(|| DemoMode::Photo);
let mut position = use_signal(|| CameraPosition::Back);
let mut active = use_signal(|| true);
let mut status = use_signal(CameraStatus::default);
let mut result_text = use_signal(|| String::from("相机准备中"));
let mut error_text = use_signal(String::new);
let mut capture_count = use_signal(|| 0_u32);
let mut scan_count = use_signal(|| 0_u32);
let mode = match demo_mode() {
DemoMode::Photo => CameraMode::Photo(CameraPhotoModeConfiguration {
toolbar: CameraPhotoToolbarConfiguration {
show_flash: true,
show_zoom: true,
show_preview_resolution: true,
show_photo_resolution: true,
show_camera_switch: true,
show_shutter: true,
show_mode_label: true,
mode_label: Arc::from("照片"),
panel_color: 0xFF000000,
control_background_color: 0xFF3A3A3C,
accent_color: 0xFFFF2D55,
shutter_color: 0xFFFFFFFF,
zoom_track_color: 0x99FFFFFF,
zoom_thumb_color: 0xFFFF2D55,
control_size: 46.0,
shutter_size: 76.0,
zoom_slider_width: 220.0,
top_bar_height: 58.0,
bottom_bar_height: 168.0,
top_inset: 0.0,
..CameraPhotoToolbarConfiguration::default()
},
interactions: CameraPhotoPreviewInteractions {
tap_to_focus: true,
meter_exposure_on_tap: true,
double_tap_to_switch_camera: true,
zoom_step: 0.1,
..CameraPhotoPreviewInteractions::default()
},
..CameraPhotoModeConfiguration::default()
}),
DemoMode::Scan => CameraMode::Scan(CameraScanModeConfiguration {
scanner: CameraScanConfiguration {
formats: Arc::from([
CameraScanFormat::QrCode,
CameraScanFormat::DataMatrix,
CameraScanFormat::Code128,
CameraScanFormat::Ean13,
]),
continuous: true,
max_frames_per_second: 10,
..CameraScanConfiguration::default()
},
toolbar: CameraScanToolbarConfiguration {
show_torch: true,
show_zoom: true,
show_camera_switch: true,
show_reticle: true,
show_reticle_scan_line: true,
show_hint: true,
show_footer: true,
hint: Arc::from("扫码模式 · 对准二维码、条码或 Data Matrix"),
footer: Arc::from("自动识别 · 无需按快门"),
torch_on_label: Arc::from("轻触关闭"),
torch_off_label: Arc::from("轻触照亮"),
panel_color: 0xB3000000,
control_background_color: 0xB33A3A3C,
accent_color: 0xFF22C55E,
zoom_track_color: 0x99FFFFFF,
zoom_thumb_color: 0xFF22C55E,
control_size: 46.0,
reticle_size: 248.0,
reticle_stroke_width: 3.0,
reticle_corner_radius: 8.0,
reticle_corner_length: 34.0,
reticle_scan_line_color: 0xCC22C55E,
reticle_scan_line_height: 2.0,
reticle_scan_line_inset: 18.0,
reticle_scan_duration: std::time::Duration::from_millis(1_800),
zoom_slider_width: 220.0,
top_bar_height: 58.0,
bottom_bar_height: 86.0,
top_inset: 0.0,
..CameraScanToolbarConfiguration::default()
},
interactions: CameraScanPreviewInteractions {
tap_to_focus: true,
meter_exposure_on_tap: false,
double_tap_to_switch_camera: false,
zoom_step: 0.1,
..CameraScanPreviewInteractions::default()
},
..CameraScanModeConfiguration::default()
}),
};
let status_label = status_text(&status());
let pause_label = if active() { "暂停" } else { "继续" };
rsx! {
column {
width: "100%",
height: "100%",
background_color: "#FF020617",
padding_top: 48.0,
column {
width: "100%",
height: if error_text().is_empty() { 88.0 } else { 108.0 },
padding_top: 6.0,
padding_right: 10.0,
padding_bottom: 6.0,
padding_left: 10.0,
background_color: "#FF000000",
row {
width: "100%",
height: 40.0,
align_items: "center",
button {
width: 68.0,
height: 34.0,
border_radius: 17.0,
background_color: if demo_mode() == DemoMode::Photo {
0xFFFF2D55u32
} else {
0xFF2C2C2Eu32
},
onclick: move |_| {
demo_mode.set(DemoMode::Photo);
result_text.set("拍照模式 · 单击对焦,双击切换镜头".into());
},
"相机"
}
button {
margin_left: 6.0,
width: 68.0,
height: 34.0,
border_radius: 17.0,
background_color: if demo_mode() == DemoMode::Scan {
0xFF16A34Au32
} else {
0xFF2C2C2Eu32
},
onclick: move |_| {
demo_mode.set(DemoMode::Scan);
result_text.set("扫码模式 · 自动识别,无需快门".into());
},
"扫码"
}
row { layout_weight: 1.0, hit_test_behavior: "transparent" }
button {
width: 68.0,
height: 34.0,
padding: 0.0,
border_radius: 17.0,
background_color: "#FF2C2C2E",
font_size: 13.0,
onclick: move |_| active.toggle(),
"{pause_label}"
}
}
row {
width: "100%",
height: 32.0,
align_items: "center",
text {
font_size: 11.0,
font_color: "#FFD1D1D6",
max_lines: 1,
"{status_label}"
}
row { width: 8.0, hit_test_behavior: "transparent" }
row {
layout_weight: 1.0,
text {
font_size: 11.0,
font_color: "#FF8E8E93",
max_lines: 1,
"{result_text}"
}
}
}
if !error_text().is_empty() {
text {
height: 20.0,
font_size: 11.0,
font_color: "#FFFF453A",
max_lines: 1,
"{error_text}"
}
}
}
column {
width: "100%",
layout_weight: 1.0,
CameraView {
controller: Some(controller.clone()),
mode,
position: position(),
active: active(),
width: "100%",
height: "100%",
on_position_change: move |next| position.set(next),
on_status_change: move |next| status.set(next),
on_photo: move |photo: CapturedPhoto| {
capture_count += 1;
result_text.set(format!(
"第 {} 张 · {}×{} · {:.1} MB · JPEG",
capture_count(),
photo.size.width,
photo.size.height,
photo.bytes().len() as f64 / 1_048_576.0,
));
error_text.set(String::new());
},
on_scan: move |result: CameraScanResult| {
scan_count += 1;
result_text.set(format!(
"第 {} 次 · {} · {}",
scan_count(),
result.format,
result.text,
));
error_text.set(String::new());
},
on_error: move |error: CameraError| error_text.set(error.to_string()),
}
}
}
}
}
fn status_text(status: &CameraStatus) -> String {
match status {
CameraStatus::Idle => "空闲".into(),
CameraStatus::WaitingForSurface => "等待预览 Surface".into(),
CameraStatus::Starting(_) => "正在启动 CameraKit".into(),
CameraStatus::Running(info) => format!(
"预览 {}×{} · 拍照 {} · 分析 {}",
info.preview_size.width,
info.preview_size.height,
info.photo_size.map_or_else(
|| "关闭".into(),
|size| format!("{}×{}", size.width, size.height)
),
info.frame_size.map_or_else(
|| "关闭".into(),
|size| format!("{}×{}", size.width, size.height)
),
),
CameraStatus::Capturing(_) => "正在拍照".into(),
CameraStatus::Stopped => "已暂停".into(),
CameraStatus::PermissionDenied => "相机权限未授权".into(),
CameraStatus::Unavailable => "当前设备没有可用相机".into(),
CameraStatus::Error(error) => format!("错误:{error}"),
}
}