使用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数据:
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代码如下:
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
- 使用Visual Studio,几步实现Python C++扩展,以及DLL调用
- C语言创建动态dll,并调用该dll(visual studio 2013环境下)
- C语言创建动态dll,并调用该dll(visual studio 2013环境下)
- Visual Studio调用自己编写的DLL
- visual studio dll生成与调用方法
- 让Visual Studio 调用Matlab的扩展
- Visual C++DLL编程实现
- Visual C++DLL编程实现
- Visual Studio 下生成dll 和调用dll
- 基于Visual Studio的DLL编程实现
- 使用Visual Studio生成DLL文件
- 使用宏(Macro)扩展Visual Studio IDE
- 我经常使用的Visual Studio 扩展
- 使用dll以及调用dll例子
- 使用Visual Studio 2008开发Python的C++扩展之准备步骤
- python 调用C++模块 visual studio版
- C#Visual Studio 资源使用?
- Part2. 使用Visual Studio把C代码编译成.dll文件
- 根据status进行批量修改(Approve和Rejected)(批准还是拒绝)
- dos: tree命令生成目录结构
- MFC对话框中的工具栏、状态栏设计小结
- 直接定址表 输入0-FF之间的数据,并输出到屏幕上
- 提高程序运行效率的10个简单方法
- 使用Visual Studio,几步实现Python C++扩展,以及DLL调用
- 迭代器模式
- oracle trunc(sysdate ,'dd') 日期
- pthread_barrier_init,pthread_barrier_wait简介
- 转载的iOS资源以后保存下
- Liferay 6.2学习笔记(一)--开发环境安装配置
- BAT命令
- 专门为C#和Unity编写的IoC框架:StrangeIoC
- chrome插件:提取页面数据