Ubuntu 连接海康智能相机步骤(亲测,成功读码)

这篇具有很好参考价值的文章主要介绍了Ubuntu 连接海康智能相机步骤(亲测,成功读码)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

Ubuntu 连接海康智能相机步骤(亲测,已成功读码)

(就是按照海康的提供的步骤和源码连接相机,流水账)

  1. 安装Ubuntu20.04
  2. 安装gcc和g++,IDmvs只提供了C代码,所以需要自己make编译,自己去搜下怎么安装。
  3. 官网下载IDMVS软件,进入海康机器人页面,进入下载选项,,选择客户端下载,第二页,选择里面的IDMVS(linux)
  4. 解压安装,我是用deb文件安装的,选择X86_64版本。
  5. 安装完成后,在菜单栏里面有IDMVS图标,可以直接打开,或者进入/opt/IDMVS/bin/,运行./IDMVS.sh,两种方法都能打开客户端。
  6. 连接相机,如果没有找到相机你可以在windows上先连下相机试试,然后设定固定IP,连接相机
  7. 连上之后,进入/opt/IDMVS/有个demo的文件夹,具体在哪里忘记了,可以找一下,
  8. IDMVS只提供了两个demo,进入/GrabImage/目录下,应该只有两个文件,一个GrabImage.c,一个makefile。

Ubuntu 连接海康智能相机步骤(亲测,成功读码),数码相机,ubuntu,linux,相机

  1. 然后把上级目录下的/sdk/里面的.so文件放到/GrabImage/目录下。
  2. 然后sudo make(如果你之前装好了g++和gcc,应该可以make成功)
  3. 执行生成后的文件。./GrabImage,就OK了,选择你想要连接的相机。就能看到获取到的结果了。

以下是GrabImage.c的代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>

#include <cstdlib>
#include <string>
#include <iconv.h>

#include "MvCodeReaderParams.h"
#include "MvCodeReaderErrorDefine.h"
#include "MvCodeReaderCtrl.h"

bool g_bExit = false;
// ch:中文转换条码长度定义 | en:Chinese coding format len
#define MAX_BCR_LEN  512

// ch:中文编码GB2312格式转换UTF_8 | en: Chinese coding format GB2312 to utf_8
int GB2312ToUTF8(char* szSrc, size_t iSrcLen, char* szDst, size_t iDstLen)
{
    iconv_t cd = iconv_open("utf-8//IGNORE", "gb2312//IGNORE");
	if(0 == cd)
    {
		return -2;  
	}
		  
    memset(szDst, 0, iDstLen);
    char **src = &szSrc;
    char **dst = &szDst;
    if(-1 == (int)iconv(cd, src, &iSrcLen, dst, &iDstLen))
    {
		return -1; 
	}
		  
    iconv_close(cd);
    return 0;
}

// ch:等待用户输入enter键来结束取流或结束程序
// en:wait for user to input enter to stop grabbing or end the sample program
void PressEnterToExit(void)
{
    int c;
    while ( (c = getchar()) != '\n' && c != EOF );
    fprintf( stderr, "\nPress Enter to exit.\n");
    while( getchar() != '\n');
    g_bExit = true;
    usleep(1);
}

// ch:打印设备信息 | en:Print device Info
bool PrintDeviceInfo(MV_CODEREADER_DEVICE_INFO* pstMVDevInfo)
{
    if (NULL == pstMVDevInfo)
    {
        printf("The Pointer of pstMVDevInfo is NULL!\r\n");
        return false;
    }

    if (MV_CODEREADER_GIGE_DEVICE == pstMVDevInfo->nTLayerType)
    {
        int nIp1 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0xff000000) >> 24);
        int nIp2 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x00ff0000) >> 16);
        int nIp3 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x0000ff00) >> 8);
        int nIp4 = (pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x000000ff);

        // ch:打印当前相机ip和用户自定义名字 | en:print current ip and user defined name
        printf("CurrentIp: %d.%d.%d.%d\r\n" , nIp1, nIp2, nIp3, nIp4);
        printf("UserDefinedName: %s\r\n\n" , pstMVDevInfo->SpecialInfo.stGigEInfo.chUserDefinedName);
    }
    else if (MV_CODEREADER_USB_DEVICE == pstMVDevInfo->nTLayerType)
    {
        printf("UserDefinedName: %s\r\n\n", pstMVDevInfo->SpecialInfo.stUsb3VInfo.chUserDefinedName);
    }
    else
    {
        printf("Not support.\r\n");
    }

    return true;
}

