Read multiple barcode from a single image using php or javascript

北慕城南 提交于 2019-12-22 11:29:27

问题


Is there any way to scan multiple bar-codes from a single image at a time using PHP or JavaScript. I have googled, but can scan only single barcode from an image.

I have tried this code : https://gist.github.com/tobytailor/421369


回答1:


If you can't find any good PHP or JavaScript Barcode SDKs, you can try some C/C++ SDKs. I just show you an example about how to wrap C/C++ methods in JavaScript and PHP.

Here is an image with multiple Barcodes.

You can use Dynamsoft Barcode SDK to decode the image and convert all returned results to JavaScript or PHP data type.

For JavaScript:

#include <node.h>

#include "If_DBR.h"
#include "BarcodeFormat.h"
#include "BarcodeStructs.h"
#include "ErrorCode.h"


#ifdef _WIN64
#pragma comment(lib, "DBRx64.lib")
#else
#pragma comment(lib, "DBRx86.lib")
#endif

using namespace v8;

void SetOptions(pReaderOptions pOption, int option_iMaxBarcodesNumPerPage, int option_llBarcodeFormat){

    if (option_llBarcodeFormat > 0)
        pOption->llBarcodeFormat = option_llBarcodeFormat;
    else
        pOption->llBarcodeFormat = OneD;

    if (option_iMaxBarcodesNumPerPage > 0)
        pOption->iMaxBarcodesNumPerPage = option_iMaxBarcodesNumPerPage;
    else
        pOption->iMaxBarcodesNumPerPage = INT_MAX;

}

void DecodeFile(const FunctionCallbackInfo<Value>& args) {

    Isolate* isolate = Isolate::GetCurrent();
    HandleScope scope(isolate);

    // convert v8 string to char *
    String::Utf8Value utfStr(args[0]->ToString());
    char *pFileName = *utfStr;

    int option_iMaxBarcodesNumPerPage = -1;
    int option_llBarcodeFormat = -1;

    pBarcodeResultArray pResults = NULL;
    ReaderOptions option;
    SetOptions(&option, option_iMaxBarcodesNumPerPage, option_llBarcodeFormat);

    // decode barcode image file
    int ret = DBR_DecodeFile(
        pFileName,
        &option,
        &pResults
        );

    if (ret == DBR_OK){
        int count = pResults->iBarcodeCount;
        pBarcodeResult* ppBarcodes = pResults->ppBarcodes;
        pBarcodeResult tmp = NULL;

        // javascript callback function
        Local<Function> cb = Local<Function>::Cast(args[1]);
        const unsigned argc = 1;

        // array for storing barcode results
        Local<Array> barcodeResults = Array::New(isolate);

        for (int i = 0; i < count; i++)
        {
            tmp = ppBarcodes[i];

            Local<Object> result = Object::New(isolate);
            result->Set(String::NewFromUtf8(isolate, "format"), Number::New(isolate, tmp->llFormat));
            result->Set(String::NewFromUtf8(isolate, "value"), String::NewFromUtf8(isolate, tmp->pBarcodeData));

            barcodeResults->Set(Number::New(isolate, i), result);
        }

        // release memory
        DBR_FreeBarcodeResults(&pResults);

        Local<Value> argv[argc] = { barcodeResults };
        cb->Call(isolate->GetCurrentContext()->Global(), argc, argv);
    }
}

void Init(Handle<Object> exports) {
    NODE_SET_METHOD(exports, "decodeFile", DecodeFile);
}

NODE_MODULE(dbr, Init)

For PHP:

#include "php_dbr.h"

#include "If_DBR.h"
#include "BarcodeFormat.h"
#include "BarcodeStructs.h"
#include "ErrorCode.h"

#ifdef _WIN64
#pragma comment(lib, "DBRx64.lib")
#else
#pragma comment(lib, "DBRx86.lib")
#endif

void SetOptions(pReaderOptions pOption, int option_iMaxBarcodesNumPerPage, int option_llBarcodeFormat){

    if (option_llBarcodeFormat > 0)
        pOption->llBarcodeFormat = option_llBarcodeFormat;
    else
        pOption->llBarcodeFormat = OneD;

    if (option_iMaxBarcodesNumPerPage > 0)
        pOption->iMaxBarcodesNumPerPage = option_iMaxBarcodesNumPerPage;
    else
        pOption->iMaxBarcodesNumPerPage = INT_MAX;

}

