1.SDK下载
下载网址:海康开放平台SDK下载地址
 注:根据需要操作系统下载对应SDK
 本文使用WIndows操作系统
2.海康Demo测试
1)IDEA打开项目ClientDemo
2)ClientDemo进行适当修改,留下加载SDK和NET_DVR_CaptureJPEGPicture方法测试
 
能够正常保存图片文件则DemoSDK可正常加载和使用。
3.迁移到java项目中使用
1).lib文件夹
 整个lib文件夹迁移至com同级目录
 
 2)接口和Common包移入项目
 
 3)本地maven仓库引入lib下的jna.jar、examples.jar包(打包时需要将这两个jar包打到war包中)
 项目的Maven Tab页
 
 Maven仓库引入jar包
mvn install:install-file -Dfile=examples.jar -DgroupId=com.example> -DartifactId=examples -Dversion=1.0.0 -Dpackaging=jar
mvn install:install-file -Dfile=examples.jar -DgroupId=com.example -DartifactId=examples -Dversion=1.0.0 -Dpackaging=jar
pom.xml文件配置:
<dependency>
    <groupId>com.example</groupId>
    <artifactId>jna</artifactId>
    <version>1.0.0</version>
</dependency>
<dependency>
    <groupId>com.example</groupId>
    <artifactId>examples</artifactId>
    <version>1.0.0</version>
</dependency>
 
配置插件打包lib文件夹
  <plugin>
            <!--拷贝资源文件 copy-resources-->
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>3.2.0</version>
            <executions>
                <execution>
                    <id>copy-resources</id>
                    <phase>package</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>
                        <resources>
                            <resource>
                                <!--打包前位置-->
                                <directory>${basedir}/lib</directory>
                            </resource>
                        </resources>
                        <!--打包后位置-->
                        <outputDirectory>${project.build.directory}/classes/lib</outputDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>
 
4)初始化SDK前需要加载dll动态库,路径需要满足测试环境和生产环境都支持
 使用下列代码获取dll文件路径,war包配置按照pom文件打包即可。Sbringboot启动和tomcat启动war包都可以直接引入。
 //win系统加载库路径
URL url = HikUtil.class.getResource("../../../../lib/HCNetSDK.dll");
strDllPath = url.getFile().substring(1).replace("/","\\");
 
5)根据ClientDemo.java使用SDK的方法或重新开发
 /**
     * 相机硬件抓图存储到内存
     *
     * @return Base64
     */
    public String PEGPicture_NEW() {
        HCNetSDK.NET_DVR_JPEGPARA  net_dvr_jpegpara = new HCNetSDK.NET_DVR_JPEGPARA();
        net_dvr_jpegpara.wPicQuality= 0;
        net_dvr_jpegpara.wPicSize = 0xff;
        int dwPicSize=1024*1024*10;
        // 分配内存
        Memory jpegPicBuffer = new Memory(dwPicSize);
        IntByReference IntByReference =new IntByReference();
        if (  hCNetSDK.NET_DVR_CaptureJPEGPicture_NEW(lUserID,1,net_dvr_jpegpara,jpegPicBuffer,dwPicSize,IntByReference))
        {
            System.out.println("抓图成功");
            byte[] jpegData = jpegPicBuffer.getByteArray(0, IntByReference.getValue());
            //转Base64
            return Base64.getEncoder().encodeToString(jpegData);
        }else{
            throw new SdkException("抓图失败,错误代码为:"+hCNetSDK.NET_DVR_GetLastError());
        }
    }
    /**
     * 相机硬件抓图存储到文件地址,需要图片名称
     *
     * @return
     */
    public void PEGPicture(String imagePath) {
        
        HCNetSDK.NET_DVR_JPEGPARA  net_dvr_jpegpara = new HCNetSDK.NET_DVR_JPEGPARA();
        net_dvr_jpegpara.wPicQuality= 0;
        net_dvr_jpegpara.wPicSize = 0xff;
        int dwPicSize=1024*1024*10;
//        抓图直接存储,且只能存储为jpeg格式,jpg格式会发生乱码
        byte[] sPicFileName;
        sPicFileName = imagePath.getBytes();
        if (hCNetSDK.NET_DVR_CaptureJPEGPicture(lUserID,  1, net_dvr_jpegpara,sPicFileName))
        {
            System.out.println("抓图成功");
        }else{
            System.out.println("抓图失败,错误代码为:"+hCNetSDK.NET_DVR_GetLastError());
        }
    }
                


















