Compare commits
6 Commits
2cb03b146a
...
18e014d9cb
| Author | SHA1 | Date | |
|---|---|---|---|
| 18e014d9cb | |||
|
|
7d48c78c9b | ||
|
|
c80dce6419 | ||
|
|
4802e11f7c | ||
|
|
fde2f18ff5 | ||
|
|
41e0b0b5b4 |
50
wms-webapi/Library/JavaEnv.bat
Normal file
50
wms-webapi/Library/JavaEnv.bat
Normal file
@@ -0,0 +1,50 @@
|
||||
:: 检查java安装环境
|
||||
|
||||
@echo off
|
||||
|
||||
:: 设置全局错误码
|
||||
set /A errorcode=0
|
||||
|
||||
:: 开启扩展功能
|
||||
setlocal ENABLEEXTENSIONS
|
||||
setlocal ENABLEDELAYEDEXPANSION
|
||||
|
||||
::通过向临时文件写内容,判断是否有文件读写权限
|
||||
echo > tmp
|
||||
if exist tmp (
|
||||
del /F/Q tmp >nul 2>&1
|
||||
) else (
|
||||
echo Please switch to an administrator account to run this batch!!!
|
||||
set /A errorcode=1
|
||||
goto:END
|
||||
)
|
||||
|
||||
::判断JAVA_HOME是否被定义
|
||||
if "%JAVA_HOME%"=="" (
|
||||
echo JAVA_HOME not set. Please make sure that java is correctly installed.
|
||||
set /A errorcode=2
|
||||
goto:END
|
||||
)
|
||||
|
||||
:: 判断CLASSPATH是否被定义
|
||||
if "%CLASSPATH%"=="" (
|
||||
echo CLASSPATH not set. Please make sure that java is correctly installed.
|
||||
set /A errorcode=3
|
||||
goto:END
|
||||
)
|
||||
|
||||
:: 判断java版本是否比 1.7.0高,需先将版本信息重定向到文件中,再对文件内容进行分析
|
||||
java -version >nul 2> JavaVer.tmp
|
||||
for /F "tokens=1,2,3*" %%i in (JavaVer.tmp) do (
|
||||
if "%%j" == "version" (
|
||||
if %%k LSS "1.7.0" (
|
||||
echo Java version is less than "1.7.0", warnings or errors may occur.
|
||||
)
|
||||
)
|
||||
)
|
||||
del /F/Q JavaVer.tmp >nul 2>&1
|
||||
|
||||
:END
|
||||
|
||||
::返回错误码
|
||||
exit /B %errorcode%
|
||||
BIN
wms-webapi/Library/MvCameraControlWrapper.jar
Normal file
BIN
wms-webapi/Library/MvCameraControlWrapper.jar
Normal file
Binary file not shown.
@@ -74,12 +74,21 @@
|
||||
<version>2.9.2</version>
|
||||
</dependency>
|
||||
|
||||
<!-- pom.xml -->
|
||||
<!-- WebSocket API -->
|
||||
<dependency>
|
||||
<groupId>jakarta.websocket</groupId>
|
||||
<artifactId>jakarta.websocket-api</artifactId>
|
||||
<version>2.1.0</version>
|
||||
</dependency>
|
||||
|
||||
<!-- 本地库:MvCameraControlWrapper -->
|
||||
<dependency>
|
||||
<groupId>com.hikvision</groupId>
|
||||
<artifactId>MvCameraControlWrapper</artifactId>
|
||||
<version>1.0</version>
|
||||
<scope>system</scope>
|
||||
<systemPath>${project.basedir}/Library/MvCameraControlWrapper.jar</systemPath>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
||||
@@ -1,17 +1,25 @@
|
||||
package top.wms.admin.config.webSocket;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.socket.config.annotation.EnableWebSocket;
|
||||
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
|
||||
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
|
||||
import top.wms.admin.controller.hkMVS.CameraWebSocketHandler;
|
||||
import top.wms.admin.controller.weighManage.ah.ScaleWebSocketHandler;
|
||||
|
||||
@Configuration
|
||||
@EnableWebSocket
|
||||
public class webSocket implements WebSocketConfigurer {
|
||||
public class WebSocketConfig implements WebSocketConfigurer {
|
||||
|
||||
@Autowired
|
||||
private CameraWebSocketHandler cameraWebSocketHandler;
|
||||
|
||||
@Override
|
||||
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
|
||||
// 注册WebSocket端点,允许所有跨域请求
|
||||
registry.addHandler(new ScaleWebSocketHandler(), "/ws/scale").setAllowedOrigins("*");
|
||||
// 注册海康相机1 WebSocket端点
|
||||
registry.addHandler(cameraWebSocketHandler, "/ws/camera").setAllowedOrigins("*");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,487 @@
|
||||
package top.wms.admin.controller.hkMVS;
|
||||
|
||||
import MvCameraControlWrapper.*;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.socket.BinaryMessage;
|
||||
import org.springframework.web.socket.WebSocketSession;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import static MvCameraControlWrapper.MvCameraControlDefines.*;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
public class CameraService {
|
||||
|
||||
// 相机句柄
|
||||
private Handle hCamera = null;
|
||||
// 设备信息列表
|
||||
private ArrayList<MV_CC_DEVICE_INFO> stDeviceList = null;
|
||||
// WebSocket会话映射,用于存储活跃的WebSocket连接
|
||||
private final Map<String, WebSocketSession> webSocketSessions = new ConcurrentHashMap<>();
|
||||
// 相机是否正在运行
|
||||
private boolean isRunning = false;
|
||||
// 帧率控制相关
|
||||
private long lastFrameTime = 0;
|
||||
private final int targetFps = 15; // 目标帧率
|
||||
private final long frameInterval = 1000 / targetFps; // 帧间隔(毫秒)
|
||||
// 最后一次会话移除时间,用于判断是否需要保留相机资源
|
||||
private long lastSessionRemoveTime = 0;
|
||||
// 相机资源保留时间(毫秒)
|
||||
private final long cameraResourceRetentionTime = 5000; // 5秒
|
||||
|
||||
/**
|
||||
* 初始化相机
|
||||
* @return 初始化结果
|
||||
*/
|
||||
public synchronized boolean initializeCamera() {
|
||||
if (hCamera != null) {
|
||||
log.info("Camera already initialized");
|
||||
return true;
|
||||
}
|
||||
|
||||
int nRet = MV_OK;
|
||||
|
||||
try {
|
||||
// 初始化SDK
|
||||
log.info("Initializing SDK...");
|
||||
nRet = MvCameraControl.MV_CC_Initialize();
|
||||
if (MV_OK != nRet) {
|
||||
log.error("Initialize SDK fail! nRet {}", Integer.toHexString(nRet));
|
||||
return false;
|
||||
}
|
||||
|
||||
// 枚举设备
|
||||
log.info("Enumerating devices...");
|
||||
try {
|
||||
stDeviceList = MvCameraControl.MV_CC_EnumDevices(MV_GIGE_DEVICE | MV_USB_DEVICE | MV_GENTL_GIGE_DEVICE | MV_GENTL_CAMERALINK_DEVICE | MV_GENTL_CXP_DEVICE | MV_GENTL_XOF_DEVICE);
|
||||
} catch (CameraControlException e) {
|
||||
log.error("Enumerate devices failed!", e);
|
||||
MvCameraControl.MV_CC_Finalize();
|
||||
return false;
|
||||
}
|
||||
|
||||
if (stDeviceList == null || stDeviceList.size() <= 0) {
|
||||
log.error("No devices found!");
|
||||
MvCameraControl.MV_CC_Finalize();
|
||||
return false;
|
||||
}
|
||||
|
||||
// 打印设备信息
|
||||
log.info("Found {} device(s):", stDeviceList.size());
|
||||
int i = 0;
|
||||
for (MV_CC_DEVICE_INFO stDeviceInfo : stDeviceList) {
|
||||
if (null == stDeviceInfo) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// 默认选择第一个设备
|
||||
log.info("Selecting first device...");
|
||||
try {
|
||||
hCamera = MvCameraControl.MV_CC_CreateHandle(stDeviceList.get(0));
|
||||
} catch (CameraControlException e) {
|
||||
log.error("Create handle failed!", e);
|
||||
MvCameraControl.MV_CC_Finalize();
|
||||
return false;
|
||||
}
|
||||
|
||||
// 打开设备
|
||||
log.info("Opening device...");
|
||||
nRet = MvCameraControl.MV_CC_OpenDevice(hCamera);
|
||||
if (MV_OK != nRet) {
|
||||
log.error("Connect to camera failed, errcode: {}", Integer.toHexString(nRet));
|
||||
MvCameraControl.MV_CC_DestroyHandle(hCamera);
|
||||
hCamera = null;
|
||||
MvCameraControl.MV_CC_Finalize();
|
||||
return false;
|
||||
}
|
||||
|
||||
// 关闭触发模式
|
||||
log.info("Turning off trigger mode...");
|
||||
nRet = MvCameraControl.MV_CC_SetEnumValueByString(hCamera, "TriggerMode", "Off");
|
||||
if (MV_OK != nRet) {
|
||||
log.error("SetTriggerMode failed, errcode: {}", Integer.toHexString(nRet));
|
||||
closeCamera();
|
||||
return false;
|
||||
}
|
||||
|
||||
// 设置Bayer转换质量,用于彩色相机
|
||||
log.info("Setting Bayer convert quality...");
|
||||
nRet = MvCameraControl.MV_CC_SetBayerCvtQuality(hCamera, 1);
|
||||
if (MV_OK != nRet) {
|
||||
log.error("Set Bayer convert quality fail! nRet {}", Integer.toHexString(nRet));
|
||||
closeCamera();
|
||||
return false;
|
||||
}
|
||||
|
||||
log.info("Camera initialized successfully");
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
log.error("Initialize camera failed", e);
|
||||
closeCamera();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 开始视频流
|
||||
* @return 开始结果
|
||||
*/
|
||||
public synchronized boolean startStream() {
|
||||
if (hCamera == null) {
|
||||
log.error("Camera not initialized");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (isRunning) {
|
||||
log.info("Stream already running");
|
||||
return true;
|
||||
}
|
||||
|
||||
try {
|
||||
// 注册图像回调
|
||||
log.info("Registering image callback...");
|
||||
int nRet = MvCameraControl.MV_CC_RegisterImageCallBack(hCamera, new CameraImageCallBack() {
|
||||
@Override
|
||||
public int OnImageCallBack(byte[] bytes, MV_FRAME_OUT_INFO mv_frame_out_info) {
|
||||
processImage(bytes, mv_frame_out_info);
|
||||
// processBlackWhiteImage(bytes, mv_frame_out_info);
|
||||
return 0;
|
||||
}
|
||||
});
|
||||
if (MV_OK != nRet) {
|
||||
log.error("Register image callback failed, errcode: {}", Integer.toHexString(nRet));
|
||||
return false;
|
||||
}
|
||||
|
||||
// 开始采集
|
||||
log.info("Starting grabbing...");
|
||||
nRet = MvCameraControl.MV_CC_StartGrabbing(hCamera);
|
||||
if (MV_OK != nRet) {
|
||||
log.error("StartGrabbing failed, errcode: {}", Integer.toHexString(nRet));
|
||||
return false;
|
||||
}
|
||||
|
||||
isRunning = true;
|
||||
log.info("Stream started successfully");
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
log.error("Start stream failed", e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 停止视频流
|
||||
*/
|
||||
public synchronized void stopStream() {
|
||||
if (hCamera == null || !isRunning) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
// 停止采集
|
||||
log.info("Stopping grabbing...");
|
||||
int nRet = MvCameraControl.MV_CC_StopGrabbing(hCamera);
|
||||
if (MV_OK != nRet) {
|
||||
log.error("StopGrabbing failed, errcode: {}", Integer.toHexString(nRet));
|
||||
}
|
||||
|
||||
// close device
|
||||
nRet = MvCameraControl.MV_CC_CloseDevice(hCamera);
|
||||
if (MV_OK != nRet) {
|
||||
|
||||
log.error("CloseDevice failed, errcode: {}", Integer.toHexString(nRet));
|
||||
}
|
||||
|
||||
if (null != hCamera) {
|
||||
// Destroy handle
|
||||
nRet = MvCameraControl.MV_CC_DestroyHandle(hCamera);
|
||||
if (MV_OK != nRet) {
|
||||
log.error("DestroyHandle failed, errcode: {}", Integer.toHexString(nRet));
|
||||
}
|
||||
hCamera = null;
|
||||
}
|
||||
MvCameraControl.MV_CC_Finalize();
|
||||
|
||||
isRunning = false;
|
||||
log.info("Stream stopped");
|
||||
} catch (Exception e) {
|
||||
log.error("Stop stream failed", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 关闭相机
|
||||
*/
|
||||
public synchronized void closeCamera() {
|
||||
if (hCamera != null) {
|
||||
try {
|
||||
// 停止采集
|
||||
log.info("Stopping grabbing...");
|
||||
MvCameraControl.MV_CC_StopGrabbing(hCamera);
|
||||
// 关闭设备
|
||||
log.info("Closing device...");
|
||||
MvCameraControl.MV_CC_CloseDevice(hCamera);
|
||||
// 销毁句柄
|
||||
log.info("Destroying handle...");
|
||||
MvCameraControl.MV_CC_DestroyHandle(hCamera);
|
||||
} catch (Exception e) {
|
||||
log.error("Close camera failed", e);
|
||||
} finally {
|
||||
hCamera = null;
|
||||
}
|
||||
}
|
||||
|
||||
// 释放SDK
|
||||
try {
|
||||
log.info("Finalizing SDK...");
|
||||
MvCameraControl.MV_CC_Finalize();
|
||||
} catch (Exception e) {
|
||||
log.error("Finalize SDK failed", e);
|
||||
}
|
||||
|
||||
isRunning = false;
|
||||
log.info("Camera closed");
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理图像数据
|
||||
*
|
||||
* @param bytes 原始图像数据
|
||||
* @param frameInfo 帧信息
|
||||
*/
|
||||
private void processImage(byte[] bytes, MV_FRAME_OUT_INFO frameInfo) {
|
||||
if (bytes == null || frameInfo == null || webSocketSessions.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
// 帧率控制
|
||||
long currentTime = System.currentTimeMillis();
|
||||
if (currentTime - lastFrameTime < frameInterval) {
|
||||
return; // 跳过当前帧,控制帧率
|
||||
}
|
||||
lastFrameTime = currentTime;
|
||||
|
||||
int dataSizeForRGB = frameInfo.width * frameInfo.height * 3; // RGB每个像素3字节
|
||||
byte[] pDataForRGB = new byte[dataSizeForRGB];
|
||||
|
||||
// 初始化像素转换参数
|
||||
MV_CC_PIXEL_CONVERT_PARAM_EX stConvertParam = new MV_CC_PIXEL_CONVERT_PARAM_EX();
|
||||
|
||||
// 使用实际的width和height,避免ExtendHeight为0的问题
|
||||
stConvertParam.width = frameInfo.width;
|
||||
stConvertParam.height = frameInfo.height;
|
||||
stConvertParam.srcData = bytes;
|
||||
stConvertParam.srcDataLen = frameInfo.frameLen;
|
||||
stConvertParam.srcPixelType = frameInfo.pixelType;
|
||||
stConvertParam.dstPixelType = MvGvspPixelType.PixelType_Gvsp_RGB8_Packed;
|
||||
stConvertParam.dstBuffer = pDataForRGB;
|
||||
stConvertParam.dstBufferSize = dataSizeForRGB;
|
||||
|
||||
// 尝试进行像素格式转换
|
||||
int nRet = MvCameraControl.MV_CC_ConvertPixelTypeEx(hCamera, stConvertParam);
|
||||
if (MV_OK != nRet) {
|
||||
log.error("Convert PixelType fail, errCode: {}, srcPixelType: {}, width: {}, height: {}",
|
||||
Integer.toHexString(nRet),
|
||||
frameInfo.pixelType.getnValue(),
|
||||
frameInfo.width,
|
||||
frameInfo.height);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// 将RGB数据转换为BufferedImage
|
||||
BufferedImage image = new BufferedImage(frameInfo.width, frameInfo.height, BufferedImage.TYPE_3BYTE_BGR);
|
||||
image.getRaster().setDataElements(0, 0, frameInfo.width, frameInfo.height, pDataForRGB);
|
||||
|
||||
// 压缩图像为JPEG格式
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
ImageIO.write(image, "jpeg", baos);
|
||||
byte[] compressedData = baos.toByteArray();
|
||||
baos.close();
|
||||
|
||||
log.debug("Original size: {} bytes, Converted size: {} bytes, Compressed size: {} bytes, Compression ratio: {:.2f}%",
|
||||
bytes.length, pDataForRGB.length, compressedData.length, (1 - (double) compressedData.length / bytes.length) * 100);
|
||||
|
||||
// 创建包含图像尺寸信息和压缩数据的消息
|
||||
// 消息格式: [width(4 bytes)][height(4 bytes)][image data]
|
||||
byte[] message = new byte[8 + compressedData.length];
|
||||
|
||||
// 写入宽度和高度(使用小端序)
|
||||
message[0] = (byte) (frameInfo.width & 0xFF);
|
||||
message[1] = (byte) ((frameInfo.width >> 8) & 0xFF);
|
||||
message[2] = (byte) ((frameInfo.width >> 16) & 0xFF);
|
||||
message[3] = (byte) ((frameInfo.width >> 24) & 0xFF);
|
||||
|
||||
message[4] = (byte) (frameInfo.height & 0xFF);
|
||||
message[5] = (byte) ((frameInfo.height >> 8) & 0xFF);
|
||||
message[6] = (byte) ((frameInfo.height >> 16) & 0xFF);
|
||||
message[7] = (byte) ((frameInfo.height >> 24) & 0xFF);
|
||||
|
||||
// 写入压缩后的图像数据
|
||||
System.arraycopy(compressedData, 0, message, 8, compressedData.length);
|
||||
|
||||
// 发送图像数据到所有连接的WebSocket客户端
|
||||
for (WebSocketSession session : webSocketSessions.values()) {
|
||||
if (session.isOpen()) {
|
||||
try {
|
||||
session.sendMessage(new BinaryMessage(message));
|
||||
} catch (Exception e) {
|
||||
log.error("Send message to WebSocket failed", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("Process image failed", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 处理黑白相机图像数据
|
||||
* @param bytes 原始图像数据
|
||||
* @param frameInfo 帧信息
|
||||
*/
|
||||
private void processBlackWhiteImage(byte[] bytes, MV_FRAME_OUT_INFO frameInfo) {
|
||||
if (bytes == null || frameInfo == null || webSocketSessions.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
// 帧率控制
|
||||
long currentTime = System.currentTimeMillis();
|
||||
if (currentTime - lastFrameTime < frameInterval) {
|
||||
return; // 跳过当前帧,控制帧率
|
||||
}
|
||||
lastFrameTime = currentTime;
|
||||
|
||||
// 对于黑白相机(MV-CE050-30GM),数据是单通道灰度格式
|
||||
// 将原始灰度数据转换为BufferedImage
|
||||
BufferedImage image = new BufferedImage(frameInfo.width, frameInfo.height, BufferedImage.TYPE_BYTE_GRAY);
|
||||
image.getRaster().setDataElements(0, 0, frameInfo.width, frameInfo.height, bytes);
|
||||
|
||||
// 压缩图像为JPEG格式
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
ImageIO.write(image, "jpeg", baos);
|
||||
byte[] compressedData = baos.toByteArray();
|
||||
baos.close();
|
||||
|
||||
log.debug("Original size: {} bytes, Compressed size: {} bytes, Compression ratio: {:.2f}%",
|
||||
bytes.length, compressedData.length, (1 - (double)compressedData.length / bytes.length) * 100);
|
||||
|
||||
// 创建包含图像尺寸信息和压缩数据的消息
|
||||
// 消息格式: [width(4 bytes)][height(4 bytes)][image data]
|
||||
byte[] message = new byte[8 + compressedData.length];
|
||||
|
||||
// 写入宽度和高度(使用小端序)
|
||||
message[0] = (byte) (frameInfo.width & 0xFF);
|
||||
message[1] = (byte) ((frameInfo.width >> 8) & 0xFF);
|
||||
message[2] = (byte) ((frameInfo.width >> 16) & 0xFF);
|
||||
message[3] = (byte) ((frameInfo.width >> 24) & 0xFF);
|
||||
|
||||
message[4] = (byte) (frameInfo.height & 0xFF);
|
||||
message[5] = (byte) ((frameInfo.height >> 8) & 0xFF);
|
||||
message[6] = (byte) ((frameInfo.height >> 16) & 0xFF);
|
||||
message[7] = (byte) ((frameInfo.height >> 24) & 0xFF);
|
||||
|
||||
// 写入压缩后的图像数据
|
||||
System.arraycopy(compressedData, 0, message, 8, compressedData.length);
|
||||
|
||||
// 发送图像数据到所有连接的WebSocket客户端
|
||||
for (WebSocketSession session : webSocketSessions.values()) {
|
||||
if (session.isOpen()) {
|
||||
try {
|
||||
session.sendMessage(new BinaryMessage(message));
|
||||
} catch (Exception e) {
|
||||
log.error("Send message to WebSocket failed", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("Process image failed", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 添加WebSocket会话
|
||||
* @param session WebSocket会话
|
||||
*/
|
||||
public void addWebSocketSession(WebSocketSession session) {
|
||||
if (session != null) {
|
||||
webSocketSessions.put(session.getId(), session);
|
||||
log.info("WebSocket session added: {}", session.getId());
|
||||
|
||||
// 确保相机已初始化
|
||||
if (!initializeCamera()) {
|
||||
log.error("Failed to initialize camera for new WebSocket session");
|
||||
return;
|
||||
}
|
||||
|
||||
// 确保视频流已开始
|
||||
if (!isRunning) {
|
||||
boolean started = startStream();
|
||||
if (!started) {
|
||||
log.error("Failed to start stream for new WebSocket session");
|
||||
}
|
||||
} else {
|
||||
log.info("Stream already running, no need to start again");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除WebSocket会话
|
||||
* @param session WebSocket会话
|
||||
*/
|
||||
public void removeWebSocketSession(WebSocketSession session) {
|
||||
if (session != null) {
|
||||
webSocketSessions.remove(session.getId());
|
||||
log.info("WebSocket session removed: {}", session.getId());
|
||||
|
||||
// 如果没有活跃的WebSocket会话,记录移除时间
|
||||
if (webSocketSessions.isEmpty()) {
|
||||
lastSessionRemoveTime = System.currentTimeMillis();
|
||||
// 启动一个线程,延迟停止视频流,以便页面刷新时能够快速恢复
|
||||
new Thread(() -> {
|
||||
try {
|
||||
Thread.sleep(cameraResourceRetentionTime);
|
||||
// 再次检查是否仍然没有活跃会话
|
||||
if (webSocketSessions.isEmpty()) {
|
||||
stopStream();
|
||||
log.info("No active sessions after retention period, stopped stream");
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
log.error("Interrupted while waiting to stop stream", e);
|
||||
}
|
||||
}).start();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前连接的WebSocket会话数量
|
||||
* @return 会话数量
|
||||
*/
|
||||
public int getWebSocketSessionCount() {
|
||||
return webSocketSessions.size();
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查相机是否正在运行
|
||||
* @return 是否正在运行
|
||||
*/
|
||||
public boolean isRunning() {
|
||||
return isRunning;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package top.wms.admin.controller.hkMVS;
|
||||
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.socket.CloseStatus;
|
||||
import org.springframework.web.socket.WebSocketSession;
|
||||
import org.springframework.web.socket.handler.BinaryWebSocketHandler;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
public class CameraWebSocketHandler extends BinaryWebSocketHandler {
|
||||
|
||||
@Resource
|
||||
private CameraService cameraService;
|
||||
|
||||
/**
|
||||
* 连接建立时触发
|
||||
*/
|
||||
@Override
|
||||
public void afterConnectionEstablished(WebSocketSession session) throws Exception {
|
||||
log.info("WebSocket connection established: {}", session.getId());
|
||||
// 添加WebSocket会话到CameraService
|
||||
cameraService.addWebSocketSession(session);
|
||||
}
|
||||
|
||||
/**
|
||||
* 连接关闭时触发
|
||||
*/
|
||||
@Override
|
||||
public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
|
||||
log.info("WebSocket connection closed: {}, status: {}", session.getId(), status);
|
||||
// 从CameraService移除WebSocket会话
|
||||
cameraService.removeWebSocketSession(session);
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理二进制消息
|
||||
*/
|
||||
@Override
|
||||
protected void handleBinaryMessage(WebSocketSession session, org.springframework.web.socket.BinaryMessage message) throws Exception {
|
||||
// 可以在这里处理前端发送的二进制消息(如果需要)
|
||||
log.debug("Received binary message from WebSocket session: {}", session.getId());
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理文本消息
|
||||
*/
|
||||
@Override
|
||||
protected void handleTextMessage(WebSocketSession session, org.springframework.web.socket.TextMessage message) {
|
||||
// 可以在这里处理前端发送的文本消息(如果需要)
|
||||
log.debug("Received text message from WebSocket session: {}, message: {}", session.getId(), message.getPayload());
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理传输错误
|
||||
*/
|
||||
@Override
|
||||
public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {
|
||||
log.error("WebSocket transport error: {}", exception.getMessage(), exception);
|
||||
// 从CameraService移除WebSocket会话
|
||||
cameraService.removeWebSocketSession(session);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,370 @@
|
||||
package top.wms.admin.controller.hkMVS; /***************************************************************************************************
|
||||
* @file ConvertPixelType.java
|
||||
* @breif Use functions provided in MvCameraControlWrapper.jar to convert pixel type。
|
||||
* @author zhanglei72
|
||||
* @date 2020/02/10
|
||||
*
|
||||
* @warning
|
||||
* @version V1.0.0 2020/02/10 create this file.
|
||||
* @since 2020/02/10
|
||||
**************************************************************************************************/
|
||||
|
||||
import MvCameraControlWrapper.CameraControlException;
|
||||
import MvCameraControlWrapper.MvCameraControl;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Scanner;
|
||||
|
||||
import static MvCameraControlWrapper.MvCameraControlDefines.*;
|
||||
|
||||
public class ConvertPixelType
|
||||
{
|
||||
public static Scanner scanner;
|
||||
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) ||( stDeviceInfo.transportLayerType == MV_GENTL_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 if (stDeviceInfo.transportLayerType == MV_GENTL_CAMERALINK_DEVICE)
|
||||
{
|
||||
System.out.println("\tUserDefinedName: " + stDeviceInfo.cmlInfo.userDefinedName);
|
||||
System.out.println("\tSerial Number: " + stDeviceInfo.cmlInfo.serialNumber);
|
||||
System.out.println("\tDevice Number: " + stDeviceInfo.cmlInfo.DeviceID);
|
||||
}
|
||||
else if (stDeviceInfo.transportLayerType == MV_GENTL_CXP_DEVICE)
|
||||
{
|
||||
System.out.println("\tUserDefinedName: " + stDeviceInfo.cxpInfo.userDefinedName);
|
||||
System.out.println("\tSerial Number: " + stDeviceInfo.cxpInfo.serialNumber);
|
||||
System.out.println("\tDevice Number: " + stDeviceInfo.cxpInfo.DeviceID);
|
||||
}
|
||||
else if (stDeviceInfo.transportLayerType == MV_GENTL_XOF_DEVICE)
|
||||
{
|
||||
System.out.println("\tUserDefinedName: " + stDeviceInfo.xofInfo.userDefinedName);
|
||||
System.out.println("\tSerial Number: " + stDeviceInfo.xofInfo.serialNumber);
|
||||
System.out.println("\tDevice Number: " + stDeviceInfo.xofInfo.DeviceID);
|
||||
}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
|
||||
{
|
||||
if((null == dataToSave)||(dataSize <= 0))
|
||||
{
|
||||
System.out.println("saveDataToFile param error.");
|
||||
return;
|
||||
}
|
||||
// create diractory
|
||||
File tempFile = new File("dat");
|
||||
if (!tempFile.exists())
|
||||
{
|
||||
tempFile.mkdirs();
|
||||
}
|
||||
|
||||
os = new FileOutputStream(tempFile.getPath() + File.separator + fileName);
|
||||
if(null != os)
|
||||
{
|
||||
os.write(dataToSave, 0, dataSize);
|
||||
System.out.println("ConvertPixelType succeed.");
|
||||
}
|
||||
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
finally
|
||||
{
|
||||
// close file stream
|
||||
try
|
||||
{
|
||||
if(os != null)
|
||||
{
|
||||
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;
|
||||
|
||||
while (true) {
|
||||
try {
|
||||
camIndex = 0;
|
||||
if ((camIndex >= 0 && camIndex < stDeviceList.size()) || -1 == camIndex) {
|
||||
break;
|
||||
} else {
|
||||
System.out.println("Input error: " + camIndex + " Over Range:( 0 - " + (stDeviceList.size() - 1) + " )");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.out.println("Input not number.");
|
||||
camIndex = -1;
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
if (-1 == camIndex) {
|
||||
System.out.println("Input error.exit");
|
||||
return camIndex;
|
||||
}
|
||||
|
||||
if (0 <= camIndex && stDeviceList.size() > camIndex)
|
||||
{
|
||||
if ((MV_GIGE_DEVICE == stDeviceList.get(camIndex).transportLayerType)||(MV_GENTL_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 if (MV_GENTL_CAMERALINK_DEVICE == stDeviceList.get(camIndex).transportLayerType)
|
||||
{
|
||||
System.out.println("Connect to camera[" + camIndex + "]: " + stDeviceList.get(camIndex).cmlInfo.DeviceID);
|
||||
}
|
||||
else if (MV_GENTL_CXP_DEVICE == stDeviceList.get(camIndex).transportLayerType)
|
||||
{
|
||||
System.out.println("Connect to camera[" + camIndex + "]: " + stDeviceList.get(camIndex).cxpInfo.DeviceID);
|
||||
}
|
||||
else if (MV_GENTL_XOF_DEVICE == stDeviceList.get(camIndex).transportLayerType)
|
||||
{
|
||||
System.out.println("Connect to camera[" + camIndex + "]: " + stDeviceList.get(camIndex).xofInfo.DeviceID);
|
||||
}
|
||||
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;
|
||||
scanner = new Scanner(System.in);
|
||||
ArrayList<MV_CC_DEVICE_INFO> stDeviceList = null;
|
||||
|
||||
do
|
||||
{
|
||||
System.out.println("SDK Version " + MvCameraControl.MV_CC_GetSDKVersion());
|
||||
|
||||
// Initialize SDK
|
||||
nRet = MvCameraControl.MV_CC_Initialize();
|
||||
if (MV_OK != nRet)
|
||||
{
|
||||
System.err.printf("Initialize SDK fail! nRet [0x%x]\n\n",nRet);
|
||||
break;
|
||||
}
|
||||
|
||||
// Enumerate GigE and USB devices
|
||||
try
|
||||
{
|
||||
stDeviceList = MvCameraControl.MV_CC_EnumDevices(MV_GIGE_DEVICE | MV_USB_DEVICE | MV_GENTL_GIGE_DEVICE | MV_GENTL_CAMERALINK_DEVICE | MV_GENTL_CXP_DEVICE | MV_GENTL_XOF_DEVICE);
|
||||
if (0 >= stDeviceList.size())
|
||||
{
|
||||
System.out.println("No devices found!");
|
||||
break;
|
||||
}
|
||||
int i = 0;
|
||||
for (MV_CC_DEVICE_INFO stDeviceInfo : stDeviceList)
|
||||
{
|
||||
System.out.println("[camera " + (i++) + "]");
|
||||
printDeviceInfo(stDeviceInfo);
|
||||
}
|
||||
}
|
||||
catch (CameraControlException e)
|
||||
{
|
||||
System.err.println("Enumrate devices failed!" + e.toString());
|
||||
e.printStackTrace();
|
||||
break;
|
||||
}
|
||||
|
||||
// choose camera
|
||||
camIndex = chooseCamera(stDeviceList);
|
||||
if (camIndex == -1)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
// Create device handle
|
||||
try
|
||||
{
|
||||
hCamera = MvCameraControl.MV_CC_CreateHandle(stDeviceList.get(camIndex));
|
||||
}
|
||||
catch (CameraControlException e)
|
||||
{
|
||||
System.err.println("Create handle failed!" + e.toString());
|
||||
e.printStackTrace();
|
||||
hCamera = null;
|
||||
break;
|
||||
}
|
||||
|
||||
// Open selected device
|
||||
nRet = MvCameraControl.MV_CC_OpenDevice(hCamera);
|
||||
if (MV_OK != nRet)
|
||||
{
|
||||
System.err.printf("Connect to camera failed, errcode: [%#x]\n", nRet);
|
||||
break;
|
||||
}
|
||||
|
||||
// make sure that Trigger mode is off
|
||||
nRet = MvCameraControl.MV_CC_SetEnumValueByString(hCamera, "TriggerMode", "Off");
|
||||
if (MV_OK != nRet)
|
||||
{
|
||||
System.err.printf("SetTriggerMode failed, errcode: [%#x]\n", nRet);
|
||||
break;
|
||||
}
|
||||
|
||||
// Get payload size
|
||||
MVCC_INTVALUE stParam = new MVCC_INTVALUE();
|
||||
nRet = MvCameraControl.MV_CC_GetIntValue(hCamera, "PayloadSize", stParam);
|
||||
if (MV_OK != nRet)
|
||||
{
|
||||
System.err.printf("Get PayloadSize fail, errcode: [%#x]\n", nRet);
|
||||
break;
|
||||
}
|
||||
|
||||
// Start grabbing images
|
||||
nRet = MvCameraControl.MV_CC_StartGrabbing(hCamera);
|
||||
if (MV_OK != nRet)
|
||||
{
|
||||
System.err.printf("Start Grabbing fail, errcode: [%#x]\n", nRet);
|
||||
break;
|
||||
}
|
||||
|
||||
// Get one frame
|
||||
MV_FRAME_OUT_INFO stImageInfo = new MV_FRAME_OUT_INFO();
|
||||
byte[] pData = new byte[(int)stParam.curValue];
|
||||
nRet = MvCameraControl.MV_CC_GetOneFrameTimeout(hCamera, pData, stImageInfo, 1000);
|
||||
if (MV_OK != nRet)
|
||||
{
|
||||
System.err.printf("GetOneFrameTimeout fail, errcode:[%#x]\n", nRet);
|
||||
break;
|
||||
}
|
||||
|
||||
System.out.println("GetOneFrame: ");
|
||||
printFrameInfo(stImageInfo);
|
||||
|
||||
// set interpolation algorithm type, 0-Fast 1-Equilibrium 2-Optimal 3-Optimal plus
|
||||
nRet = MvCameraControl.MV_CC_SetBayerCvtQuality(hCamera, 1);
|
||||
if (MV_OK != nRet)
|
||||
{
|
||||
System.err.printf("set Bayer convert quality fail! nRet [0x%x]\n", nRet);
|
||||
break;
|
||||
}
|
||||
System.out.println("临时日志: frameInfo.width = " + stImageInfo.width + ", frameInfo.height = " + stImageInfo.height + ", frameInfo.ExtendWidth = " + stImageInfo.ExtendWidth + ", frameInfo.ExtendHeight = " + stImageInfo.ExtendHeight + ", frameInfo.frameLen = " + stImageInfo.frameLen + ", frameInfo.pixelType = " + stImageInfo.pixelType);
|
||||
int dataSizeForRGB = stImageInfo.width * stImageInfo.height * 3; // every RGB pixel takes 3 bytes
|
||||
byte[] pDataForRGB = new byte[dataSizeForRGB];
|
||||
|
||||
// Convert pixel type to RGB8_Packed
|
||||
MV_CC_PIXEL_CONVERT_PARAM_EX stConvertParam = new MV_CC_PIXEL_CONVERT_PARAM_EX();
|
||||
stConvertParam.width = stImageInfo.ExtendWidth; // image width
|
||||
stConvertParam.height = stImageInfo.ExtendHeight; // image height
|
||||
stConvertParam.srcData = pData; // input buffer
|
||||
stConvertParam.srcDataLen = stImageInfo.frameLen; // input buffer size
|
||||
stConvertParam.srcPixelType = stImageInfo.pixelType; // input pixel format
|
||||
stConvertParam.dstPixelType = MvGvspPixelType.PixelType_Gvsp_RGB8_Packed; // output pixel format
|
||||
stConvertParam.dstBuffer = pDataForRGB; // output buffer
|
||||
stConvertParam.dstBufferSize = dataSizeForRGB; // output buffer size
|
||||
|
||||
nRet = MvCameraControl.MV_CC_ConvertPixelTypeEx(hCamera, stConvertParam);
|
||||
if (MV_OK != nRet)
|
||||
{
|
||||
System.err.printf("Convert PixelType fail, errcode: [%#x]\n", nRet);
|
||||
break;
|
||||
}
|
||||
|
||||
// Save buffer content to file
|
||||
saveDataToFile(pDataForRGB, dataSizeForRGB, "AfterConvert_RGB.raw");
|
||||
|
||||
// Stop grabbing
|
||||
nRet = MvCameraControl.MV_CC_StopGrabbing(hCamera);
|
||||
if (MV_OK != nRet)
|
||||
{
|
||||
System.err.printf("StopGrabbing failed, errcode: [%#x]\n", nRet);
|
||||
break;
|
||||
}
|
||||
|
||||
// close device
|
||||
nRet = MvCameraControl.MV_CC_CloseDevice(hCamera);
|
||||
if (MV_OK != nRet)
|
||||
{
|
||||
System.err.printf("CloseDevice failed, errcode: [%#x]\n", nRet);
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
} while (false);
|
||||
|
||||
if (null != hCamera)
|
||||
{
|
||||
// Destroy handle
|
||||
nRet = MvCameraControl.MV_CC_DestroyHandle(hCamera);
|
||||
if (MV_OK != nRet) {
|
||||
System.err.printf("DestroyHandle failed, errcode: [%#x]\n", nRet);
|
||||
}
|
||||
}
|
||||
MvCameraControl.MV_CC_Finalize();
|
||||
scanner.close();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,264 @@
|
||||
package top.wms.admin.controller.hkMVS; /***************************************************************************************************
|
||||
* @file Grab_Callback.java
|
||||
* @breif Use functions provided in MvCameraControlWrapper.jar to grab images
|
||||
* @author zhanglei72
|
||||
* @date 2020/01/12
|
||||
*
|
||||
* @warning
|
||||
* @version V1.0.0 2020/01/12 Create this file
|
||||
* V1.0.1 2020/02/10 add parameter checking
|
||||
* @since 2020/02/10
|
||||
**************************************************************************************************/
|
||||
|
||||
import MvCameraControlWrapper.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Scanner;
|
||||
|
||||
import static MvCameraControlWrapper.MvCameraControlDefines.*;
|
||||
|
||||
public class Grab_Callback {
|
||||
public static Scanner scanner;
|
||||
|
||||
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) || (stDeviceInfo.transportLayerType == MV_GENTL_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 if (stDeviceInfo.transportLayerType == MV_GENTL_CAMERALINK_DEVICE) {
|
||||
System.out.println("\tUserDefinedName: " + stDeviceInfo.cmlInfo.userDefinedName);
|
||||
System.out.println("\tSerial Number: " + stDeviceInfo.cmlInfo.serialNumber);
|
||||
System.out.println("\tDevice Number: " + stDeviceInfo.cmlInfo.DeviceID);
|
||||
} else if (stDeviceInfo.transportLayerType == MV_GENTL_CXP_DEVICE) {
|
||||
System.out.println("\tUserDefinedName: " + stDeviceInfo.cxpInfo.userDefinedName);
|
||||
System.out.println("\tSerial Number: " + stDeviceInfo.cxpInfo.serialNumber);
|
||||
System.out.println("\tDevice Number: " + stDeviceInfo.cxpInfo.DeviceID);
|
||||
} else if (stDeviceInfo.transportLayerType == MV_GENTL_XOF_DEVICE) {
|
||||
System.out.println("\tUserDefinedName: " + stDeviceInfo.xofInfo.userDefinedName);
|
||||
System.out.println("\tSerial Number: " + stDeviceInfo.xofInfo.serialNumber);
|
||||
System.out.println("\tDevice Number: " + stDeviceInfo.xofInfo.DeviceID);
|
||||
} 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 int chooseCamera(ArrayList<MV_CC_DEVICE_INFO> stDeviceList) {
|
||||
if (null == stDeviceList) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Choose a device to operate
|
||||
int camIndex = -1;
|
||||
|
||||
while (true) {
|
||||
System.out.print("Please input camera index:");
|
||||
if (scanner.hasNextInt()) {
|
||||
try {
|
||||
|
||||
camIndex = scanner.nextInt();
|
||||
if ((camIndex >= 0 && camIndex < stDeviceList.size()) || -1 == camIndex) {
|
||||
break;
|
||||
} else {
|
||||
System.out.println("Input error: " + camIndex + " Over Range:( 0 - " + (stDeviceList.size() - 1) + " )");
|
||||
}
|
||||
} catch (NumberFormatException e) {
|
||||
System.out.println("Input not number.");
|
||||
camIndex = -1;
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
camIndex = -1;
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
if (-1 == camIndex) {
|
||||
System.out.println("Input error.exit");
|
||||
return camIndex;
|
||||
}
|
||||
|
||||
if (0 <= camIndex && stDeviceList.size() > camIndex) {
|
||||
if ((MV_GIGE_DEVICE == stDeviceList.get(camIndex).transportLayerType) || (MV_GENTL_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 if (MV_GENTL_CAMERALINK_DEVICE == stDeviceList.get(camIndex).transportLayerType) {
|
||||
System.out.println("Connect to camera[" + camIndex + "]: " + stDeviceList.get(camIndex).cmlInfo.DeviceID);
|
||||
} else if (MV_GENTL_CXP_DEVICE == stDeviceList.get(camIndex).transportLayerType) {
|
||||
System.out.println("Connect to camera[" + camIndex + "]: " + stDeviceList.get(camIndex).cxpInfo.DeviceID);
|
||||
} else if (MV_GENTL_XOF_DEVICE == stDeviceList.get(camIndex).transportLayerType) {
|
||||
System.out.println("Connect to camera[" + camIndex + "]: " + stDeviceList.get(camIndex).xofInfo.DeviceID);
|
||||
} 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;
|
||||
scanner = new Scanner(System.in);
|
||||
|
||||
Handle hCamera = null;
|
||||
ArrayList<MV_CC_DEVICE_INFO> stDeviceList = null;
|
||||
|
||||
do {
|
||||
System.out.println("SDK Version " + MvCameraControl.MV_CC_GetSDKVersion());
|
||||
|
||||
// Initialize SDK
|
||||
nRet = MvCameraControl.MV_CC_Initialize();
|
||||
if (MV_OK != nRet) {
|
||||
System.err.printf("Initialize SDK fail! nRet [0x%x]\n\n", nRet);
|
||||
break;
|
||||
}
|
||||
|
||||
// Enumerate devices
|
||||
try {
|
||||
stDeviceList = MvCameraControl.MV_CC_EnumDevices(MV_GIGE_DEVICE | MV_USB_DEVICE | MV_GENTL_GIGE_DEVICE | MV_GENTL_CAMERALINK_DEVICE | MV_GENTL_CXP_DEVICE | MV_GENTL_XOF_DEVICE);
|
||||
} catch (CameraControlException e) {
|
||||
System.err.println("Enumrate devices failed!" + e.toString());
|
||||
e.printStackTrace();
|
||||
break;
|
||||
}
|
||||
|
||||
if (0 >= stDeviceList.size()) {
|
||||
System.out.println("No devices found!");
|
||||
break;
|
||||
}
|
||||
int i = 0;
|
||||
for (MV_CC_DEVICE_INFO stDeviceInfo : stDeviceList) {
|
||||
if (null == stDeviceInfo) {
|
||||
continue;
|
||||
}
|
||||
System.out.println("[camera " + (i++) + "]");
|
||||
printDeviceInfo(stDeviceInfo);
|
||||
}
|
||||
|
||||
// choose camera
|
||||
camIndex = chooseCamera(stDeviceList);
|
||||
if (-1 == camIndex) {
|
||||
break;
|
||||
}
|
||||
|
||||
// Create device handle
|
||||
try {
|
||||
hCamera = MvCameraControl.MV_CC_CreateHandle(stDeviceList.get(camIndex));
|
||||
} catch (CameraControlException e) {
|
||||
System.err.println("Create handle failed!" + e.toString());
|
||||
e.printStackTrace();
|
||||
hCamera = null;
|
||||
break;
|
||||
}
|
||||
|
||||
// Open selected device
|
||||
nRet = MvCameraControl.MV_CC_OpenDevice(hCamera);
|
||||
if (MV_OK != nRet) {
|
||||
System.err.printf("Connect to camera failed, errcode: [%#x]\n", nRet);
|
||||
break;
|
||||
}
|
||||
|
||||
// Register image callback
|
||||
nRet = MvCameraControl.MV_CC_RegisterImageCallBack(hCamera, new CameraImageCallBack() {
|
||||
@Override
|
||||
public int OnImageCallBack(byte[] bytes, MV_FRAME_OUT_INFO mv_frame_out_info) {
|
||||
printFrameInfo(mv_frame_out_info);
|
||||
return 0;
|
||||
}
|
||||
});
|
||||
if (MV_OK != nRet) {
|
||||
System.err.printf("register image callback failed, errcode: [%#x]\n", nRet);
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
// Turn off trigger mode
|
||||
nRet = MvCameraControl.MV_CC_SetEnumValueByString(hCamera, "TriggerMode", "Off");
|
||||
if (MV_OK != nRet) {
|
||||
System.err.printf("SetTriggerMode failed, errcode: [%#x]\n", nRet);
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
nRet = MvCameraControl.MV_CC_StartGrabbing(hCamera);
|
||||
if (MV_OK != nRet) {
|
||||
System.err.printf("StartGrabbing failed, errcode: [%#x]\n", nRet);
|
||||
break;
|
||||
}
|
||||
|
||||
scanner.useDelimiter("");
|
||||
System.out.println("Press Enter to exit.");
|
||||
while (true) {
|
||||
String input = scanner.nextLine();
|
||||
if (scanner.hasNextLine()) {
|
||||
break;
|
||||
} else {
|
||||
try {
|
||||
Thread.sleep(1 * 10);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Stop grabbing
|
||||
nRet = MvCameraControl.MV_CC_StopGrabbing(hCamera);
|
||||
if (MV_OK != nRet) {
|
||||
System.err.printf("StopGrabbing failed, errcode: [%#x]\n", nRet);
|
||||
break;
|
||||
}
|
||||
|
||||
// close device
|
||||
nRet = MvCameraControl.MV_CC_CloseDevice(hCamera);
|
||||
if (MV_OK != nRet) {
|
||||
System.err.printf("CloseDevice failed, errcode: [%#x]\n", nRet);
|
||||
break;
|
||||
}
|
||||
|
||||
} while (false);
|
||||
|
||||
if (null != hCamera) {
|
||||
// Destroy handle
|
||||
nRet = MvCameraControl.MV_CC_DestroyHandle(hCamera);
|
||||
if (MV_OK != nRet) {
|
||||
System.err.printf("DestroyHandle failed, errcode: [%#x]\n", nRet);
|
||||
}
|
||||
}
|
||||
MvCameraControl.MV_CC_Finalize();
|
||||
scanner.close();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user