使用php或javascript从单个图像中读取多个条形码

时间:2015-06-09 05:20:05

标签: javascript php jquery jquery-plugins barcode-scanner

有没有办法使用PHP或JavaScript一次扫描单个图像中的多个条形码。我用谷歌搜索,但只能扫描图像中的单个条形码。

我试过这段代码:https://gist.github.com/tobytailor/421369

2 个答案:

答案 0 :(得分:0)

如果找不到任何好的PHP或JavaScript条码SDK,可以试试一些C / C ++ SDK。我只是向您展示如何在JavaScript和PHP中包装C / C ++方法。

这是一个包含多个条形码的图像。 enter image description here

您可以使用Dynamsoft Barcode SDK解码图像并将所有返回的结果转换为JavaScript或PHP数据类型。

对于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)

enter image description here

对于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);
    }

}

enter image description here

有关更多详细信息,请参阅GitHub中的示例代码。 https://github.com/Dynamsoft/Dynamsoft-Barcode-Reader/tree/master/samples/Node.js https://github.com/Dynamsoft/Dynamsoft-Barcode-Reader/tree/master/samples/PHP

答案 1 :(得分:0)

你也可以像HP IDOL OnDemand那样使用Barcode Recognition API(我为HP工作)。我通过在线控制台运行上面的图像进行快速测试,并能够从图像文件中提取以下信息......

{
  "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": {}
    }
  ]
}