使用Visual Studio,几步实现Python C++扩展,以及DLL调用

来源:互联网 发布:think in java 电子版 编辑:程序博客网 时间:2024/06/14 01:40

在网上搜了下Python扩展教程,很多提到第三方开源库,而官方推荐的是用setup.py。其实用Visual Studio很简单!来看一下如何一步步编写一个Python扩展,以及如何通过扩展来调用DLL。

参考原文:Wrapping C/C++ Methods of Dynamsoft Barcode SDK for Python

Visual Studio环境配置

在Visual Studio中创建一个Win32工程DynamsoftBarcodeReader。打开工程属性,添加头文件和库文件路径:

添加依赖库python27.lib

把扩展改成.pyd

这样就可以去build工程了。不过如果是使用Debug去build,会出现错误:

原因是因为官方发布的python安装包不包涵debug的库,如果需要可以自己下载源码编译。所以把配置切换到release,成功生成DynamsoftBarcodeReader.pyd

DLL调用举例:封装Dynamsoft Barcode SDK C++接口

在Python脚本中导入DynamsoftBarcodeReader,Python会搜索DynamsoftBarcodeReader.pyd,并且调用initDynamsoftBarcodeReader()做初始化。

在工程配置中先加入Dynamsoft Barcode SDK的头文件和库路径,然后使用下面的代码调用Barcode解码,并把结果转换成Python数据:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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;
}
  
static PyMethodDef methods[] = {
    "initLicense", initLicense, METH_VARARGS, NULL },
    "decodeFile", decodeFile, METH_VARARGS, NULL },
    { NULL, NULL }
};
 

在工程配置中加入DLL自动拷贝,在编译之后就可以把DLL拷贝到release目录中了:

编译工程生成DynamsoftBarcodeReader.pyd

在release目录中新建一个Python脚本:

一个简单的Python Barcode Reader代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import os.path
import DynamsoftBarcodeReader
  
formats = {
    0x1FFL "OneD",
    0x1L   "CODE_39",
    0x2L "CODE_128",
    0x4L   "CODE_93",
    0x8L "CODABAR",
    0x10L   "ITF",
    0x20L "EAN_13",
    0x40L   "EAN_8",
    0x80L "UPC_A",
    0x100L   "UPC_E",
}
  
def initLicense(license):
    DynamsoftBarcodeReader.initLicense(license)
  
def decodeFile(fileName):
    results = DynamsoftBarcodeReader.decodeFile(fileName)
    for result in results:
        print "barcode format: " + formats[result[0]]
        print "barcode value: " + result[1]
  
if __name__ == "__main__":
    barcode_image = input("Enter the barcode file: ");
    if not os.path.isfile(barcode_image):
        print "It is not a valid file."
    else:
        decodeFile(barcode_image);
 

用Barcode图片测试一下:

源码

https://github.com/Dynamsoft/Dynamsoft-Barcode-Reader/tree/master/samples/Python

0 0
原创粉丝点击