Skip to main content

qr_code_styling/config/
image_options.rs

1//! Image/logo embedding options.
2
3/// Options for embedding an image/logo in the QR code.
4#[derive(Debug, Clone, PartialEq)]
5#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
6pub struct ImageOptions {
7    /// Size of the image relative to the QR code (0.0 to 1.0).
8    pub image_size: f64,
9    /// Whether to hide dots behind the image.
10    pub hide_background_dots: bool,
11    /// Margin around the image in modules.
12    pub margin: u32,
13    /// Cross-origin setting for loading images (browser context).
14    pub cross_origin: Option<String>,
15    /// Whether to save the image as a data URL in SVG.
16    pub save_as_blob: bool,
17}
18
19impl Default for ImageOptions {
20    fn default() -> Self {
21        Self {
22            image_size: 0.4,
23            hide_background_dots: true,
24            margin: 0,
25            cross_origin: None,
26            save_as_blob: true,
27        }
28    }
29}
30
31impl ImageOptions {
32    /// Create new image options.
33    pub fn new() -> Self {
34        Self::default()
35    }
36
37    /// Set the image size ratio.
38    pub fn with_image_size(mut self, size: f64) -> Self {
39        self.image_size = size.clamp(0.0, 1.0);
40        self
41    }
42
43    /// Set whether to hide background dots.
44    pub fn with_hide_background_dots(mut self, hide: bool) -> Self {
45        self.hide_background_dots = hide;
46        self
47    }
48
49    /// Set the margin around the image.
50    pub fn with_margin(mut self, margin: u32) -> Self {
51        self.margin = margin;
52        self
53    }
54
55    /// Set cross-origin setting.
56    pub fn with_cross_origin(mut self, cross_origin: impl Into<String>) -> Self {
57        self.cross_origin = Some(cross_origin.into());
58        self
59    }
60
61    /// Set whether to save as blob/data URL.
62    pub fn with_save_as_blob(mut self, save: bool) -> Self {
63        self.save_as_blob = save;
64        self
65    }
66}