Skip to content
This repository has been archived by the owner on Jun 24, 2024. It is now read-only.

Update to ZXing 1.4.0 #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "zxing-rs"
version = "0.1.1"
authors = ["higumachan <[email protected]>"]
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"
Expand Down
8 changes: 6 additions & 2 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion c_api/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
target_link_libraries(zxing_c_api ZXingCore)
23 changes: 10 additions & 13 deletions c_api/src/reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,29 @@
//

#include <cstdlib>
#include <cstring>
#include <iostream>
#include "reader.h"
#include <DecodeHints.h>
#include <Result.h>
#include "DecodeHints.h"
#include "Result.h"
#include "MultiFormatReader.h"
#include "GenericLuminanceSource.h"
#include "HybridBinarizer.h"
#include "TextUtfEncoding.h"
#include <cstring>
#include <iostream>
#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<ZXing::LuminanceSource>(&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));
Expand All @@ -41,7 +38,7 @@ zxing_read_qrcode(ZXING_RESULT **result, const uint8_t *buffer, int width, int h
}

(*result)->format = static_cast<int>(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();
Expand Down
3 changes: 1 addition & 2 deletions c_api/src/reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
3 changes: 1 addition & 2 deletions c_api/src/writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@
#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)
{
ZXing::MultiFormatWriter writer(static_cast<ZXing::BarcodeFormat>(format));
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;
Expand Down
43 changes: 17 additions & 26 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,41 +28,41 @@ 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);

/** 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);
}
}

Expand Down Expand Up @@ -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);

Expand All @@ -171,9 +168,6 @@ pub fn read_qrcode(image: DynamicImage) -> Result<DecodedQrCode, DecodeError> {
image.height() as crate::c_int,
(image.width() * 3) as crate::c_int,
3,
0,
1,
2,
);
let result = result.assume_init();

Expand Down Expand Up @@ -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);
Expand All @@ -292,8 +283,8 @@ mod tests {
[
(72i32, 116i32),
(228i32, 111i32),
(233i32, 268i32),
(76i32, 273i32)
(233i32, 269i32),
(75i32, 273i32)
]
);
}
Expand Down
2 changes: 1 addition & 1 deletion submodules/zxing-cpp