Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ee8b405

Browse files
committed
Add option to change error correction level in MultiFormatWriter
1 parent 46ed7a5 commit ee8b405

File tree

6 files changed

+89
-26
lines changed

6 files changed

+89
-26
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ emmake make
7575

7676
See usage example of exported functions from a demo here: https://nu-book.github.io/zxing-cpp/demo.html.
7777

78+
By default, both encoder and decoder are included. If you don't plan to use either of them, you can disable it to reduce generated code size. To do so, in the line `emconfigure cmake ...` above, pass `-ENABLE_ENCODERS=0` to disable encoders or `-ENABLE_DECODERS=0` to disable decoders.
79+
7880
### For other platforms
7981
Wrappers are provided as convenient way to work with native image format. You still can use the library without a wrapper.
8082

core/src/MultiFormatWriter.cpp

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "datamatrix/DMWriter.h"
2121
#include "pdf417/PDFWriter.h"
2222
#include "qrcode/QRWriter.h"
23+
#include "qrcode/QRErrorCorrectionLevel.h"
2324
#include "oned/ODCodabarWriter.h"
2425
#include "oned/ODCode39Writer.h"
2526
#include "oned/ODCode93Writer.h"
@@ -34,12 +35,14 @@ namespace ZXing {
3435

3536
namespace {
3637

37-
template <typename Writer>
38-
Writer CreateWriter(CharacterSet encoding)
38+
template <typename Writer, typename EccConverter>
39+
Writer CreateWriter(CharacterSet encoding, int eccLevel)
3940
{
4041
Writer writer;
4142
if (encoding != CharacterSet::Unknown)
4243
writer.setEncoding(encoding);
44+
if (eccLevel >= 0 && eccLevel <= 8)
45+
writer.setEccPercent(EccConverter()(eccLevel));
4346
return writer;
4447
}
4548

@@ -52,17 +55,45 @@ namespace {
5255
return writer;
5356
}
5457

55-
template <typename Writer>
56-
Writer CreateWriter(CharacterSet encoding, int margin)
58+
template <typename Writer, typename EccConverter>
59+
Writer CreateWriter(CharacterSet encoding, int margin, int eccLevel)
5760
{
5861
Writer writer;
5962
if (encoding != CharacterSet::Unknown)
6063
writer.setEncoding(encoding);
6164
if (margin >= 0)
6265
writer.setMargin(margin);
66+
if (eccLevel >= 0 && eccLevel <= 8)
67+
writer.setErrorCorrectionLevel(EccConverter()(eccLevel));
6368
return writer;
6469
}
6570

71+
struct AztecEccConverter {
72+
int operator()(int eccLevel) const {
73+
// Aztec supports levels 0 to 100 in percentage
74+
return eccLevel * 100 / 8;
75+
}
76+
};
77+
78+
struct Pdf417EccConverter {
79+
int operator()(int eccLevel) const {
80+
return eccLevel;
81+
}
82+
};
83+
84+
struct QRCodeEccConverter {
85+
QRCode::ErrorCorrectionLevel operator()(int eccPercent) const {
86+
if (eccPercent <= 1)
87+
return QRCode::ErrorCorrectionLevel::Low;
88+
else if (eccPercent <= 4)
89+
return QRCode::ErrorCorrectionLevel::Medium;
90+
else if (eccPercent <= 6)
91+
return QRCode::ErrorCorrectionLevel::Quality;
92+
else
93+
return QRCode::ErrorCorrectionLevel::High;
94+
}
95+
};
96+
6697
} // anonymous
6798

6899
BitMatrix
@@ -71,13 +102,13 @@ MultiFormatWriter::encode(const std::wstring& contents, int width, int height) c
71102
switch (_format)
72103
{
73104
case BarcodeFormat::AZTEC:
74-
return CreateWriter<Aztec::Writer>(_encoding).encode(contents, width, height);
105+
return CreateWriter<Aztec::Writer, AztecEccConverter>(_encoding, _eccLevel).encode(contents, width, height);
75106
case BarcodeFormat::DATA_MATRIX:
76107
return DataMatrix::Writer().encode(contents, width, height);
77108
case BarcodeFormat::PDF_417:
78-
return CreateWriter<Pdf417::Writer>(_encoding, _margin).encode(contents, width, height);
109+
return CreateWriter<Pdf417::Writer, Pdf417EccConverter>(_encoding, _margin, _eccLevel).encode(contents, width, height);
79110
case BarcodeFormat::QR_CODE:
80-
return CreateWriter<QRCode::Writer>(_encoding, _margin).encode(contents, width, height);
111+
return CreateWriter<QRCode::Writer, QRCodeEccConverter>(_encoding, _margin, _eccLevel).encode(contents, width, height);
81112
case BarcodeFormat::CODABAR:
82113
return CreateWriter<OneD::CodabarWriter>(_margin).encode(contents, width, height);
83114
case BarcodeFormat::CODE_39:

core/src/MultiFormatWriter.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ class MultiFormatWriter
4040
return *this;
4141
}
4242

43+
/**
44+
* Used for Aztec, PDF417, and QRCode only, [0-8].
45+
*/
46+
MultiFormatWriter& setEccLevel(int level) {
47+
_eccLevel = level;
48+
return *this;
49+
}
50+
4351
/**
4452
* Used for all 1D formats, PDF417, and QRCode only.
4553
*/
@@ -54,6 +62,7 @@ class MultiFormatWriter
5462
BarcodeFormat _format;
5563
CharacterSet _encoding = CharacterSet::Unknown;
5664
int _margin = -1;
65+
int _eccLevel = -1;
5766
};
5867

