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 2022 gitlost
*/
// SPDX-License-Identifier: Apache-2.0
#include "ThresholdBinarizer.h"
#include "oned/ODReader.h"
#include "gtest/gtest.h"
using namespace ZXing;
// Helper to parse a 0/1 string into a BitMatrix
static BitMatrix ParseBitMatrix(const std::string& str, const int width)
{
const int height = narrow_cast<int>(str.length() / width);
BitMatrix mat(width, height);
for (int y = 0; y < height; ++y) {
size_t offset = y * width;
for (int x = 0; x < width; ++x, offset++) {
if (str[offset] == '1') {
mat.set(x, y);
}
}
}
return mat;
}
// Helper to convert a BitMatrix into a black/white ImageView
static ImageView getImageView(std::vector<uint8_t> &buf, const BitMatrix &bits)
{
buf.resize(bits.width() * bits.height());
for (int r = 0; r < bits.height(); r++) {
const int k = r * bits.width();
for (int c = 0; c < bits.width(); c++) {
buf[k + c] = bits.get(c, r) ? 0x00 : 0xFF;
}
}
return ImageView(buf.data(), bits.width(), bits.height(), ImageFormat::Lum);
}
TEST(ThresholdBinarizerTest, PatternRowClear)
{
std::string bitstream;
BitMatrix bits;
ReaderOptions opts;
std::vector<uint8_t> buf;
// Test that ThresholdBinarizer::getPatternRow() clears row first (same as GlobalHistogramBinarizer)
// Following was failing due to OneD::DoDecode() accumulating bars in loop when using ThresholdBinarizer
// DataBarExpanded Stacked with 11 rows (11 + 10 * 3 separators), 1 column (2 data segments) wide
bitstream = "01010101111011111110111111110000101100100000010100010"
"00001010000100000001000000001010010011011111101010000"
"00000101010101010101010101010101010101010101010100000"
"00001000001110100010100001010101001110000101100110000"
"10100111110001011101011110000000010001111010011000101"
"00001000001110100010100001010101001110000101100110000"
"00000101010101010101010101010101010101010101010100000"
"00000001110100100001010000001010010101100111000110000"
"01011110001011011110001111110000101010011000111000010"
"00000001110100100001010000001010010101100111000110000"
"00000101010101010101010101010101010101010101010100000"
"00000110001111101110100001010100001110001110111010000"
"10101001110000010001011110000001110001110001000100101"
"00000110001111101110100001010100001110001110111010000"
"00000101010101010101010101010101010101010101010100000"
"00001000011011000101010000101010010011100010001100000"
"01000111100100111010001111000000101100011101110010010"
"00001000011011000101010000101010010011100010001100000"
"00000101010101010101010101010101010101010101010100000"
"00001000110001110110100000000100001011110100001000000"
"10100111001110001001011111111001110100001011110111101"
"00001000110001110110100000000100001011110100001000000"
"00000101010101010101010101010101010101010101010100000"
"00000010011101111101010010101010010100110000100000000"
"01011101100010000010001100000000101011001111011110010"
"00000010011101111101010010101010010100110000100000000"
"00000101010101010101010101010101010101010101010100000"
"00000100010111101110100000101010001100000110011010000"
"10111011101000010001011111000000110011111001100101101"
"00000100010111101110100000101010001100000110011010000"
"00000101010101010101010101010101010101010101010100000"
"00000011000110001001000000010101010000011110011010000"
"01011100111001110110011111100000101111100001100101010"
"00000011000110001001000000010101010000011110011010000"
"00000101010101010101010101010101010101010101010100000"
"00001011100010001110100000000010001011111010001100000"
"10100100011101110001011111111100110100000101110011101"
"00001011100010001110100000000010001011111010001100000"
"00000101010101010101010101010101010101010101010100000"
"00001000111010000101000101010101010100001011000110000"
"01000111000101111010011000000000101011110100111000010";
bits = ParseBitMatrix(bitstream, 53 /*width*/);
opts.setFormats(BarcodeFormat::DataBarExpanded);
opts.setMinLineCount(1);
OneD::Reader reader(opts);
auto barcode = reader.decode(ThresholdBinarizer(getImageView(buf, bits), 0x7F));
EXPECT_TRUE(barcode.isValid());
EXPECT_EQ(barcode.text(TextMode::HRI), "(91)12345678901234567890123456789012345678901234567890123456789012345678");
}
|