使用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。打开工程属性,添加头文件和库文件路径:

使用Visual Studio,几步实现Python C++扩展,以及DLL调用_第1张图片

使用Visual Studio,几步实现Python C++扩展,以及DLL调用_第2张图片

添加依赖库python27.lib

使用Visual Studio,几步实现Python C++扩展,以及DLL调用_第3张图片

把扩展改成.pyd

使用Visual Studio,几步实现Python C++扩展,以及DLL调用_第4张图片

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

使用Visual Studio,几步实现Python C++扩展,以及DLL调用_第5张图片

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

使用Visual Studio,几步实现Python C++扩展,以及DLL调用_第6张图片

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

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

使用Visual Studio,几步实现Python C++扩展,以及DLL调用_第7张图片

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

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目录中了:

使用Visual Studio,几步实现Python C++扩展,以及DLL调用_第8张图片

编译工程生成DynamsoftBarcodeReader.pyd

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

使用Visual Studio,几步实现Python C++扩展,以及DLL调用_第9张图片

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

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图片测试一下:

使用Visual Studio,几步实现Python C++扩展,以及DLL调用_第10张图片

源码

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



你可能感兴趣的:(python,c/c++,barcode)