// ch:获取图像线程 | en:Get Image Thread
static void* GrabImageThread(void* pUser)
{
    int nRet = MV_CODEREADER_OK;

    MV_CODEREADER_IMAGE_OUT_INFO_EX2 stImageInfo = {0};
    memset(&stImageInfo, 0, sizeof(MV_CODEREADER_IMAGE_OUT_INFO_EX2));
    unsigned char * pData = NULL;

    while(1)
    {
        if (g_bExit)
        {
            break;
        }

        nRet = MV_CODEREADER_GetOneFrameTimeoutEx2(pUser, &pData, &stImageInfo, 1000);
        if (nRet == MV_CODEREADER_OK)
        {
            MV_CODEREADER_RESULT_BCR_EX* stBcrResult = (MV_CODEREADER_RESULT_BCR_EX*)stImageInfo.pstCodeListEx;

            printf("Get One Frame: nChannelID[%d] Width[%d], Height[%d], nFrameNum[%d], nTriggerIndex[%d]\n", 
                stImageInfo.nChannelID, stImageInfo.nWidth, stImageInfo.nHeight, stImageInfo.nFrameNum, stImageInfo.nTriggerIndex);

            printf("CodeNum[%d]\n", stBcrResult->nCodeNum);

			char strChar[MAX_BCR_LEN] = {0};
            for (int i = 0; i < stBcrResult->nCodeNum; i++)
            {
				memset(strChar, 0, MAX_BCR_LEN);
				nRet = GB2312ToUTF8(stBcrResult->stBcrInfoEx[i].chCode, strlen(stBcrResult->stBcrInfoEx[i].chCode), strChar, MAX_BCR_LEN);
				if (nRet == MV_CODEREADER_OK)
				{
					printf("CodeNum[%d] Code[%s]\r\n", i, strChar);
				}
				else
				{
					printf("CodeNum[%d] Code[%s]\r\n", i, stBcrResult->stBcrInfoEx[i].chCode);
				}				
            }
        }
        else
        {
            printf("No data[0x%x]\r\n", nRet);
        }
    }

    return 0;

}

