qr: fix GIF creation
This commit is contained in:
parent
4c77faff60
commit
69b7fc2fac
4 changed files with 40 additions and 21 deletions
10
Cargo.lock
generated
10
Cargo.lock
generated
|
@ -3775,6 +3775,7 @@ dependencies = [
|
||||||
"nokhwa",
|
"nokhwa",
|
||||||
"openssl-sys",
|
"openssl-sys",
|
||||||
"parking_lot 0.12.3",
|
"parking_lot 0.12.3",
|
||||||
|
"qrcode",
|
||||||
"qrcodegen",
|
"qrcodegen",
|
||||||
"rand 0.8.5",
|
"rand 0.8.5",
|
||||||
"rfd",
|
"rfd",
|
||||||
|
@ -6985,6 +6986,15 @@ version = "1.1.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "5520fbcd7da152a449261c5a533a1c7fad044e9e8aa9528cfec3f464786c7926"
|
checksum = "5520fbcd7da152a449261c5a533a1c7fad044e9e8aa9528cfec3f464786c7926"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "qrcode"
|
||||||
|
version = "0.14.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "23e719ca51966ff9f5a8436edb00d6115b3c606a0bb27c8f8ca74a38ff2b036d"
|
||||||
|
dependencies = [
|
||||||
|
"image 0.25.1",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "qrcodegen"
|
name = "qrcodegen"
|
||||||
version = "1.8.0"
|
version = "1.8.0"
|
||||||
|
|
|
@ -59,6 +59,7 @@ tokio = { version = "1.37.0", features = ["full"] }
|
||||||
image = "0.25.1"
|
image = "0.25.1"
|
||||||
rqrr = "0.7.1"
|
rqrr = "0.7.1"
|
||||||
qrcodegen = "1.8.0"
|
qrcodegen = "1.8.0"
|
||||||
|
qrcode = "0.14.0"
|
||||||
ur = "0.4.1"
|
ur = "0.4.1"
|
||||||
gif = "0.13.1"
|
gif = "0.13.1"
|
||||||
|
|
||||||
|
|
|
@ -345,36 +345,44 @@ impl QrCodeContent {
|
||||||
let mut ur_enc = ur::Encoder::bytes(text.as_bytes(), 100).unwrap();
|
let mut ur_enc = ur::Encoder::bytes(text.as_bytes(), 100).unwrap();
|
||||||
for _ in 0..ur_enc.fragment_count() {
|
for _ in 0..ur_enc.fragment_count() {
|
||||||
let ur = ur_enc.next_part().unwrap();
|
let ur = ur_enc.next_part().unwrap();
|
||||||
if let Ok(qr) = QrCode::encode_text(ur.as_str(), qrcodegen::QrCodeEcc::Low) {
|
if let Ok(qr) = qrcode::QrCode::with_error_correction_level(
|
||||||
qrs.push(qr);
|
ur.as_bytes(),
|
||||||
|
qrcode::EcLevel::L
|
||||||
|
) {
|
||||||
|
// Create an image from QR data.
|
||||||
|
let image = qr.render()
|
||||||
|
.max_dimensions(size as u32, size as u32)
|
||||||
|
.dark_color(image::Rgb([0, 0, 0]))
|
||||||
|
.light_color(image::Rgb([255, 255, 255]))
|
||||||
|
.build();
|
||||||
|
qrs.push(image);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !qrs.is_empty() {
|
||||||
// Generate GIF data.
|
// Generate GIF data.
|
||||||
let color_map = &[0, 0, 0, 0xFF, 0xFF, 0xFF];
|
let color_map = &[0, 0, 0, 0xFF, 0xFF, 0xFF];
|
||||||
let mut gif_enc = gif::Encoder::new(&mut gif,
|
let mut gif_enc = gif::Encoder::new(&mut gif,
|
||||||
size as u16,
|
qrs[0].width() as u16,
|
||||||
size as u16,
|
qrs[0].height() as u16,
|
||||||
color_map).unwrap();
|
color_map).unwrap();
|
||||||
gif_enc.set_repeat(gif::Repeat::Infinite).unwrap();
|
gif_enc.set_repeat(gif::Repeat::Infinite).unwrap();
|
||||||
for qr in qrs {
|
for qr in qrs {
|
||||||
// Create an image from QR data and write it to encoder.
|
let mut frame = gif::Frame::from_rgb(qr.width() as u16,
|
||||||
if let Some(image) = Self::qr_to_image_data(qr, size) {
|
qr.height() as u16,
|
||||||
let mut frame = gif::Frame::from_indexed_pixels(size as u16,
|
qr.as_raw().as_slice());
|
||||||
size as u16,
|
|
||||||
image.as_slice(),
|
|
||||||
None);
|
|
||||||
frame.delay = 10;
|
frame.delay = 10;
|
||||||
// Write an image to GIF encoder.
|
// Write an image to GIF encoder.
|
||||||
if let Ok(_) = gif_enc.write_frame(&frame) {
|
if let Ok(_) = gif_enc.write_frame(&frame) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
// Exit on error.
|
// Exit on error.
|
||||||
let mut w_state = qr_state.write();
|
let mut w_state = qr_state.write();
|
||||||
w_state.gif_creating = false;
|
w_state.gif_creating = false;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
// Setup GIF image data.
|
// Setup GIF image data.
|
||||||
let mut w_state = qr_state.write();
|
let mut w_state = qr_state.write();
|
||||||
if !gif.is_empty() {
|
if !gif.is_empty() {
|
||||||
|
|
|
@ -518,7 +518,7 @@ impl View {
|
||||||
// Draw box content.
|
// Draw box content.
|
||||||
let content_resp = ui.allocate_ui_at_rect(rect, |ui| {
|
let content_resp = ui.allocate_ui_at_rect(rect, |ui| {
|
||||||
ui.vertical_centered_justified(|ui| {
|
ui.vertical_centered_justified(|ui| {
|
||||||
ui.add_space(3.0);
|
ui.add_space(4.0);
|
||||||
ui.scope(|ui| {
|
ui.scope(|ui| {
|
||||||
// Correct vertical spacing between items.
|
// Correct vertical spacing between items.
|
||||||
ui.style_mut().spacing.item_spacing.y = -3.0;
|
ui.style_mut().spacing.item_spacing.y = -3.0;
|
||||||
|
|
Loading…
Reference in a new issue