I built an component for barcode scanning:
#[component]
pub fn BarCodeReader(
mut props: BarcodeReaderProps,
) -> Element {
let mut stream_started = use_signal(|| false);
// tofix:
// If torch_status is set to TorchStatus::Deactivated and is switched to TorchStatus::Disabled because of check_for_torch_capability(), the torch can't be activated
// If torch_status is set to TorchStatus::Disabled and is switched to TorchStatus::Disabled because of check_for_torch_capability(), the torch can be activated
let mut torch_status: Signal<TorchStatus> = use_signal(|| TorchStatus::Deactivated);
rsx!{
div {
CamStream {
width: props.camstream_x,
height: props.camstream_y,
poster: props.camstream_placeholder,
}
if props.show_scan_button {
// dioxus button component
div {
if !stream_started() {
Button {
variant: ButtonVariant::Outline,
onclick: move |_| {
let formats = props.barcode_formats.clone();
async move {
match start_cam_stream().await {
Ok(_) => {
debug!("Camera stream started");
stream_started.set(true);
match check_for_torch_capability().await {
Ok(capability) => {
debug!("Torch capability: {:?}", capability);
if capability {
torch_status.set(TorchStatus::Disabled);
debug!("Torch status: {:?}", torch_status);
}
}
Err(e) => {
error!("Failed getting torch capability: {}", e);
}
}
match scan(formats).await {
Ok(value) => {
debug!("scanned value is {value:?}");
props.content.set(value);
match stop_cam_stream().await {
Ok(_) => {
stream_started.set(false);
torch_status.set(TorchStatus::Deactivated);
debug!("Camera stream stopped");
}
Err(e) => error!("Failed to stop camera: {}", e),
};
}
Err(e) => {
error!("Failed to scan barcode: {}", e);
}
}
}
Err(e) => error!("Failed to start camera: {}", e),
};
}
},
"Start Scan"
...
TorchButton {torch_status}