5968
} // ZXing

example/generate_png.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,11 @@ using namespace ZXing;
3232

3333
static void PrintUsage(const char* exePath)
3434
{
35-
std::cout << "Usage: " << exePath << " [-size <width>x<height>] [-margin <margin>] [-encoding <encoding>] <format> <text> <output>" << std::endl
35+
std::cout << "Usage: " << exePath << " [-size <width>x<height>] [-margin <margin>] [-encoding <encoding>] [-ecc <level>] <format> <text> <output>" << std::endl
3636
<< " -size Size of generated image" << std::endl
3737
<< " -margin Margin around barcode" << std::endl
3838
<< " -encoding Encoding used to encode input text" << std::endl
39+
<< " -ecc Error correction level, [0-8]"
3940
<< std::endl
4041
<< "Supported formats are:" << std::endl
4142
<< " AZTEC" << std::endl
@@ -84,7 +85,7 @@ static bool ParseSize(std::string str, int* width, int* height)
8485
return false;
8586
}
8687

87-
static bool ParseOptions(int argc, char* argv[], int* width, int* height, int* margin, std::string* format, std::string* text, std::string* filePath)
88+
static bool ParseOptions(int argc, char* argv[], int* width, int* height, int* margin, int* eccLevel, std::string* format, std::string* text, std::string* filePath)
8889
{
8990
int nonOptArgCount = 0;
9091
for (int i = 1; i < argc; ++i) {
@@ -109,6 +110,15 @@ static bool ParseOptions(int argc, char* argv[], int* width, int* height, int* m
109110
return false;
110111
}
111112
}
113+
else if (strcmp(argv[i], "-ecc") == 0) {
114+
if (i + 1 < argc) {
115+
++i;
116+
*eccLevel = std::stoi(argv[i]);
117+
}
118+
else {
119+
return false;
120+
}
121+
}
112122
else if (nonOptArgCount == 0) {
113123
*format = ParseFormat(argv[i]);
114124
if (format->empty()) {
@@ -142,9 +152,10 @@ int main(int argc, char* argv[])
142152

143153
int width = 100, height = 100;
144154
int margin = 10;
155+
int eccLevel = -1;
145156
std::string format, text, filePath;
146157

147-
if (!ParseOptions(argc, argv, &width, &height, &margin, &format, &text, &filePath)) {
158+
if (!ParseOptions(argc, argv, &width, &height, &margin, &eccLevel, &format, &text, &filePath)) {
148159
PrintUsage(argv[0]);
149160
return -1;
150161
}
@@ -157,6 +168,8 @@ int main(int argc, char* argv[])
157168
MultiFormatWriter writer(barcodeFormat);
158169
if (margin >= 0)
159170
writer.setMargin(margin);
171+
if (eccLevel >= 0)
172+
writer.setEccLevel(eccLevel);
160173

161174
auto matrix = writer.encode(TextUtfEncoding::FromUtf8(text), width, height);
162175

wrappers/wasm/BarcodeWriter.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class WriteResult
4646
}
4747
};
4848

49-
WriteResult generateBarcode(std::wstring text, std::string format, std::string encoding, int margin, int width, int height)
49+
WriteResult generateBarcode(std::wstring text, std::string format, std::string encoding, int margin, int width, int height, int eccLevel)
5050
{
5151
using namespace ZXing;
5252
try {
@@ -63,6 +63,10 @@ WriteResult generateBarcode(std::wstring text, std::string format, std::string e
6363
writer.setEncoding(charset);
6464
}
6565

66+
if (eccLevel >= 0 && eccLevel <= 8) {
67+
writer.setEccLevel(eccLevel);
68+
}
69+
6670
auto matrix = writer.encode(text, width, height);
6771

6872
std::vector<unsigned char> buffer(matrix.width() * matrix.height(), '\0');

wrappers/wasm/CMakeLists.txt

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,12 @@ cmake_minimum_required (VERSION 3.1.3)
22

33
project (ZXingWasm)
44

5-
set (ENABLE_ENCODERS ON CACHE INTERNAL "Check to include encoders")
6-
set (ENABLE_DECODERS ON CACHE INTERNAL "Check to include decoders")
5+
set (ENABLE_ENCODERS ON CACHE BOOL "Check to include encoders")
6+
set (ENABLE_DECODERS ON CACHE BOOL "Check to include decoders")
77

88
add_definitions (-DUNICODE -D_UNICODE)
99

10-
if (MSVC)
11-
add_definitions (-D_SCL_SECURE_NO_WARNINGS -D_CRT_SECURE_NO_WARNINGS -D_CRT_NONSTDC_NO_WARNINGS -DNOMINMAX)
12-
13-
set (CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} /Oi /GS-")
14-
set (CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /Oi /GS-")
15-
else()
16-
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra")
17-
set (CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -D_DEBUG")
18-
set (CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -D_DEBUG")
19-
endif()
10+
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra")
2011

2112
include (CheckCXXCompilerFlag)
2213
CHECK_CXX_COMPILER_FLAG ("-std=c++11" COMPILER_SUPPORTS_CXX11)
@@ -27,12 +18,25 @@ endif()
2718

2819
add_subdirectory (${CMAKE_CURRENT_SOURCE_DIR}/../../core ${CMAKE_BINARY_DIR}/ZXingCore)
2920

21+
set (SOURCE_FILES lodepng.cpp)
22+
23+
if (ENABLE_DECODERS)
24+
set (SOURCE_FILES ${SOURCE_FILES}
25+
BarcodeReader.cpp
26+
)
27+
endif()
28+
29+
if (ENABLE_ENCODERS)
30+
set (SOURCE_FILES ${SOURCE_FILES}
31+
BarcodeWriter.cpp
32+
)
33+
endif()
34+
3035
add_executable (zxing
31-
BarcodeReader.cpp
32-
BarcodeWriter.cpp
36+
${SOURCE_FILES}
3337
lodepng.cpp
3438
)
3539

36-
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --bind -s DISABLE_EXCEPTION_CATCHING=0")
40+
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --bind -Oz -s DISABLE_EXCEPTION_CATCHING=0 -s FILESYSTEM=0")
3741

3842
target_link_libraries (zxing ZXingCore)

0 commit comments

Comments
 (0)