| 1 | use image::{GrayImage, Luma};
|
| 2 |
|
| 3 | use crate::dsl::{BarcodeSymbology, QrEcc};
|
| 4 |
|
| 5 | #[derive(Debug)]
|
| 6 | pub enum RasterError {
|
| 7 | Encode(String),
|
| 8 | Image(String),
|
| 9 | }
|
| 10 |
|
| 11 | impl std::fmt::Display for RasterError {
|
| 12 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
| 13 | match self {
|
| 14 | Self::Encode(msg) | Self::Image(msg) => write!(f, "{msg}"),
|
| 15 | }
|
| 16 | }
|
| 17 | }
|
| 18 |
|
| 19 | impl std::error::Error for RasterError {}
|
| 20 |
|
| 21 | pub fn render_barcode(
|
| 22 | symbology: BarcodeSymbology,
|
| 23 | data: &str,
|
| 24 | height: u32,
|
| 25 | ) -> Result<GrayImage, RasterError> {
|
| 26 | use barcoders::generators::image::Image as BarcodeImage;
|
| 27 | use barcoders::sym::codabar::Codabar;
|
| 28 | use barcoders::sym::code11::Code11;
|
| 29 | use barcoders::sym::code128::Code128;
|
| 30 | use barcoders::sym::code39::Code39;
|
| 31 | use barcoders::sym::code93::Code93;
|
| 32 | use barcoders::sym::ean13::UPCA;
|
| 33 | use barcoders::sym::ean13::EAN13;
|
| 34 | use barcoders::sym::ean8::EAN8;
|
| 35 | use barcoders::sym::tf::TF;
|
| 36 |
|
| 37 | let encoded = match symbology {
|
| 38 | BarcodeSymbology::Code128 => {
|
| 39 | Code128::new(format!("\u{00C0}{data}"))
|
| 40 | .map_err(|e| RasterError::Encode(e.to_string()))?
|
| 41 | .encode()
|
| 42 | }
|
| 43 | BarcodeSymbology::Code39 => Code39::new(data)
|
| 44 | .map_err(|e| RasterError::Encode(e.to_string()))?
|
| 45 | .encode(),
|
| 46 | BarcodeSymbology::Code93 => Code93::new(data)
|
| 47 | .map_err(|e| RasterError::Encode(e.to_string()))?
|
| 48 | .encode(),
|
| 49 | BarcodeSymbology::Ean13 => EAN13::new(data)
|
| 50 | .map_err(|e| RasterError::Encode(e.to_string()))?
|
| 51 | .encode(),
|
| 52 | BarcodeSymbology::Ean8 => EAN8::new(data)
|
| 53 | .map_err(|e| RasterError::Encode(e.to_string()))?
|
| 54 | .encode(),
|
| 55 | BarcodeSymbology::UpcA => UPCA::new(data)
|
| 56 | .map_err(|e| RasterError::Encode(e.to_string()))?
|
| 57 | .encode(),
|
| 58 | BarcodeSymbology::UpcE => {
|
| 59 | return Err(RasterError::Encode(
|
| 60 | "UPC-E raster is not supported; use native UPC-E".into(),
|
| 61 | ));
|
| 62 | }
|
| 63 | BarcodeSymbology::Itf => TF::interleaved(data)
|
| 64 | .map_err(|e| RasterError::Encode(e.to_string()))?
|
| 65 | .encode(),
|
| 66 | BarcodeSymbology::Codabar => Codabar::new(data)
|
| 67 | .map_err(|e| RasterError::Encode(e.to_string()))?
|
| 68 | .encode(),
|
| 69 | BarcodeSymbology::Code11 => Code11::new(data)
|
| 70 | .map_err(|e| RasterError::Encode(e.to_string()))?
|
| 71 | .encode(),
|
| 72 | };
|
| 73 |
|
| 74 | let png = BarcodeImage::png(height);
|
| 75 | let bytes = png
|
| 76 | .generate(&encoded)
|
| 77 | .map_err(|e| RasterError::Image(e.to_string()))?;
|
| 78 | let img = image::load_from_memory(&bytes).map_err(|e| RasterError::Image(e.to_string()))?;
|
| 79 | Ok(img.to_luma8())
|
| 80 | }
|
| 81 |
|
| 82 | pub fn render_qr(
|
| 83 | data: &str,
|
| 84 | version: Option<u8>,
|
| 85 | ecc: QrEcc,
|
| 86 | module_pixels: u32,
|
| 87 | ) -> Result<GrayImage, RasterError> {
|
| 88 | use qrcode::EcLevel;
|
| 89 | use qrcode::QrCode;
|
| 90 | use qrcode::Version;
|
| 91 |
|
| 92 | let ec = match ecc {
|
| 93 | QrEcc::L => EcLevel::L,
|
| 94 | QrEcc::M => EcLevel::M,
|
| 95 | QrEcc::Q => EcLevel::Q,
|
| 96 | QrEcc::H => EcLevel::H,
|
| 97 | };
|
| 98 |
|
| 99 | let code = if let Some(v) = version {
|
| 100 | let v = v.clamp(1, 40);
|
| 101 | QrCode::with_version(data.as_bytes(), Version::Normal(i16::from(v)), ec)
|
| 102 | .map_err(|e| RasterError::Encode(e.to_string()))?
|
| 103 | } else {
|
| 104 | QrCode::with_error_correction_level(data.as_bytes(), ec)
|
| 105 | .map_err(|e| RasterError::Encode(e.to_string()))?
|
| 106 | };
|
| 107 |
|
| 108 | let width = code.width() as u32;
|
| 109 | let scale = module_pixels.max(1);
|
| 110 | let out_w = width * scale;
|
| 111 | let mut img = GrayImage::new(out_w, out_w);
|
| 112 | for y in 0..width {
|
| 113 | for x in 0..width {
|
| 114 | let dark = code[(x as usize, y as usize)] == qrcode::Color::Dark;
|
| 115 | let color = if dark { 0 } else { 255 };
|
| 116 | for dy in 0..scale {
|
| 117 | for dx in 0..scale {
|
| 118 | img.put_pixel(x * scale + dx, y * scale + dy, Luma([color]));
|
| 119 | }
|
| 120 | }
|
| 121 | }
|
| 122 | }
|
| 123 | Ok(img)
|
| 124 | }
|
| 125 |
|
| 126 | #[cfg(test)]
|
| 127 | mod tests {
|
| 128 | use super::*;
|
| 129 |
|
| 130 | #[test]
|
| 131 | fn renders_code128_raster() {
|
| 132 | let img = render_barcode(BarcodeSymbology::Code128, "TEST123", 60).unwrap();
|
| 133 | assert!(img.width() > 10);
|
| 134 | assert_eq!(img.height(), 60);
|
| 135 | }
|
| 136 |
|
| 137 | #[test]
|
| 138 | fn code128_rejects_url_like_data() {
|
| 139 | let err = render_barcode(BarcodeSymbology::Code128, "https://example.com", 60).unwrap_err();
|
| 140 | assert!(err.to_string().contains("invalid"));
|
| 141 | }
|
| 142 |
|
| 143 | #[test]
|
| 144 | fn renders_qr_raster_with_version() {
|
| 145 | let img = render_qr("https://example.com", Some(5), QrEcc::M, 4).unwrap();
|
| 146 | assert!(img.width() >= 20);
|
| 147 | }
|
| 148 | }
|