DHController.java
@RestController
public class DHController {
Logger logger = LoggerFactory.getLogger(DHController.class);
@Autowired
HttpServletRequest request;
@RequestMapping(value = "/capture", method = RequestMethod.POST)
public JsonResponse capture(@RequestBody Map<String, String> params) {
JsonResponse jsonResponse;
String key = null;
NetSDKLib.LLong loginhandle = new NetSDKLib.LLong(0);
try {
String ip = params.get("ip");
String port = params.get("port");
String user = params.get("user");
String password = params.get("password");
String channel = params.get("channel");
String filename = params.get("filename");
boolean init = this.initClient();
if (!init) {
throw new Exception("客户端初始化失败");
}
CaptureReceiveCB captureReceiveCB = new CaptureReceiveCB(ip, filename);
loginhandle = this.login(ip, Integer.parseInt(port), user, password, captureReceiveCB);
if(loginhandle.longValue() == 0) {
throw new Exception("登录失败,用户名或密码不正确");
}
boolean resultCapture = this.capture(loginhandle, Integer.parseInt(channel), captureReceiveCB);
if(!resultCapture) {
throw new Exception("抓拍图片失败");
}
key = ip + loginhandle.longValue();
Semaphore semaphore = Cache.getSemaphore(key);
semaphore.tryAcquire(1, 5, TimeUnit.SECONDS);
String filepath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()+ request.getContextPath() + "/static/" + filename;
jsonResponse = JsonResponse.success("抓拍图片成功", filepath);
} catch (Exception e) {
jsonResponse = JsonResponse.error("抓拍图片失败:" + e.getMessage());
logger.error(e.getMessage(), e);
}finally {
if(key != null) {
Cache.removeSemaphore(key);
}
if(loginhandle.longValue() != 0) {
NetSDKLib.NETSDK_INSTANCE.CLIENT_Logout(loginhandle);
}
NetSDKLib.NETSDK_INSTANCE.CLIENT_Cleanup();
}
return jsonResponse;
}
private boolean initClient() {
DisConnect disConnect = new DisConnect();
return NetSDKLib.NETSDK_INSTANCE.CLIENT_Init(disConnect, null);
}
private NetSDKLib.LLong login(String ip, int port, String user, String password, CaptureReceiveCB captureReceiveCB) throws Exception {
Native.setCallbackThreadInitializer(captureReceiveCB,
new CallbackThreadInitializer(false, false, "snapPicture callback thread"));
NetSDKLib.NET_IN_LOGIN_WITH_HIGHLEVEL_SECURITY pstInParam = new NetSDKLib.NET_IN_LOGIN_WITH_HIGHLEVEL_SECURITY();
pstInParam.nPort = port;
pstInParam.szIP = ip.getBytes();
pstInParam.szPassword = password.getBytes();
pstInParam.szUserName = user.getBytes();
//出参
NetSDKLib.NET_DEVICEINFO_Ex m_stDeviceInfo = new NetSDKLib.NET_DEVICEINFO_Ex();
NetSDKLib.NET_OUT_LOGIN_WITH_HIGHLEVEL_SECURITY pstOutParam = new NetSDKLib.NET_OUT_LOGIN_WITH_HIGHLEVEL_SECURITY();
pstOutParam.stuDeviceInfo = m_stDeviceInfo;
return NetSDKLib.NETSDK_INSTANCE.CLIENT_LoginWithHighLevelSecurity(pstInParam, pstOutParam);
}
private boolean capture(NetSDKLib.LLong loginhandle, int channel, CaptureReceiveCB captureReceiveCB) throws Exception {
NetSDKLib.NETSDK_INSTANCE.CLIENT_SetSnapRevCallBack(captureReceiveCB, null);
// send caputre picture command to device
NetSDKLib.SNAP_PARAMS stuSnapParams = new NetSDKLib.SNAP_PARAMS();
stuSnapParams.Channel = channel; // channel
stuSnapParams.mode = 0; // capture picture mode
stuSnapParams.Quality = 3; // picture quality
stuSnapParams.InterSnap = 0; // timer capture picture time interval
stuSnapParams.CmdSerial = 0; // request serial
IntByReference reserved = new IntByReference(0);
return NetSDKLib.NETSDK_INSTANCE.CLIENT_SnapPictureEx(loginhandle, stuSnapParams, reserved);
}
}
CaptureReceiveCB.java
public class CaptureReceiveCB implements NetSDKLib.fSnapRev{
Logger logger = LoggerFactory.getLogger(CaptureReceiveCB.class);
String ip;
private String filename;
public CaptureReceiveCB(String ip, String filename) {
this.ip = ip;
this.filename = filename;
}
@Override
public void invoke(NetSDKLib.LLong lLoginID, Pointer pBuf, int RevLen, int EncodeType, int CmdSerial, Pointer dwUser) {
if(pBuf != null && RevLen > 0) {
try {
byte[] buf = pBuf.getByteArray(0, RevLen);
ByteArrayInputStream byteArrInput = new ByteArrayInputStream(buf);
BufferedImage bufferedImage = ImageIO.read(byteArrInput);
if(bufferedImage == null) {
return;
}
String path = this.getClass().getResource("/").getPath() + "/static/" + filename;
ImageIO.write(bufferedImage, "jpg", new File(path));
} catch (IOException e) {
logger.error("写文件失败:" + e.getMessage(), e);
}finally {
String key = ip + lLoginID.longValue();
Semaphore semaphore = Cache.getSemaphore(key);
semaphore.release();
}
}
}
}
Cache.java
public class Cache {
private static ConcurrentHashMap<String, Semaphore> semaphores = new ConcurrentHashMap<String, Semaphore>();
public static Semaphore getSemaphore(String key) {
Semaphore newSemaphore = new Semaphore(0);
Semaphore semaphore = semaphores.putIfAbsent(key, newSemaphore);
return semaphore == null ? newSemaphore : semaphore;
}
public static void removeSemaphore(String key) {
semaphores.remove(key);
}
}
DisConnect.java
public class DisConnect implements NetSDKLib.fDisConnect{
Logger logger = LoggerFactory.getLogger(DisConnect.class);
@Override
public void invoke(NetSDKLib.LLong lLong, String s, int i, Pointer pointer) {
logger.error("客户端失去连接");
}
}