// ch:主处理函数 | en:main process
int main()
{
    int nRet = MV_CODEREADER_OK;
    void* handle = NULL;
	bool bIsNormalRun = true;

    do
    {
        MV_CODEREADER_DEVICE_INFO_LIST stDeviceList;
        memset(&stDeviceList, 0, sizeof(MV_CODEREADER_DEVICE_INFO_LIST));

        // ch:枚举设备 | Enum device
        nRet = MV_CODEREADER_EnumDevices(&stDeviceList, MV_CODEREADER_GIGE_DEVICE);
        if (MV_CODEREADER_OK != nRet)
        {
            printf("Enum Devices fail! nRet [%#x]\r\n", nRet);
            break;
        }
        else
        {
            printf("Enum Devices succeed!\r\n");
        }

        if (stDeviceList.nDeviceNum > 0)
        {
            for (unsigned int i = 0; i < stDeviceList.nDeviceNum; i++)
            {
                printf("[device %d]:\r\n", i);
                MV_CODEREADER_DEVICE_INFO* pDeviceInfo = stDeviceList.pDeviceInfo[i];
                if (NULL == pDeviceInfo)
                {
                    break;
                }
                PrintDeviceInfo(pDeviceInfo);
            }
        }
        else
        {
            printf("Find No Devices!\r\n");
            break;
        }

        printf("Please Intput camera index:");
        unsigned int nIndex = 0;
        scanf("%d", &nIndex);

        if (nIndex >= stDeviceList.nDeviceNum)
        {
            printf("Intput error!\r\n");
            break;
        }

        // ch:选择设备并创建句柄 | Select device and create handle
        nRet = MV_CODEREADER_CreateHandle(&handle, stDeviceList.pDeviceInfo[nIndex]);
        if (MV_CODEREADER_OK != nRet)
        {
            printf("Create Handle fail! nRet [%#x]\r\n", nRet);
            break;
        }
        else
        {
            printf("Create Handle succeed!\r\n");
        }

        // ch:打开设备 | Open device
        nRet = MV_CODEREADER_OpenDevice(handle);
        if (MV_CODEREADER_OK != nRet)
        {
            printf("Open Device fail! nRet [%#x]\r\n", nRet);
            break;
        }
        else
        {
            printf("Open Device succeed!\r\n");
        }

        // ch:设置触发模式为off | eb:Set trigger mode as off
        nRet = MV_CODEREADER_SetEnumValue(handle, "TriggerMode", MV_CODEREADER_TRIGGER_MODE_OFF);
        if (MV_CODEREADER_OK != nRet)
        {
            printf("Set Trigger Mode fail! nRet [%#x]\r\n", nRet);
            break;
        }
        else
        {
            printf("Set Trigger Mode succeed!\r\n");
        }

        // ch:开始取流 | en:Start grab image
        nRet = MV_CODEREADER_StartGrabbing(handle);
        if (MV_CODEREADER_OK != nRet)
        {
            printf("Start Grabbing fail! nRet [%#x]\r\n", nRet);
            break;
        }
        else
        {
            printf("Start Grabbing succeed!\r\n");
        }

        pthread_t nThreadID;
		nRet = pthread_create(&nThreadID, NULL, GrabImageThread, handle);
        if (MV_CODEREADER_OK != nRet)
        {
            printf("Thread create failed! nRet [%d]\r\n", nRet);
            break;
        }

        PressEnterToExit();
		
		nRet = pthread_join(nThreadID, NULL);
		if (MV_CODEREADER_OK != nRet)
        {
            printf("Thread free failed! nRet = [%d]\r\n", nRet);
			bIsNormalRun = false;
            break;
        }

        // ch:停止取流 | en:Stop grab image
        nRet = MV_CODEREADER_StopGrabbing(handle);
        if (MV_CODEREADER_OK != nRet)
        {
            printf("Stop Grabbing fail! nRet [%#x]\r\n", nRet);
			bIsNormalRun = false;
            break;
        }
        else
        {
            printf("Stop Grabbing succeed!\r\n");
        }

        // ch:关闭设备 | en:close device
        nRet = MV_CODEREADER_CloseDevice(handle);
        if (MV_CODEREADER_OK != nRet)
        {
            printf("MV_CODEREADER_CloseDevice fail! nRet [%#x]\r\n", nRet);
			bIsNormalRun = false;
            break;
        }
        else
        {
            printf("MV_CODEREADER_CloseDevice succeed!\r\n");
        }

        // ch:销毁句柄 | en:Destroy handle
        nRet = MV_CODEREADER_DestroyHandle(handle);
        if (MV_CODEREADER_OK != nRet)
        {
            printf("MV_CODEREADER_DestroyHandle fail! nRet [%#x]\r\n", nRet);
			bIsNormalRun = false;
            break;
        }
        else
        {
			handle = NULL;
            printf("MV_CODEREADER_DestroyHandle succeed!\r\n");
        }

    } while (0);

    if (handle != NULL)
    {
        // ch:关闭设备 | en:Close device
        // ch:销毁句柄 | en:Destroy handle
        MV_CODEREADER_CloseDevice(handle);
        MV_CODEREADER_DestroyHandle(handle);
        handle = NULL;
    }

	if (bIsNormalRun)
	{
		printf("Exit!\r\n");		
	}

	if (false == bIsNormalRun)
	{
		PressEnterToExit();
		printf("Exit!\r\n");
	}
	
    return 0;

}

输出的结果

Ubuntu 连接海康智能相机步骤(亲测,成功读码),数码相机,ubuntu,linux,相机文章来源地址https://www.toymoban.com/news/detail-658761.html

