單個圖像讀取多個條形碼有什麼辦法可以在使用PHP或JavaScript時間從一個單一的圖像掃描多個條形碼。我已經使用Google搜索,但只能掃描圖像中的單個條形碼。使用PHP或JavaScript
我曾嘗試這樣的代碼:https://gist.github.com/tobytailor/421369
單個圖像讀取多個條形碼有什麼辦法可以在使用PHP或JavaScript時間從一個單一的圖像掃描多個條形碼。我已經使用Google搜索,但只能掃描圖像中的單個條形碼。使用PHP或JavaScript
我曾嘗試這樣的代碼:https://gist.github.com/tobytailor/421369
如果你不能找到任何好的PHP或JavaScript條碼的SDK,你可以嘗試一些C/C++軟件開發工具包。我只是向你展示一個關於如何在JavaScript和PHP中封裝C/C++方法的例子。
這裏是具有多個條形碼圖像。
您可以使用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)
對於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);
}
}
如需更詳細的信息,可以參考從GitHub示例代碼。 https://github.com/Dynamsoft/Dynamsoft-Barcode-Reader/tree/master/samples/Node.js https://github.com/Dynamsoft/Dynamsoft-Barcode-Reader/tree/master/samples/PHP
您還可以使用HP IDOL OnDemand(我爲HP工作)中的一個Barcode Recognition API。我跑上面的圖片通過一個快速測試在線的控制檯,並能夠從圖像中提取文件的以下信息...
{
"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": "0",
"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": "",
"barcode_type": "ean-8",
"left": 1696,
"top": 2022,
"width": 390,
"height": 160,
"additional_information": {}
},
{
"text": "",
"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": "0",
"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": {}
}
]
}
發佈您的代碼。 – captainsac
我已經從這個鏈接複製代碼:https://gist.github.com/tobytailor/421369 –
請編輯的問題,並提到在它的鏈接。它會讓你的問題更清楚。 – captainsac