海康威视相机SDK二次开发(JAVA语言)

该博客围绕使用Java程序读取海康威视相机图片展开。因官网示例代码无法满足需求,需进行二次开发。使用Springboot框架和maven架构,对保存照片的代码进行修改,还处理了过期照片,最终实现图片保存名称不重复、路径自定义等功能。

前言

有个项目需要使用java程序读取海康威视的相机图片。相机通过以太网连接服务器,部署在服务器上的java程序将相机拍摄的画面保存在指定路径下。
海康威视提供了sdk开发包,可以在官网中下载,windows和linux系统都有。但是开发包中给出的示例代码,无法满足实际需要,所以还需要对代码进行二次开发。
在进行二次开发时,官网并未提供java语言的开发手册,示例代码中也并未提供详细注释,所以我只能在阅读示例代码时,按照自己的理解添加一些注释。

客户端创建虚拟相机

实体相机已经还回去了,所以这里用MVS客户端创建一个虚拟相机,测试代码。
在这里插入图片描述
在这里插入图片描述

设置相机名字

在这里插入图片描述

示例代码

我这里只需要对保存照片的代码进行修改。
以下是官网提供的保存照片的代码
我在示例代码中对一些方法做了注释

保存图片程序
/***************************************************************************************************
 * @file      SaveImage.java
 * @breif     Use functions provided in MvCameraControlWrapper.jar to save image as JPEG。
 * @author    zhanglei72
 * @date      2020/02/10
 *
 * @warning  
 * @version   V1.0.0  2020/02/10 Create this file
 * @since     2020/02/10
 **************************************************************************************************/

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

import MvCameraControlWrapper.*;
import static MvCameraControlWrapper.MvCameraControl.*;
import static MvCameraControlWrapper.MvCameraControlDefines.*;

public class SaveImage
{
   
   
	/**
	* 打印连接的设备信息
	* 如果是通过以太网连接的相机,则会输出ip、名称等内容
	* 如果是通过usb连接的相机,则会输出相机名称
	*/
    private static void printDeviceInfo(MV_CC_DEVICE_INFO stDeviceInfo) 
    {
   
   
        if (null == stDeviceInfo) {
   
   
            System.out.println("stDeviceInfo is null");
            return;
        }
        
        if (stDeviceInfo.transportLayerType == MV_GIGE_DEVICE) {
   
   
            System.out.println("\tCurrentIp:       " + stDeviceInfo.gigEInfo.currentIp);
            System.out.println("\tModel:           " + stDeviceInfo.gigEInfo.modelName);
            System.out.println("\tUserDefinedName: " + stDeviceInfo.gigEInfo.userDefinedName);
        } else if (stDeviceInfo.transportLayerType == MV_USB_DEVICE) {
   
   
            System.out.println("\tUserDefinedName: " + stDeviceInfo.usb3VInfo.userDefinedName);
            System.out.println("\tSerial Number:   " + stDeviceInfo.usb3VInfo.serialNumber);
            System.out.println("\tDevice Number:   " + stDeviceInfo.usb3VInfo.deviceNumber);
        } else {
   
   
            System.err.print("Device is not supported! \n");
        }

        System.out.println("\tAccessible:      "
            + MvCameraControl.MV_CC_IsDeviceAccessible(stDeviceInfo, MV_ACCESS_Exclusive));
        System.out.println("");
    }

    private static void printFrameInfo(MV_FRAME_OUT_INFO stFrameInfo)
    {
   
   
        if (null == stFrameInfo)
        {
   
   
            System.err.println("stFrameInfo is null");
            return;
        }
        
        StringBuilder frameInfo = new StringBuilder("");
        frameInfo.append(("\tFrameNum[" + stFrameInfo.frameNum + "]"));
        frameInfo.append("\tWidth[" + stFrameInfo.width + "]");
        frameInfo.append("\tHeight[" + stFrameInfo.height + "]");
        frameInfo.append(String.format("\tPixelType[%#x]", stFrameInfo.pixelType.getnValue()));

        System.out.println(frameInfo.toString());
    }

	/**
	* 保存图片到本地
	* 这个方法中主要是设置了文件保存的路径
	*/
    public static void saveDataToFile(byte[] dataToSave, int dataSize, String fileName)
    {
   
   
        OutputStream os = null;

        try
        {
   
   
            // Create directory
            File tempFile = new File("dat");
            if (!tempFile.exists()) 
            {
   
   
                tempFile.mkdirs();
            }

            os = new FileOutputStream(tempFile.getPath() + File.separator + fileName);
            os.write(dataToSave, 0, dataSize);
            System.out.println("SaveImage succeed.");
        }
        catch (IOException e)
        {
   
   
            e.printStackTrace();
        }
        finally
        {
   
   
            // Close file stream
            try 
            {
   
   
                os.close();
            } 
            catch (IOException e) 
            {
   
   
                e.printStackTrace();
            }
        }
    }
    
    /**
    * 当有多个相机连接但程序只能保存一台相机拍摄的照片
    * 所以需要通过此方法输入相机索引号
    */
    public static int chooseCamera(ArrayList<MV_CC_DEVICE_INFO> stDeviceList)
    {
   
   
        if (null == stDeviceList)
        {
   
   
            return -1;
        }
        
        // Choose a device to operate
        int camIndex = -1;
        Scanner scanner = new Scanner(System.in);

        while (true)
        {
   
   
            try
            {
   
   
            	/*
            	* 手动输入
            	*/
                System.out.print("Please input camera index (-1 to quit):");
                camIndex = scanner.nextInt();
                if ((camIndex >= 0 && camIndex < stDeviceList.size()) || -1 == camIndex)
                {
   
   
                    break;
                }
                else
                {
   
   
                    System.out.println("Input error: " + camIndex);
                }
            }
            catch (Exception e)
            {
   
   
                e.printStackTrace();
                camIndex = -1;
                break;
            }
        }
        scanner.close();

        if (-1 == camIndex)
        {
   
   
            System.out.println("Bye.");
            return camIndex;
        }

        if (0 <= camIndex && stDeviceList.size() > camIndex)
        {
   
   
            if (MV_GIGE_DEVICE == stDeviceList.get(camIndex).transportLayerType)
            {
   
   
                System.out.println("Connect to camera[" + camIndex + "]: " + stDeviceList.get(camIndex).gigEInfo.userDefinedName);
            }
            else if (MV_USB_DEVICE == stDeviceList.get(camIndex).transportLayerType)
            {
   
   
                System.out.println("Connect to camera[" + camIndex + "]: " + stDeviceList.get(camIndex).usb3VInfo.userDefinedName);
            }
            else
            {
   
   
                System.out.println("Device is not supported.");
            }
        }
        else
        {
   
   
            System.out.println("Invalid index " + camIndex);
            camIndex = -1;
        }

        return camIndex;
    }

	/**
	* 主方法
	* 保存照片的代码在这里
	*/
    public static void main(String[] args)
    {
   
   
        int nRet = MV_OK;
        int camIndex = -1;
        Handle hCamera = null;
        ArrayList<MV_CC_DEVICE_INFO> stDeviceList;

        do
        {
   
   
            System.out.println
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值