到了这里,关于Ubuntu 连接海康智能相机步骤(亲测,成功读码)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Linux下Docker 离线安装详细步骤,亲测成功

    1.离线原因:公司新创不能使用开元linux,使用了一个变种centOS,致使yum被禁 2.步骤:         2.1 下载docker tar包,下载地址: Index of linux/ https://download.docker.com/linux/       2.2  新建自己的软件目录,然后将tar移过去,并解压  2.3将解压文件中docker下的所有文件移动到/usr/bi

    2024年02月05日
    浏览(28)
  • 海康工业相机SDK连接demo,Qt界面显示

    在机器视觉开发过程中,经常使用工业相机采集图像,以下采用海康机器人提供的SDK开发demo,使用语言C++,界面显示使用的Qt。SDK来自MVS 3.1版本中附属的Development文件夹。完整的项目链接地址为: 海康工业相机SDK的Demo源代码C++版本 附录海康工业相机的命名方式: 海康工业相

    2024年02月13日
    浏览(44)
  • halcon不能连接海康相机解决方法(仅供参考)

    halcon不能连接相机有很多原因,这里作者给出其中一种的解决方法。 首先需要自行先下载海康软件,   1.首先点开Development, 2.根据图片的路径,点开以下文件夹  3.根据自己电脑安装的halcon版本打开对应文件夹     4.我的电脑是win64位的,根据自己的电脑打开对应的文件夹。

    2024年02月12日
    浏览(29)
  • 海康、大华连接相机失败:未安装USB3驱动或驱动无效

    https://blog.csdn.net/weixin_43197380/article/details/124428063 一、前言 设备信息: Win10 + Halcon13 + 海康工业相机(MV-CH120-10UM系列) HK相机客户端下载: 官网地址:HK相机客户端官网下载地址 博主使用的客户端版本:MVS_STD_3.2.1_200609   提取码:z9tk 二、问题分析 博主在下载完客户端,连接

    2024年02月11日
    浏览(55)
  • 关于海康工业相机连接电脑时出现链接速度低于1Ggps解决办法

    一、电脑端网卡配置 打开电脑设置——网络和Internet——高级网络设置——更改适配器选项——双击以太网 网络和Internet点击属性、打开配置 点击配置 点击高级 巨型帧9KB 连接速度和双工模式_1.0Gbps全双工 电源管理取消勾选 二、MVS相机软件参数设置 将相机连接至电脑 打开

    2024年02月02日
    浏览(53)
  • VisionPro连接相机步骤

    一、修改相机与电脑IP地址在同一网段上 1、修改相机IP地址 在菜单栏找到 “Cognex GigE Vision Configurator” ,可直接输出搜索。 或者在visionPro默认安装目录下 “C:Program FilesCognexVisionProbin”,找到“Cognex GigE Vision Configurator” 。  以管理员身份运行 2、修改本机IP地址 在知道相

    2024年02月09日
    浏览(64)
  • ubuntu 20.04 安装 微信,QQ等客户端,一键安装,亲测成功,最新更新,优麒麟

    之前一直使用网页版微信,但是聊天记录完全无法存留,一旦断网就会退出登录,然后每次登录都要确认,很麻烦。 要是有ubuntu下的微信客户端就好了,但是并不是所有的客户端都一样好用,博主安装并实测了几个ubuntu下的微信客户端,发现基于wine的微信客户端最好用,也

    2024年02月07日
    浏览(48)
  • Ubuntu 20.04 X86成功编译运行wayland、wayland-protocols、weston,亲测有效,踩了很多坑,完美解决。

    编译前期准备: 1、更换国内源: #添加阿里源 deb http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ foc

    2024年02月11日
    浏览(38)
  • 宜搭低代码开发师(高级)「FaaS连接器加解密系统」操作步骤(亲测OK)

    目录 目标: 步骤: 一、FaaS连接器 二、页面步骤介绍 轻松搞定高级认证!!! 提示:这里可以添加要学的内容 1. 界面介绍   说明: 右上点击进来 = 点击连接器工厂 = 填写信息(开发语言分三种)  接口入参填写,可以直接在上面测试。不过可以前往云 IDE开发。 工具类(

    2024年02月10日
    浏览(29)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包