static PyObject * decodeFile(PyObject *self, PyObject *args) { char *pFileName; int option_iMaxBarcodesNumPerPage = -1; int option_llBarcodeFormat = -1; if (!PyArg_ParseTuple(args, "s", &pFileName)) { return NULL; } pBarcodeResultArray pResults = NULL; ReaderOptions option; SetOptions(&option, option_iMaxBarcodesNumPerPage, option_llBarcodeFormat); int ret = DBR_DecodeFile( pFileName, &option, &pResults ); if (ret == DBR_OK){ int count = pResults->iBarcodeCount; pBarcodeResult* ppBarcodes = pResults->ppBarcodes; pBarcodeResult tmp = NULL; PyObject* list = PyList_New(count); PyObject* result = NULL; for (int i = 0; i < count; i++) { tmp = ppBarcodes[i]; result = PyString_FromString(tmp->pBarcodeData); PyList_SetItem(list, i, Py_BuildValue("iN", (int)tmp->llFormat, result)); } // release memory DBR_FreeBarcodeResults(&pResults); return list; } return Py_None; }
/** * Decode barcode from a file * */ static PyObject * decodeFile(PyObject *self, PyObject *args) { char *pFileName; if (!PyArg_ParseTuple(args, "s", &pFileName)) { return NULL; } // Dynamsoft Barcode Reader initialization __int64 llFormat = (OneD | QR_CODE | PDF417 | DATAMATRIX); int iMaxCount = 0x7FFFFFFF; ReaderOptions ro = {0}; pBarcodeResultArray pResults = NULL; ro.llBarcodeFormat = llFormat; ro.iMaxBarcodesNumPerPage = iMaxCount; // Barcode detection int ret = DBR_DecodeFile(pFileName, &ro, &pResults); printf("DecodeFile ret: %d\n", ret); // Get results int count = pResults->iBarcodeCount; pBarcodeResult* ppBarcodes = pResults->ppBarcodes; pBarcodeResult tmp = NULL; PyObject* list = PyList_New(count); // The returned Python object PyObject* result = NULL; int i = 0; for (; i < count; i++) { tmp = ppBarcodes[i]; result = PyString_FromString(tmp->pBarcodeData); PyList_SetItem(list, i, Py_BuildValue("iN", (int)tmp->llFormat, result)); // Add results to list } // release memory DBR_FreeBarcodeResults(&pResults); return list; }
VALUE decode(const char *pszImageFile) { VALUE ary = rb_ary_new(); __int64 llFormat = (OneD |QR_CODE); int iMaxCount = 0x7FFFFFFF; int iIndex = 0; ReaderOptions ro = {0}; pBarcodeResultArray paryResult = NULL; int iRet = -1; char * pszTemp = NULL; char * pszTemp1 = NULL; struct timeval begin, end; if (NULL == pszImageFile) { printf("The syntax of the command is incorrect.\n"); return 1; } // Set license DBR_InitLicense("B8DF4560D0953299D5454C71A38D1FEB"); // Read barcode gettimeofday(&begin, NULL); ro.llBarcodeFormat = llFormat; ro.iMaxBarcodesNumPerPage = iMaxCount; iRet = DBR_DecodeFile(pszImageFile, &ro, &paryResult); gettimeofday(&end, NULL); // Output barcode result pszTemp = (char*)malloc(4096); if (iRet != DBR_OK) { sprintf(pszTemp, "Failed to read barcode: %s\r\n", DBR_GetErrorString(iRet)); printf("%s", pszTemp); free(pszTemp); return 1; } if (paryResult->iBarcodeCount == 0) { sprintf(pszTemp, "No barcode found. Total time spent: %.3f seconds.\r\n", ((float)((end.tv_sec * 1000 * 1000 + end.tv_usec) - (begin.tv_sec * 1000 * 1000 + begin.tv_usec))/(1000 * 1000))); printf("%s", pszTemp); DBR_FreeBarcodeResults(&paryResult); return 0; } sprintf(pszTemp, "Total barcode(s) found: %d. Total time spent: %.3f seconds\r\n\r\n", paryResult->iBarcodeCount, ((float)((end.tv_sec * 1000 * 1000 + end.tv_usec) - (begin.tv_sec * 1000 * 1000 + begin.tv_usec))/(1000 * 1000))); printf("%s", pszTemp); for (iIndex = 0; iIndex < paryResult->iBarcodeCount; iIndex++) { sprintf(pszTemp, "Barcode %d:\r\n", iIndex + 1); // printf("%s", pszTemp); sprintf(pszTemp, "%s Page: %d\r\n", pszTemp, paryResult->ppBarcodes[iIndex]->iPageNum); // printf("%s", pszTemp); sprintf(pszTemp, "%s Type: %s\r\n", pszTemp, GetFormatStr(paryResult->ppBarcodes[iIndex]->llFormat)); // printf("%s", pszTemp); pszTemp1 = (char*)malloc(paryResult->ppBarcodes[iIndex]->iBarcodeDataLength + 1); memset(pszTemp1, 0, paryResult->ppBarcodes[iIndex]->iBarcodeDataLength + 1); memcpy(pszTemp1, paryResult->ppBarcodes[iIndex]->pBarcodeData, paryResult->ppBarcodes[iIndex]->iBarcodeDataLength); sprintf(pszTemp, "%s Value: %s\r\n", pszTemp, pszTemp1); // printf("%s", pszTemp); free(pszTemp1); // sprintf(pszTemp, " Region: {Left: %d, Top: %d, Width: %d, Height: %d}\r\n\r\n", // paryResult->ppBarcodes[iIndex]->iLeft, paryResult->ppBarcodes[iIndex]->iTop, // paryResult->ppBarcodes[iIndex]->iWidth, paryResult->ppBarcodes[iIndex]->iHeight); // printf("%s", pszTemp); rb_ary_push(ary, rb_str_new_cstr(pszTemp)); } free(pszTemp); DBR_FreeBarcodeResults(&paryResult); return ary; }
/** * Decode barcode from an image buffer. The supported data structure is DIB. * It is necessary to re-construct input data for barcode detection. */ static PyObject * decodeBuffer(PyObject *self, PyObject *args) { PyObject *o; if (!PyArg_ParseTuple(args, "O", &o)) return NULL; PyObject *ao = PyObject_GetAttrString(o, "__array_struct__"); PyObject *retval; if ((ao == NULL) || !PyCObject_Check(ao)) { PyErr_SetString(PyExc_TypeError, "object does not have array interface"); return NULL; } PyArrayInterface *pai = (PyArrayInterface*)PyCObject_AsVoidPtr(ao); if (pai->two != 2) { PyErr_SetString(PyExc_TypeError, "object does not have array interface"); Py_DECREF(ao); return NULL; } // Construct data with header info and image data char *buffer = (char*)pai->data; // The address of image data int width = pai->shape[1]; // image width int height = pai->shape[0]; // image height int size = pai->strides[0] * pai->shape[0]; // image size = stride * height char *total = (char *)malloc(size + 40); // buffer size = image size + header size memset(total, 0, size + 40); BITMAPINFOHEADER bitmap_info = {40, width, height, 0, 24, 0, size, 0, 0, 0, 0}; memcpy(total, &bitmap_info, 40); // Copy image data to buffer from bottom to top char *data = total + 40; int stride = pai->strides[0]; int i = 1; for (; i <= height; i++) { memcpy(data, buffer + stride * (height - i), stride); data += stride; } // Dynamsoft Barcode Reader initialization __int64 llFormat = (OneD | QR_CODE | PDF417 | DATAMATRIX); int iMaxCount = 0x7FFFFFFF; ReaderOptions ro = {0}; pBarcodeResultArray pResults = NULL; ro.llBarcodeFormat = llFormat; ro.iMaxBarcodesNumPerPage = iMaxCount; printf("width: %d, height: %d, size:%d\n", width, height, size); int iRet = DBR_DecodeBuffer((unsigned char *)total, size + 40, &ro, &pResults); printf("DBR_DecodeBuffer ret: %d\n", iRet); free(total); // Do not forget to release the constructed buffer // Get results int count = pResults->iBarcodeCount; pBarcodeResult* ppBarcodes = pResults->ppBarcodes; pBarcodeResult tmp = NULL; retval = PyList_New(count); // The returned Python object PyObject* result = NULL; i = 0; for (; i < count; i++) { tmp = ppBarcodes[i]; result = PyString_FromString(tmp->pBarcodeData); printf("result: %s\n", tmp->pBarcodeData); PyList_SetItem(retval, i, Py_BuildValue("iN", (int)tmp->llFormat, result)); // Add results to list } // release memory DBR_FreeBarcodeResults(&pResults); Py_DECREF(ao); return retval; }