初步成形,还有GTK_demo以及QT_demo未完成

This commit is contained in:
2026-06-26 16:03:24 +08:00
commit 1dfdeb3ce8
15 changed files with 370 additions and 0 deletions

9
.gitignore vendored Normal file
View File

@@ -0,0 +1,9 @@
# Xmake cache
.xmake/
build/
./vscode
# MacOS Cache
.DS_Store

18
.vscode/c_cpp_properties.json vendored Normal file
View File

@@ -0,0 +1,18 @@
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"/usr/include/opencv4"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "c11",
"cppStandard": "c++11",
"intelliSenseMode": "linux-gcc-x64",
"browse": {}
}
],
"version": 4
}

3
README.md Normal file
View File

@@ -0,0 +1,3 @@
# MindVision摄像头SDK简述
此文档用于针对**MindVision摄像头**的使用的教学。
具体细节请阅读[教程内容](教程内容.md)

BIN
component/例一.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 649 KiB

BIN
component/例七.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 57 KiB

BIN
component/例三.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

BIN
component/例九.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

BIN
component/例二.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

BIN
component/例五.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.3 KiB

BIN
component/例八.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 63 KiB

BIN
component/例六.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 65 KiB

BIN
component/例四.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 25 KiB

88
demo/example1.cpp Normal file
View File

@@ -0,0 +1,88 @@
#include <iostream>
#include "CameraApi.h"
#include "opencv2/core.hpp"
#include "opencv2/opencv.hpp"
#include "opencv2/highgui.hpp"
unsigned char * g_pRgbBuffer;
int main(){
///////////////////// 主体部分
int iCameraCounts = 1;
int iStatues = -1;
int setStatues = -1;
tSdkCameraDevInfo tCameraEnumList;
double* pfExposureTime;
int hCamera;
tSdkCameraCapbility tCapability;
tSdkFrameHead sFrameInfo;
BYTE* pbyBuffer;
int iDisplatFrames = 10000;
int channel = 3;
CameraSdkInit(1);
iStatues = CameraEnumerateDevice(&tCameraEnumList, &iCameraCounts);
printf("statue = %d\n", iStatues);
printf("count = %d\n", iCameraCounts);
if(iCameraCounts == 0){
return -1;
}
iStatues = CameraInit(&tCameraEnumList, -1, -1, &hCamera);
printf("state = %d\n", iStatues);
if(iStatues != CAMERA_STATUS_SUCCESS){
return -1;
}
CameraGetCapability(hCamera, &tCapability);
g_pRgbBuffer = (unsigned char*)malloc(tCapability.sResolutionRange.iHeightMax*tCapability.sResolutionRange.iWidthMax*3);
CameraPlay(hCamera);
///////////////////////
CameraSetAeState(hCamera, false);
setStatues = CameraSetExposureTime(hCamera, 5000);
CameraSetGain(hCamera, 100, 70, 50);
printf("statue = %d\n", setStatues);
if(tCapability.sIspCapacity.bMonoSensor){
channel = 1;
CameraSetIspOutFormat(hCamera, CAMERA_MEDIA_TYPE_MONO8);
}else{
channel = 3;
CameraSetIspOutFormat(hCamera, CAMERA_MEDIA_TYPE_BGR8);
}
///////////////////// 主体部分
while(iDisplatFrames){
if(CameraGetImageBuffer(hCamera, &sFrameInfo, &pbyBuffer, 1000) == CAMERA_STATUS_SUCCESS){
CameraImageProcess(hCamera, pbyBuffer, g_pRgbBuffer, &sFrameInfo);
cv::Mat matImage(
cv::Size(sFrameInfo.iWidth,sFrameInfo.iHeight),
sFrameInfo.uiMediaType == CAMERA_MEDIA_TYPE_MONO8 ? CV_8UC1 : CV_8UC3,
g_pRgbBuffer
);
imshow("example1", matImage);
cv::waitKey(5);
CameraReleaseImageBuffer(hCamera, pbyBuffer);
}
}
CameraUnInit(hCamera);
free(g_pRgbBuffer);
////////////////////
return 0;
}

82
xmake.lua Normal file
View File

