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
use std::collections::HashMap;
use encoding::EncodingRef;
use crate::{
common::BitMatrix, exceptions::Exceptions, BarcodeFormat, EncodeHintType, EncodeHintValue,
Writer,
};
use super::encoder::{encoder, AztecCode};
pub struct AztecWriter;
impl Writer for AztecWriter {
fn encode(
&self,
contents: &str,
format: &crate::BarcodeFormat,
width: i32,
height: i32,
) -> Result<crate::common::BitMatrix, crate::exceptions::Exceptions> {
self.encode_with_hints(contents, format, width, height, &HashMap::new())
}
fn encode_with_hints(
&self,
contents: &str,
format: &crate::BarcodeFormat,
width: i32,
height: i32,
hints: &std::collections::HashMap<crate::EncodeHintType, crate::EncodeHintValue>,
) -> Result<crate::common::BitMatrix, crate::exceptions::Exceptions> {
let mut charset = None; let mut ecc_percent = encoder::DEFAULT_EC_PERCENT;
let mut layers = encoder::DEFAULT_AZTEC_LAYERS;
if hints.contains_key(&EncodeHintType::CHARACTER_SET) {
if let EncodeHintValue::CharacterSet(cset_name) = hints
.get(&EncodeHintType::CHARACTER_SET)
.expect("already knonw presence")
{
if cset_name != "iso-8859-1" {
charset = Some(encoding::label::encoding_from_whatwg_label(cset_name).unwrap());
}
}
}
if hints.contains_key(&EncodeHintType::ERROR_CORRECTION) {
if let EncodeHintValue::ErrorCorrection(ecc_level) = hints
.get(&EncodeHintType::ERROR_CORRECTION)
.expect("key exists")
{
ecc_percent = ecc_level.parse().expect("should convert to int");
}
}
if hints.contains_key(&EncodeHintType::AZTEC_LAYERS) {
if let EncodeHintValue::AztecLayers(az_layers) = hints
.get(&EncodeHintType::AZTEC_LAYERS)
.expect("key exists")
{
layers = *az_layers;
}
}
encode(
contents,
*format,
width as u32,
height as u32,
charset,
ecc_percent,
layers,
)
}
}
fn encode(
contents: &str,
format: BarcodeFormat,
width: u32,
height: u32,
charset: Option<EncodingRef>,
ecc_percent: u32,
layers: i32,
) -> Result<BitMatrix, Exceptions> {
if format != BarcodeFormat::AZTEC {
return Err(Exceptions::IllegalArgumentException(format!(
"Can only encode AZTEC, but got {:?}",
format
)));
}
let aztec = if let Some(cset) = charset {
encoder::encode_with_charset(contents, ecc_percent, layers, cset)?
} else {
encoder::encode(contents, ecc_percent, layers)?
};
renderRXingResult(&aztec, width, height)
}
fn renderRXingResult(code: &AztecCode, width: u32, height: u32) -> Result<BitMatrix, Exceptions> {
let input = code.getMatrix();
let input_width = input.getWidth();
let input_height = input.getHeight();
let output_width = width.max(input_width);
let output_height = height.max(input_height);
let multiple = (output_width / input_width).min(output_height / input_height);
let left_padding = (output_width - (input_width * multiple)) / 2;
let top_padding = (output_height - (input_height * multiple)) / 2;
let mut output = BitMatrix::new(output_width, output_height)?;
let mut input_y = 0;
let mut output_y = top_padding;
while input_y < input_height {
let mut input_x = 0;
let mut output_x = left_padding;
while input_x < input_width {
if input.get(input_x, input_y) {
output.setRegion(output_x, output_y, multiple, multiple)?;
}
input_x += 1;
output_x += multiple;
}
input_y += 1;
output_y += multiple
}
Ok(output)
}