欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java web整合海康威视录像机摄像SDK

程序员文章站 2023-12-22 16:45:52
...

java 项目demo目录结构 java web整合海康威视录像机摄像SDK实现拍照功能。 一、maven项目结构

java web整合海康威视录像机摄像SDK

【提示】主要的是HCNetSDK.java文件,其中都是海康java调用dll文件获取摄像头资源的接口。 java web整合海康威视录像机摄像SDK

(1)库文件配置 java web整合海康威视录像机摄像SDK

如果是linux环境 将库文件放在/usr/lib64/目录下,并设置可执行权限。

java web整合海康威视录像机摄像SDK

1.拍照

 * 拍照功能
 * [@param](https://my.oschina.net/u/2303379) CameraInfoEntity cameraInfoEntity  设备初始化注册参数
 * [@return](https://my.oschina.net/u/556800) path   返回ftp文件路径
 * [@throws](https://my.oschina.net/throws) org.bytedeco.javacv.FrameRecorder.Exception
 * [@throws](https://my.oschina.net/throws) Exception
 */
public static String cameraTakePhoto(CameraInfoEntity cameraInfoEntity) throws Exception {
		//初始化设备
        long startTime = System.currentTimeMillis();
        HCNetSDK sdk = HCNetSDK.INSTANCE;
        if (!sdk.NET_DVR_Init()) {
           throw new RuntimeException("设备初始化获取失败");
        }
 
        NET_DVR_DEVICEINFO_V30 devinfo = new NET_DVR_DEVICEINFO_V30();
        
        //设备注册
        NativeLong id = sdk.NET_DVR_Login_V30(cameraInfoEntity.getCameraIP(), (short) cameraInfoEntity.getCameraPort(), cameraInfoEntity.getUserName(), cameraInfoEntity.getUserPwd(), devinfo);
        if (id.longValue() < 0) {
        	throw new RuntimeException("设备注册失败");
        } 
        System.out.println("设备注册成功,用户id" + id);
        NET_DVR_WORKSTATE_V30 devwork = new NET_DVR_WORKSTATE_V30();
        if (!sdk.NET_DVR_GetDVRWorkState_V30(id, devwork)) {
        	// 返回Boolean值,判断是否获取设备能力
        	throw new RuntimeException("返回设备状态失败");
        }
        startTime = System.currentTimeMillis();
        //截屏对象
        NET_DVR_JPEGPARA jpeg = new NET_DVR_JPEGPARA();
        // 设置图片的分辨率
        jpeg.wPicSize = 2;
        // 设置图片质量
        jpeg.wPicQuality = 2;
 
        IntByReference reference = new IntByReference();
        ByteBuffer jpegBuffer = ByteBuffer.allocate(1024 * 1024);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
       // String fileNameString = "D:/CapturePicture/"+sdf.format(date)+ "_"+ cameraInfoEntity.getChannel() +".jpg";
        String fileNameString = "cameracut_" + UUID.randomUUID().toString().replaceAll("-", "").toUpperCase() + sdf.format(new Date())+ "_"+ cameraInfoEntity.getChannel() +".jpg";
        //声明文件
        File file = new File(fileNameString);
        boolean is = sdk.NET_DVR_CaptureJPEGPicture_NEW(id, cameraInfoEntity.getChannel(), jpeg, jpegBuffer, 1024 * 1024, reference);
        System.out.println("拍照获取图片用时" + (System.currentTimeMillis() - startTime) + "ms]");
        System.out.println("拍照文件大小" + reference.getValue());
        BufferedOutputStream outputStream = null;
        try {
            outputStream = new BufferedOutputStream(new FileOutputStream(file));
            outputStream.write(jpegBuffer.array(), 0, reference.getValue());
            outputStream.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        
        InputStream inputStream = new BufferedInputStream(new FileInputStream(file));
        boolean upload = FTPUtils.ftpUpload("img.xxx.net", 21, "eGpZyE38rG0ExkGv", "Kq8J6GrTt1ngJ0HKeJ", "/vtsp/imgs", file.getName(), inputStream);
        System.out.println("图片上传ftp,状态===========>" + upload);
        
        //删除图片文件
        if(file.exists() && file.isFile()) {
        	file.deleteOnExit();
        }
        inputStream.close();
        
        sdk.NET_DVR_Logout(id);
        sdk.NET_DVR_Cleanup();
        
        if (is) {
    		return "http://"+ "ftp.xxx.net" + "/vtsp/imgs/" +file.getName();
        } else {
        	return null;
        }
}

在海康的HCNetSDK中加方法

 boolean NET_DVR_CaptureJPEGPicture_NEW(NativeLong lUserID, NativeLong lChannel, NET_DVR_JPEGPARA lpJpegPara, ByteBuffer sJpegPicBuffer, int dwPicSize, IntByReference lpSizeReturned);

转载于:https://my.oschina.net/kkgh/blog/2252293

上一篇:

下一篇: