CapturePictureModule.java 6.06 KB
package com.huaheng.api.erp.domain;

import com.huaheng.common.constant.QuantityConstant;
import com.huaheng.framework.config.HuaHengConfig;
import com.netsdk.demo.module.LoginModule;
import com.netsdk.lib.NetSDKLib;
import com.netsdk.lib.ToolKits;
import com.sun.jna.Pointer;
import com.sun.jna.ptr.IntByReference;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;

public class CapturePictureModule {

    public static boolean remoteCapturePicture(int chn, NetSDKLib.LLong m_hLoginHandle) {
        return snapPicture(chn, 0, 0,m_hLoginHandle);
    }

    /**
     * \if ENGLISH_LANG
     * Capture Picture (except local capture picture, others all call this interface)
     * \else
     * 抓图 (除本地抓图外, 其他全部调用此接口)
     * \endif
     */
    private static boolean snapPicture(int chn, int mode, int interval, NetSDKLib.LLong m_hLoginHandle) {
        // send caputre picture command to device
        NetSDKLib.SNAP_PARAMS stuSnapParams = new NetSDKLib.SNAP_PARAMS();
        stuSnapParams.Channel = chn;  			// channel
        stuSnapParams.mode = mode;    			// capture picture mode
        stuSnapParams.Quality = 3;				// picture quality
        stuSnapParams.InterSnap = interval; 	// timer capture picture time interval
        stuSnapParams.CmdSerial = 0;  			// request serial

        IntByReference reserved = new IntByReference(0);

        if (!LoginModule.netsdk.CLIENT_SnapPictureEx(m_hLoginHandle, stuSnapParams, reserved)) {
            System.err.printf("CLIENT_SnapPictureEx Failed!" + ToolKits.getErrorCodePrint());
            return false;
        } else {
            System.out.println("CLIENT_SnapPictureEx success");
        }
        return true;
    }
    public static final CarInCaptureReceiveCB m_CarInCaptureReceiveCB = new CarInCaptureReceiveCB();
    public static final CarOutCaptureReceiveCB m_CarOutCaptureReceiveCB = new CarOutCaptureReceiveCB();
    public static final MaterialCaptureReceiveCB m_MaterialCaptureReceiveCB = new MaterialCaptureReceiveCB();
    public static void setSnapRevCallBack(NetSDKLib.fSnapRev cbSnapReceive){
        LoginModule.netsdk.CLIENT_SetSnapRevCallBack(cbSnapReceive, null);
    }

    public static class CarInCaptureReceiveCB implements NetSDKLib.fSnapRev {
        @Override
        public void invoke(NetSDKLib.LLong lLoginID, Pointer pBuf, int RevLen, int EncodeType, int CmdSerial, Pointer dwUser) {
            if (pBuf != null && RevLen > 0) {
                String imgSavePath = QuantityConstant.CAR_IN_CAPTURE_IMAGE_ADDRESS;
                // 创建存储目录
                File path1 = new File(HuaHengConfig.getProfile()+"img/Capture");
                if (!path1.exists()) {
                    path1.mkdirs();
                }
                String imgPath = null;
                imgPath = HuaHengConfig.getProfile() + imgSavePath + ".jpg";

                // 将照片数据写入文件
                byte[] buf = pBuf.getByteArray(0, RevLen);
                ByteArrayInputStream byteArrInput = new ByteArrayInputStream(buf);
                try {
                    BufferedImage bufferedImage = ImageIO.read(byteArrInput);
                    if (bufferedImage == null) {
                        return;
                    }
                    ImageIO.write(bufferedImage, "jpg", new File(imgPath));//这里保存照片
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    public static class CarOutCaptureReceiveCB implements NetSDKLib.fSnapRev {
        @Override
        public void invoke(NetSDKLib.LLong lLoginID, Pointer pBuf, int RevLen, int EncodeType, int CmdSerial, Pointer dwUser) {
            if (pBuf != null && RevLen > 0) {
                String imgSavePath = QuantityConstant.CAR_OUT_CAPTURE_IMAGE_ADDRESS;
                // 创建存储目录
                File path1 = new File(HuaHengConfig.getProfile()+"img/Capture");
                if (!path1.exists()) {
                    path1.mkdirs();
                }
                String imgPath = null;
                imgPath = HuaHengConfig.getProfile() + imgSavePath + ".jpg";

                // 将照片数据写入文件
                byte[] buf = pBuf.getByteArray(0, RevLen);
                ByteArrayInputStream byteArrInput = new ByteArrayInputStream(buf);
                try {
                    BufferedImage bufferedImage = ImageIO.read(byteArrInput);
                    if (bufferedImage == null) {
                        return;
                    }
                    ImageIO.write(bufferedImage, "jpg", new File(imgPath));//这里保存照片
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static class MaterialCaptureReceiveCB implements NetSDKLib.fSnapRev {
        @Override
        public void invoke(NetSDKLib.LLong lLoginID, Pointer pBuf, int RevLen, int EncodeType, int CmdSerial, Pointer dwUser) {
            if (pBuf != null && RevLen > 0) {
                String imgSavePath = QuantityConstant.MATERIAL_CAPTURE_IMAGE_ADDRESS;
                // 创建存储目录
                File path1 = new File(HuaHengConfig.getProfile()+"img/Capture");
                if (!path1.exists()) {
                    path1.mkdirs();
                }
                String imgPath = null;
                imgPath = HuaHengConfig.getProfile() + imgSavePath + ".jpg";

                // 将照片数据写入文件
                byte[] buf = pBuf.getByteArray(0, RevLen);
                ByteArrayInputStream byteArrInput = new ByteArrayInputStream(buf);
                try {
                    BufferedImage bufferedImage = ImageIO.read(byteArrInput);
                    if (bufferedImage == null) {
                        return;
                    }
                    ImageIO.write(bufferedImage, "jpg", new File(imgPath));//这里保存照片
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}