glassechidna / zxing-cpp

ZXing C++ Library
Apache License 2.0
597 stars 435 forks source link

cannot decode EAN8, code93, UPC-E !!!??? #87

Open smiler96 opened 5 years ago

smiler96 commented 5 years ago

i used the zxing c++ version to decode the following images, but it can work for the images about EAN8, UPC-E, but i cropped the barcode ROI for the image to decode and it doesn't work, what's the problem? i am a freshman in this and i hope i can get some help from you! Thank s a lot for your help! The images and their ROIs are listed in the following, and my code also:

EAN8 image: EAN8 and its ROI: ROI_EAN8

UPC-E image: upca and its ROI: ROI_upca

#include "zxing_decode.h"

const char* my_code_type[] = {
    "NONE",
    "AZTEC",
    "CODABAR",
    "CODE_39",
    "CODE_93",
    "CODE_128",
    "DATA_MATRIX",
    "EAN_8",
    "EAN_13",
    "ITF"
    "MAXICODE",
    "PDF_417",
    "QR_CODE",
    "RSS_14",
    "RSS_EXPANDED",
    "UPC_A",
    "UPC_E",
    "UPC_EAN_EXTENSION"
};

bool decode_by_zxing(cv::Mat& matGray, std::string& code, std::string& code_type)
{
    zxing::Ref<zxing::LuminanceSource> source(new MatSource(matGray));
    int width = source->getWidth();
    int height = source->getHeight();
    fprintf(stderr, "image width: %d, height: %d\n", width, height);

    //zxing::Ref<zxing::Reader> reader;
    //reader.reset(new zxing::oned::EAN13Reader);
    //reader.reset(new zxing::oned::Code128Reader);
    zxing::oned::MultiFormatOneDReader reader(zxing::DecodeHints::DEFAULT_HINT);

    zxing::Ref<zxing::Binarizer> binarizer(new zxing::HybridBinarizer(source));
    //zxing::Ref<zxing::Binarizer> binarizer(new zxing::);
    zxing::Ref<zxing::BinaryBitmap> bitmap(new zxing::BinaryBitmap(binarizer));
    try
    {
        zxing::Ref<zxing::Result> result(reader.decode(bitmap, zxing::DecodeHints(zxing::DecodeHints::DEFAULT_HINT)));
        //zxing::Ref<zxing::Result> result(zxing::MultiFormatReader().decode(bitmap, zxing::DecodeHints(zxing::DecodeHints::EAN_13_HINT)));
        code = result->getText()->getText();
        code_type = my_code_type[result->getBarcodeFormat().value];
        /*fprintf(stdout, "recognization result: %s\n", code.c_str());
        std::cout << "barcode type is: " << my_code_type[result->getBarcodeFormat().value] << "\n\n";
        */
        return true;
    }
    catch (zxing::Exception& e)
    {
        //std::cout << "Error: " << e.what() << "\n";
        return false;
    }

}
smiler96 commented 5 years ago

thanks for your good guys!