From 6b324b5db50174264ced3fda7a8a59c6b019609d Mon Sep 17 00:00:00 2001 From: Markus Mayer Date: Mon, 28 Nov 2022 22:14:59 +0100 Subject: [PATCH] Update to ZXing 1.4.0 --- Cargo.toml | 2 +- build.rs | 8 ++++++-- c_api/src/CMakeLists.txt | 3 ++- c_api/src/reader.cpp | 23 ++++++++++----------- c_api/src/reader.h | 3 +-- c_api/src/writer.cpp | 3 +-- src/lib.rs | 43 ++++++++++++++++------------------------ submodules/zxing-cpp | 2 +- 8 files changed, 39 insertions(+), 48 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index b43c133..0281761 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ name = "zxing-rs" version = "0.1.1" authors = ["higumachan "] -edition = "2018" +edition = "2021" description = "port from [zxing-cpp](https://github.com/nu-book/zxing-cpp)" repository = "https://github.com/higumachan/zxing-rs" license = "Apache-2.0" diff --git a/build.rs b/build.rs index 786dd1c..9432148 100644 --- a/build.rs +++ b/build.rs @@ -16,16 +16,20 @@ fn main() { cc::Build::new() .cpp(true) - .flag("-std=c++14") + .flag("-std=c++17") .flag("-Wno-missing-braces") + .define("ZX_USE_UTF8", "1") + // .define("NDEBUG", "1") .include(core_src_dir) .files(core_sources) .compile("zxing_core"); cc::Build::new() .cpp(true) - .flag("-std=c++14") + .flag("-std=c++17") .flag("-v") .flag("-g") + .define("ZX_USE_UTF8", "1") + // .define("NDEBUG", "1") .include(c_api_src_dir) .include(core_src_dir) .files(c_api_sources) diff --git a/c_api/src/CMakeLists.txt b/c_api/src/CMakeLists.txt index 971621f..c62f303 100644 --- a/c_api/src/CMakeLists.txt +++ b/c_api/src/CMakeLists.txt @@ -1,2 +1,3 @@ +set(CMAKE_CXX_STANDARD 17) add_library(zxing_c_api reader.cpp writer.cpp) -target_link_libraries(zxing_c_api ZXingCore) \ No newline at end of file +target_link_libraries(zxing_c_api ZXingCore) diff --git a/c_api/src/reader.cpp b/c_api/src/reader.cpp index a426fda..2ecb9ba 100644 --- a/c_api/src/reader.cpp +++ b/c_api/src/reader.cpp @@ -3,32 +3,29 @@ // #include +#include +#include #include "reader.h" -#include -#include +#include "DecodeHints.h" +#include "Result.h" #include "MultiFormatReader.h" -#include "GenericLuminanceSource.h" #include "HybridBinarizer.h" #include "TextUtfEncoding.h" -#include -#include +#include "ReadBarcode.h" using Binarizer = ZXing::HybridBinarizer; int -zxing_read_qrcode(ZXING_RESULT **result, const uint8_t *buffer, int width, int height, int row_bytes, int pixel_bytes, - int index_r, - int index_g, int index_b) +zxing_read_qrcode(ZXING_RESULT **result, const uint8_t *buffer, int width, int height, int row_bytes, int pixel_bytes) { ZXing::DecodeHints hints; hints.setTryHarder(true); + hints.setBinarizer(ZXing::Binarizer::LocalAverage); ZXing::MultiFormatReader reader(hints); - ZXing::GenericLuminanceSource source((int)width, (int)height, buffer, row_bytes, pixel_bytes, index_r, index_g, index_b); - Binarizer binImage(std::shared_ptr(&source, [](void*) {})); - - ZXing::Result reader_result = reader.read(binImage); + ZXing::ImageView image { buffer, width, height, ZXing::ImageFormat::RGB, row_bytes, pixel_bytes }; + ZXing::Result reader_result = ZXing::ReadBarcode(image, hints); *result = (ZXING_RESULT *)malloc(sizeof(ZXING_RESULT)); @@ -41,7 +38,7 @@ zxing_read_qrcode(ZXING_RESULT **result, const uint8_t *buffer, int width, int h } (*result)->format = static_cast(reader_result.format()); - auto s = ZXing::TextUtfEncoding::ToUtf8(reader_result.text()); + auto s = reader_result.text(); (*result)->bytes = (uint8_t *)malloc(sizeof(char) * s.size()); std::memcpy((*result)->bytes, s.data(), sizeof(char) * s.size()); (*result)->bytes_size = sizeof(char) * s.size(); diff --git a/c_api/src/reader.h b/c_api/src/reader.h index 5bfb7f7..b16d153 100644 --- a/c_api/src/reader.h +++ b/c_api/src/reader.h @@ -18,8 +18,7 @@ typedef struct ZxingQrResult { extern "C" { - int zxing_read_qrcode(ZXING_RESULT **result, const uint8_t *buffer, int width, int height, int row_bytes, - int pixel_bytes, int index_r, int index_g, int index_b); + int zxing_read_qrcode(ZXING_RESULT **result, const uint8_t *buffer, int width, int height, int row_bytes, int pixel_bytes); int release_result(ZXING_RESULT* result); } diff --git a/c_api/src/writer.cpp b/c_api/src/writer.cpp index ae5bc35..39540ec 100644 --- a/c_api/src/writer.cpp +++ b/c_api/src/writer.cpp @@ -9,7 +9,6 @@ #include "MultiFormatWriter.h" #include "BitMatrix.h" #include "TextUtfEncoding.h" -#include "ZXStrConvWorkaround.h" int zxing_write_qrcode(const char* text, uint8_t ** buffer, int format, int width, int height, int margin, int ecc_level) { @@ -17,7 +16,7 @@ int zxing_write_qrcode(const char* text, uint8_t ** buffer, int format, int widt writer.setMargin(margin); writer.setEccLevel(ecc_level); - auto matrix = writer.encode(ZXing::TextUtfEncoding::FromUtf8(text), width, height); + auto matrix = writer.encode(text, width, height); int size = matrix.width() * matrix.height(); *buffer = (unsigned char*)std::calloc(matrix.width() * matrix.height(), sizeof(unsigned char)); const unsigned char black = 0; diff --git a/src/lib.rs b/src/lib.rs index ecfb631..9c24902 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -28,32 +28,32 @@ bitflags! { /** Code 128 1D format. */ const CODE_128 = (1 << 4); + /** GS1 DataBar, formerly known as RSS 14. */ + const DATA_BAR = (1 << 5); + + /** GS1 DataBar Expanded, formerly known as RSS EXPANDED. */ + const DATA_BAR_EXPANDED = (1 << 6); + /** Data Matrix 2D barcode format. */ - const DATA_MATRIX = (1 << 5); + const DATA_MATRIX = (1 << 7); /** EAN-8 1D format. */ - const EAN_8 = (1 << 6); + const EAN_8 = (1 << 8); /** EAN-13 1D format. */ - const EAN_13 = (1 << 7); + const EAN_13 = (1 << 9); /** ITF (Interleaved Two of Five) 1D format. */ - const ITF = (1 << 8); + const ITF = (1 << 10); /** MaxiCode 2D barcode format. */ - const MAXICODE = (1 << 9); + const MAXICODE = (1 << 11); /** PDF417 format. */ - const PDF_417 = (1 << 10); + const PDF_417 = (1 << 12); /** QR Code 2D barcode format. */ - const QR_CODE = (1 << 11); - - /** RSS 14 */ - const RSS_14 = (1 << 12); - - /** RSS EXPANDED */ - const RSS_EXPANDED = (1 << 13); + const QR_CODE = (1 << 13); /** UPC-A 1D format. */ const UPC_A = (1 << 14); @@ -61,8 +61,8 @@ bitflags! { /** UPC-E 1D format. */ const UPC_E = (1 << 15); - /** UPC/EAN extension (1D). Not a stand-alone format. */ - const UPC_EAN_EXTENSION = (1 << 16); + /** Micro QR Code format. */ + const MICROQR_CODE = (1 << 16); } } @@ -142,9 +142,6 @@ extern "C" { height: c_int, row_bytes: c_int, pixel_bytes: c_int, - index_r: c_int, - index_g: c_int, - index_b: c_int, ) -> c_int; fn release_result(result: *mut ZxingResult); @@ -171,9 +168,6 @@ pub fn read_qrcode(image: DynamicImage) -> Result { image.height() as crate::c_int, (image.width() * 3) as crate::c_int, 3, - 0, - 1, - 2, ); let result = result.assume_init(); @@ -266,9 +260,6 @@ mod tests { image.height() as crate::c_int, (image.width() * 4) as crate::c_int, 4, - 0, - 1, - 2, ); let s = from_raw_parts((*result).bytes, (*result).bytes_size as usize); let text = from_utf8(s); @@ -292,8 +283,8 @@ mod tests { [ (72i32, 116i32), (228i32, 111i32), - (233i32, 268i32), - (76i32, 273i32) + (233i32, 269i32), + (75i32, 273i32) ] ); } diff --git a/submodules/zxing-cpp b/submodules/zxing-cpp index 29ec2dd..f89db69 160000 --- a/submodules/zxing-cpp +++ b/submodules/zxing-cpp @@ -1 +1 @@ -Subproject commit 29ec2dd4e5c1cfe977530287936acebac1a40f19 +Subproject commit f89db69e34543c508f80e807535dab3012eea7ad