@@ -0,0 +1,82 @@
set_languages("1.0.0")
add_requires("opencv 4.x", {configs = {shared = true}})
set_languages("c++11")
add_rules("mode.debug", "mode.release")
target("MdVison_edu")
set_kind("binary")
add_files("demo/example1.cpp")
add_packages("opencv")
set_rundir("$(projectdir)")
add_links("MVSDK")
--
-- If you want to known more usage about xmake, please see https://xmake.io
--
-- ## FAQ
--
-- You can enter the project directory firstly before building project.
--
-- $ cd projectdir
--
-- 1. How to build project?
--
-- $ xmake
--
-- 2. How to configure project?
--
-- $ xmake f -p [macosx|linux|iphoneos ..] -a [x86_64|i386|arm64 ..] -m [debug|release]
--
-- 3. Where is the build output directory?
--
-- The default output directory is `./build` and you can configure the output directory.
--
-- $ xmake f -o outputdir
-- $ xmake
--
-- 4. How to run and debug target after building project?
--
-- $ xmake run [targetname]
-- $ xmake run -d [targetname]
--
-- 5. How to install target to the system directory or other output directory?
--
-- $ xmake install
-- $ xmake install -o installdir
--
-- 6. Add some frequently-used compilation flags in xmake.lua
--
-- @code
-- -- add debug and release modes
-- add_rules("mode.debug", "mode.release")
--
-- -- add macro definition
-- add_defines("NDEBUG", "_GNU_SOURCE=1")
--
-- -- set warning all as error
-- set_warnings("all", "error")
--
-- -- set language: c99, c++11
-- set_languages("c99", "c++11")
--
-- -- set optimization: none, faster, fastest, smallest
-- set_optimize("fastest")
--
-- -- add include search directories
-- add_includedirs("/usr/include", "/usr/local/include")
--
-- -- add link libraries and search directories
-- add_links("tbox")
-- add_linkdirs("/usr/local/lib", "/usr/lib")
--
-- -- add system link libraries
-- add_syslinks("z", "pthread")
--
-- -- add compilation and link flags
-- add_cxflags("-stdnolib", "-fno-strict-aliasing")
-- add_ldflags("-L/usr/local/lib", "-lpthread", {force = true})
--
-- @endcode
--

170
教程内容.md Normal file
View File

