Skip to content

ZXing cannot handle special Greek symbol "β" #819

Closed
@Mq-b

Description

// Scale the BitMatrix to the specified size
ZXing::BitMatrix scaleBitMatrix(const ZXing::BitMatrix& inputMatrix, int desiredWidth, int desiredHeight)
{
    int inputWidth = inputMatrix.width();
    int inputHeight = inputMatrix.height();

    double scaleX = static_cast<double>(desiredWidth) / inputWidth;
    double scaleY = static_cast<double>(desiredHeight) / inputHeight;

    ZXing::BitMatrix scaledMatrix(desiredWidth, desiredHeight);

    // Optimize mapping using the original matrix and scaling factors
    for (int y = 0; y < desiredHeight; ++y) {
        int originalY = static_cast<int>(y / scaleY);
        if (originalY >= inputHeight) originalY = inputHeight - 1;

        for (int x = 0; x < desiredWidth; ++x) {
            int originalX = static_cast<int>(x / scaleX);
            if (originalX >= inputWidth) originalX = inputWidth - 1;

            scaledMatrix.set(x, y, inputMatrix.get(originalX, originalY));
        }
    }

    return scaledMatrix;
}

// Generate a barcode image
cv::Mat generateBarcode(const std::string& input, int barcodeWidth, int barcodeHeight)
{
    ZXing::MultiFormatWriter writer(ZXing::BarcodeFormat::PDF417);

    ZXing::BitMatrix bitMatrix = writer.encode(input, barcodeWidth, barcodeHeight);

    // Use the bitMatrix directly if it is already the target size
    if (bitMatrix.width() != barcodeWidth || bitMatrix.height() != barcodeHeight) {
        bitMatrix = scaleBitMatrix(bitMatrix, barcodeWidth, barcodeHeight);
    }

    cv::Mat barcodeImage(bitMatrix.height(), bitMatrix.width(), CV_8UC1, cv::Scalar(255)); // White background

    for (int y = 0; y < bitMatrix.height(); ++y) {
        for (int x = 0; x < bitMatrix.width(); ++x) {
            if (bitMatrix.get(x, y)) {
                barcodeImage.at<uchar>(y, x) = 0; // Black pixel
            }
        }
    }

    return barcodeImage;
}

My environment isn't utf8, and I've tried a lot of things. For example, even if I make sure that the input string is utf8, I can debug it and see that there is no problem, but the barcode data is not right.

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions