1 /*
2 * Copyright 2017 Huy Cuong Nguyen
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "MultiFormatWriter.h"
18
19 #include "BitMatrix.h"
20 #include "aztec/AZWriter.h"
21 #include "datamatrix/DMWriter.h"
22 #include "oned/ODCodabarWriter.h"
23 #include "oned/ODCode128Writer.h"
24 #include "oned/ODCode39Writer.h"
25 #include "oned/ODCode93Writer.h"
26 #include "oned/ODEAN13Writer.h"
27 #include "oned/ODEAN8Writer.h"
28 #include "oned/ODITFWriter.h"
29 #include "oned/ODUPCAWriter.h"
30 #include "oned/ODUPCEWriter.h"
31 #include "pdf417/PDFWriter.h"
32 #include "qrcode/QRErrorCorrectionLevel.h"
33 #include "qrcode/QRWriter.h"
34
35 #include <stdexcept>
36
37 namespace ZXing {
38
39 BitMatrix
encode(const std::wstring & contents,int width,int height) const40 MultiFormatWriter::encode(const std::wstring& contents, int width, int height) const
41 {
42 auto exec0 = [&](auto&& writer) {
43 if (_margin >=0)
44 writer.setMargin(_margin);
45 return writer.encode(contents, width, height);
46 };
47
48 auto AztecEccLevel = [&](Aztec::Writer& writer, int eccLevel) { writer.setEccPercent(eccLevel * 100 / 8); };
49 auto Pdf417EccLevel = [&](Pdf417::Writer& writer, int eccLevel) { writer.setErrorCorrectionLevel(eccLevel); };
50 auto QRCodeEccLevel = [&](QRCode::Writer& writer, int eccLevel) {
51 writer.setErrorCorrectionLevel(static_cast<QRCode::ErrorCorrectionLevel>(--eccLevel / 2));
52 };
53
54 auto exec1 = [&](auto&& writer, auto setEccLevel) {
55 if (_encoding != CharacterSet::Unknown)
56 writer.setEncoding(_encoding);
57 if (_eccLevel >= 0 && _eccLevel <= 8)
58 setEccLevel(writer, _eccLevel);
59 return exec0(std::move(writer));
60 };
61
62 switch (_format) {
63 case BarcodeFormat::Aztec: return exec1(Aztec::Writer(), AztecEccLevel);
64 case BarcodeFormat::DataMatrix: return exec0(DataMatrix::Writer());
65 case BarcodeFormat::PDF417: return exec1(Pdf417::Writer(), Pdf417EccLevel);
66 case BarcodeFormat::QRCode: return exec1(QRCode::Writer(), QRCodeEccLevel);
67 case BarcodeFormat::Codabar: return exec0(OneD::CodabarWriter());
68 case BarcodeFormat::Code39: return exec0(OneD::Code39Writer());
69 case BarcodeFormat::Code93: return exec0(OneD::Code93Writer());
70 case BarcodeFormat::Code128: return exec0(OneD::Code128Writer());
71 case BarcodeFormat::EAN8: return exec0(OneD::EAN8Writer());
72 case BarcodeFormat::EAN13: return exec0(OneD::EAN13Writer());
73 case BarcodeFormat::ITF: return exec0(OneD::ITFWriter());
74 case BarcodeFormat::UPCA: return exec0(OneD::UPCAWriter());
75 case BarcodeFormat::UPCE: return exec0(OneD::UPCEWriter());
76 default: throw std::invalid_argument(std::string("Unsupported format: ") + ToString(_format));
77 }
78 }
79
80 } // ZXing
81