ZEND_FUNCTION(DecodeBarcodeFile);

zend_function_entry CustomExtModule_functions[] = {
    ZEND_FE(DecodeBarcodeFile, NULL)
    {NULL, NULL, NULL}
};

zend_module_entry CustomExtModule_module_entry = {
    STANDARD_MODULE_HEADER,
    "Dynamsoft Barcode Reader",
    CustomExtModule_functions,
    NULL, NULL, NULL, NULL, NULL,
    NO_VERSION_YET, STANDARD_MODULE_PROPERTIES
};

ZEND_GET_MODULE(CustomExtModule)

ZEND_FUNCTION(DecodeBarcodeFile){
    array_init(return_value);

    // Get Barcode image path
    char* pFileName = NULL;
    int iLen = 0;

    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &pFileName, &iLen) == FAILURE) {
        RETURN_STRING("Invalid parameters", true);
    }

    // Dynamsoft Barcode Reader: init
    int option_iMaxBarcodesNumPerPage = -1;
    int option_llBarcodeFormat = -1;
    pBarcodeResultArray pResults = NULL;
    ReaderOptions option;

    SetOptions(&option, option_iMaxBarcodesNumPerPage, option_llBarcodeFormat);

    // decode barcode image file
    int ret = DBR_DecodeFile(
        pFileName,
        &option,
        &pResults
        );

    if (ret == DBR_OK)
    {
        int count = pResults->iBarcodeCount;
        pBarcodeResult* ppBarcodes = pResults->ppBarcodes;
        pBarcodeResult tmp = NULL;

        // loop all results
        for (int i = 0; i < count; i++)
        {
            tmp = ppBarcodes[i];

            // convert format type to string
            char format[64]; 
            sprintf (format, "%d", tmp->llFormat); 

            // (barcode type, result)
            add_assoc_string(return_value, format, tmp->pBarcodeData, 1);
        }

        // Dynamsoft Barcode Reader: release memory
        DBR_FreeBarcodeResults(&pResults);
    }
    else
    {
        RETURN_STRING("No Barcode detected", true);
    }

}

For more detailed information, you can refer to the sample code from GitHub. https://github.com/Dynamsoft/Dynamsoft-Barcode-Reader/tree/master/samples/Node.js https://github.com/Dynamsoft/Dynamsoft-Barcode-Reader/tree/master/samples/PHP




回答2:


You could also use a Barcode Recognition API like the one from HP IDOL OnDemand (I work for HP). I ran the image above through the online console for a quick test and was able to extract the following information from the image file...

{
  "barcode": [
    {
      "text": "CODE128",
      "barcode_type": "code-128",
      "left": 1498,
      "top": 552,
      "width": 598,
      "height": 262,
      "additional_information": {}
    },
    {
      "text": "CODE39",
      "barcode_type": "code-39",
      "left": 300,
      "top": 552,
      "width": 768,
      "height": 262,
      "additional_information": {}
    },
    {
      "text": "0012345678905",
      "barcode_type": "ean-13",
      "left": 1480,
      "top": 1466,
      "width": 619,
      "height": 260,
      "additional_information": {
        "country": "U.S. and Canada"
      }
    },
    {
      "text": "1234567890128",
      "barcode_type": "ean-13",
      "left": 366,
      "top": 1922,
      "width": 584,
      "height": 260,
      "additional_information": {
        "country": "U.S. (reserved for later use)"
      }
    },
    {
      "text": "01234565",
      "barcode_type": "ean-8",
      "left": 1696,
      "top": 2022,
      "width": 390,
      "height": 160,
      "additional_information": {}
    },
    {
      "text": "012345",
      "barcode_type": "codabar",
      "left": 300,
      "top": 1010,
      "width": 672,
      "height": 260,
      "additional_information": {}
    },
    {
      "text": "CODE93",
      "barcode_type": "code-93",
      "left": 300,
      "top": 1466,
      "width": 730,
      "height": 260,
      "additional_information": {}
    },
    {
      "text": "00123456",
      "barcode_type": "i25",
      "left": 1501,
      "top": 1010,
      "width": 264,
      "height": 260,
      "additional_information": {}
    },
    {
      "text": "00",
      "barcode_type": "i25",
      "left": 1394,
      "top": 1029,
      "width": 242,
      "height": 242,
      "additional_information": {}
    }
  ]
}


来源:https://stackoverflow.com/questions/30723550/read-multiple-barcode-from-a-single-image-using-php-or-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!