@@ -0,0 +1,170 @@
# MindVison 摄像头使用简述
如需访问官方请点击此处 --> [***MindVision***](https://www.mindvision.com.cn/)
## 目录
- [第一章 MindVision-SDK安装教程](#第一章-mindvision-sdk安装教程)
- [第一步 获取SDK安装包](#第一步-获取sdk安装包)
- [第二步 解压并安装](#第二步-解压并安装)
- [第二章 MindVision-开发与利用](#第二章-mindvision-开发与利用)
- [第零节 环境的搭配](#第零节-环境的搭配)
- [第一节 demo的运行-浅浅上手](#第一节-demo的运行-浅浅上手)
- [第二节 深入品鉴demo](#第二节-深入品鉴demo)
- [补充](#补充)
- [1.对于Linux下官方驱动程序的安装](#1对于linux下官方程序的编译与应用gtk-demo--qt-demo)
- [2.虚拟机帧率不高的问题](#2.虚拟机帧率不高的问题)
## 第一章 MindVision-SDK安装教程
### 第一步 获取SDK安装包
输入下面的命令获取MindVision的***SDK安装包***
```bash
wget https://www.mindvision.com.cn/wp-content/uploads/2026/02/linuxSDK_V2.1.0.49202602041120.tar.gz
```
![图一](component/例一.png)
下载完成之后**ls**会有一个LinuxSDK
![图二](component/例二.png)
### 第二步 解压并安装
```bash
mkdir MindVisionSDK && cd MindVisionSDK
tar -zxf ~/linuxSDK_V2.1.0.49202602041120.tar.gz
```
解压完成后,可以获得如下内容
![图三](component/例三.png)
```text
MindVisionSDK/
├── demo/ // 相机的demo程序工程及源码
├── document/ // 有关linuxSDK详细说明在里面
├── tools // ip配置工具
├── lib // 各平台使用的SDK库文件x86 x64 arm以及arm64
├── include // SDK头文件
├── install.sh: // 安装运行脚本需要root运行
└── 88-mvusb.rules // USB设备权限修改文件
```
之后运行install.sh脚本文件(需要root权限)
```bash
sudo ./install.sh
```
![图四](component/例四.png)
显示这样之后就安装OK了接下来重启虚拟机
## 第二章 MindVision-开发与利用
### 第零节 环境的搭配
为了方便链接MDVS的CameraApi.h需要能跳转定义的配置请下载插件[C/C++](https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools)。**如果你环境OK就[开始demo吧](#第一节-demo的运行-浅浅上手)**
在vscode快捷键`Ctrl+Shift+P`,找到 `C/C++:编译配置(UI)`,之后点击进入。将**C版本与C++版本调为11**。以及对于包含路径,加一句`/usr/include/opencv4`。之后你会看到左侧目录多了一个.vscode的其下有c_cpp_properties.json。
---
![图六](component/例六.png)
---
与此同时,你还会解决这个问题
![图五](component/例五.png)
---
### 第一节 demo的运行-浅浅上手
在我们的`demo`文件夹下存放案例`example1.cpp`。直接在工作文件夹终端键入`xmake run`
---
![图七](component/例七.png)
---
显示这个错误说明代码成功了!!!!你只是没有连接摄像头。
如果你连接了摄像头,但是很卡顿请阅读[虚拟机帧率不高的问题](#2虚拟机帧率不高的问题)
### 第二节 深入品鉴demo
**MindVision摄像头读**取大致流程:
![图九](component/例九.png)
有关所有的错误抛出定义请阅读`CameraStatus.h`.
下面将简单分析代码作用,对于相机驱动函数的参数请自行阅读`CameraApi.h`.
```cpp
CameraSdkInit(1); // 相机接口初始化函数
iStatues = CameraEnumerateDevice(&tCameraEnumList, &iCameraCounts); // 枚举相机
iStatues = CameraInit(&tCameraEnumList, -1, -1, &hCamera) // 相机初始化
CameraGetCapability(hCamera, &tCapability); // 获取相机内部配置信息
g_pRgbBuffer = (unsigned char*)malloc(tCapability.sResolutionRange.iHeightMax*tCapability.sResolutionRange.iWidthMax*3); // 分配与相机内部画面相同的空间!!!
CameraPlay(hCamera); // 启动相机
CameraSetAeState(hCamera, false); // 相机曝光相关的设置......
setStatues = CameraSetExposureTime(hCamera, 5000);
CameraSetGain(hCamera, 100, 70, 50);
```
后面所有有关相机画面设置相关的内容,直接调用函数配置就行。
例如:
```cpp
iStatues = CameraConnectTest(hCamera); // CameraConnectTest 检查相机连接状态的函数,
```
根据函数接口定义进行调用。
```cpp
/******************************************************/
// 函数名 : CameraReConnect
// 功能描述 : 重新连接设备用于USB设备意外掉线后重连
// 参数 : hCamera 相机的句柄由CameraInit函数获得。
// 返回值 : 成功时返回CAMERA_STATUS_SUCCESS (0);
// 否则返回非0值的错误码,请参考CameraStatus.h
// 中错误码的定义。
/******************************************************/
MVSDK_API CameraSdkStatus CameraReConnect(
CameraHandle hCamera
);
```
**关键**:对于每次获取画面之后,记得清理调用`CameraGetImageBuffer`缓冲区。具体细节请参阅`CameraGetImageBuffer`函数说明!!!
对于正常的工况MindVisionApi给我们提供了摄像头重连、状态检测等等的函数接口我们直接调用就可以用最为简洁的代码提高摄像头连接的抗干扰能力
## 第三节 对于一些关键接口的列表
| 函数名 | 作用 |
| :----: | :----: |
| CameraSetAeState | 设置相机曝光的模式。自动或者手动。(如果需要自定义曝光一定要先设置为手动)|
| CameraSetExposureTime | 设置曝光时间。单位为微秒。 |
| CameraSetFrameSpeed | 设定相机输出图像的帧率。 |
| CameraCreateSettingPageEx | 创建该相机的属性配置窗口。调用该函数SDK内部会帮您创建好相机的配置窗口 |
| CameraReConnect | 重新连接设备用于USB设备意外掉线后重连 |
| CameraConnectTest | 测试相机的连接状态,用于检测相机是否掉线 |
| CameraSetImageResolution | 设置预览的分辨率。|
## &补充
### 1.对于Linux下官方程序的编译与应用(GTK-demo / QT-demo)
***前言*** 考虑到队员们的系统都为Windows虚拟机与MindVision摄像头连接不畅不考虑安装官方Linux的驱动程序对于镜头参数的调节我的观点是在Windows的驱动程序[MVDCP2](https://www.mindvision.com.cn/wp-content/uploads/2026/04/MindVision-Camera-Platform-Setup2.1.10.195_202604021438.exe)上进行图像调节再将对应参数修改考虑到NUC为Linux赛场调试可能需要故对此部分进行说明。
#### GTK_Demo 版本
首先安装GTK库
```bash
sudo apt install libgtk2.0-dev
```
之后进入MindVisionSDK/demo/GTK_Demo编译
```bash
cd MindVisionSDK/demo/GTK_Demo
make clean
make
./Gtk_Demo // 开始运行
```
### 2.虚拟机帧率不高的问题
在虚拟机中默认的接口为***USB2.0***MindVision摄像头**高帧率**需使用**USB3.0**及以上。
![图八](component/例八.png)
修改后记得重启虚拟机。
............... 未完待续
```text
Author Simba-Jokit
Date: 2026/6/25
```