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

在网上搜了下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

Python 中,有两种方法可以创建 C/C++ 扩展模块,一种是使用 ctypes 库将 C/C++ 代码封装为 Python 模块,另一种是使用 Python 的 C API 构建 Python 模块。 如果你想使用 Python 的 C API 构建 Python 模块,那么可以使用以下步骤编译 pyd 文件: 1. 编写 C/C++ 扩展模块的代码,并将代码保存为 .c 或 .cpp 文件。 2. 使用 Visual Studio 或者 GCC 等编译器将 C/C++ 代码编译成动态链接库(DLL)或共享对象(SO)文件。 3. 在编译时链接 Python 库文件和头文件。 4. 将生成的 DLL 或 SO 文件重命名为 pyd 文件。 下面是一个示例代码: ```c++ #include <Python.h> static PyObject* example(PyObject* self, PyObject* args) { // 实现扩展模块的逻辑 return Py_BuildValue("s", "Hello, World!"); } static PyMethodDef exampleMethods[] = { {"example", example, METH_VARARGS, "example function"}, {NULL, NULL, 0, NULL} }; static struct PyModuleDef exampleModule = { PyModuleDef_HEAD_INIT, "example", "example module", -1, exampleMethods }; PyMODINIT_FUNC PyInit_example(void) { return PyModule_Create(&exampleModule); } ``` 上面的代码实现了一个名为 example 的函数,并将其作为 Python 模块的一部分导出。如果你使用 Visual Studio 编译器,可以使用以下命令编译该代码: ``` cl /LD /I C:\Python38\include example.c /link /LIBPATH:C:\Python38\libs python38.lib ``` 这将在当前目录下生成 example.dll 文件。你可以将其重命名为 example.pyd 文件,然后将其导入到 Python 代码中使用。 注意:在编译时需要根据你的 Python 版本和安装路径设置头文件、库文件、链接库等相关参数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值