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
/*
* Copyright 2010 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
use crate::{common::Result, BarcodeFormat, EncodeHints, Exceptions, Writer};
use super::EAN13Writer;
/**
* This object renders a UPC-A code as a {@link BitMatrix}.
*
* @author qwandor@google.com (Andrew Walbran)
*/
#[derive(Default)]
pub struct UPCAWriter(EAN13Writer);
impl Writer for UPCAWriter {
fn encode(
&self,
contents: &str,
format: &crate::BarcodeFormat,
width: i32,
height: i32,
) -> Result<crate::common::BitMatrix> {
self.encode_with_hints(contents, format, width, height, &EncodeHints::default())
}
fn encode_with_hints(
&self,
contents: &str,
format: &crate::BarcodeFormat,
width: i32,
height: i32,
hints: &crate::EncodeHints,
) -> Result<crate::common::BitMatrix> {
if format != &BarcodeFormat::UPC_A {
return Err(Exceptions::illegal_argument_with(format!(
"Can only encode UPC-A, but got {format:?}"
)));
}
// Transform a UPC-A code into the equivalent EAN-13 code and write it that way
self.0.encode_with_hints(
&format!("0{contents}"),
&BarcodeFormat::EAN_13,
width,
height,
hints,
)
}
}
// private final EAN13Writer subWriter = new EAN13Writer();
/**
* @author qwandor@google.com (Andrew Walbran)
*/
#[cfg(test)]
mod UPCAWriterTestCase {
use crate::{common::bit_matrix_test_case, BarcodeFormat, Writer};
use super::UPCAWriter;
#[test]
fn testEncode() {
let testStr =
"00001010100011011011101100010001011010111101111010101011100101110100100111011001101101100101110010100000";
let result = UPCAWriter::default()
.encode(
"485963095124",
&BarcodeFormat::UPC_A,
testStr.chars().count() as i32,
0,
)
.expect("ok");
assert_eq!(testStr, bit_matrix_test_case::matrix_to_string(&result));
}
#[test]
fn testAddChecksumAndEncode() {
let testStr =
"00001010011001001001101111010100011011000101011110101010001001001000111010011100101100110110110010100000";
let result = UPCAWriter::default()
.encode(
"12345678901",
&BarcodeFormat::UPC_A,
testStr.chars().count() as i32,
0,
)
.expect("ok");
assert_eq!(testStr, bit_matrix_test_case::matrix_to_string(&result));
}
}