diff --git a/wms-webapi/pom.xml b/wms-webapi/pom.xml
index 5fcb322..ae5d3b9 100644
--- a/wms-webapi/pom.xml
+++ b/wms-webapi/pom.xml
@@ -80,6 +80,15 @@
jakarta.websocket-api
2.1.0
+
+
+
+ com.hikvision
+ MvCameraControlWrapper
+ 1.0
+ system
+ ${project.basedir}/Library/MvCameraControlWrapper.jar
+
diff --git a/wms-webapi/src/main/java/top/wms/admin/config/webSocket/webSocket.java b/wms-webapi/src/main/java/top/wms/admin/config/webSocket/webSocket.java
index 4466b67..a541351 100644
--- a/wms-webapi/src/main/java/top/wms/admin/config/webSocket/webSocket.java
+++ b/wms-webapi/src/main/java/top/wms/admin/config/webSocket/webSocket.java
@@ -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 {
+
+ @Autowired
+ private CameraWebSocketHandler cameraWebSocketHandler;
+
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
// 注册WebSocket端点,允许所有跨域请求
registry.addHandler(new ScaleWebSocketHandler(), "/ws/scale").setAllowedOrigins("*");
+ // 注册海康相机WebSocket端点
+ registry.addHandler(cameraWebSocketHandler, "/ws/camera").setAllowedOrigins("*");
}
}
\ No newline at end of file
diff --git a/wms-webapi/src/main/java/top/wms/admin/controller/hkMVS/CameraService.java b/wms-webapi/src/main/java/top/wms/admin/controller/hkMVS/CameraService.java
new file mode 100644
index 0000000..a5736a5
--- /dev/null
+++ b/wms-webapi/src/main/java/top/wms/admin/controller/hkMVS/CameraService.java
@@ -0,0 +1,377 @@
+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 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 stDeviceList = null;
+ // WebSocket会话映射,用于存储活跃的WebSocket连接
+ private final Map webSocketSessions = new ConcurrentHashMap<>();
+ // 相机是否正在运行
+ private boolean isRunning = false;
+
+ /**
+ * 初始化相机
+ * @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 [0x{0}]", 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("[camera {}}", i++);
+ printDeviceInfo(stDeviceInfo);
+ }
+
+ // 默认选择第一个设备
+ 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: [0x{0}]", 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: [0x{0}]", 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) {
+ printFrameInfo(mv_frame_out_info);
+ processImage(bytes, mv_frame_out_info);
+ return 0;
+ }
+ });
+ if (MV_OK != nRet) {
+ log.error("Register image callback failed, errcode: [0x{0}]", Integer.toHexString(nRet));
+ return false;
+ }
+
+ // 开始采集
+ log.info("Starting grabbing...");
+ nRet = MvCameraControl.MV_CC_StartGrabbing(hCamera);
+ if (MV_OK != nRet) {
+ log.error("StartGrabbing failed, errcode: [0x{0}]", 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: [0x{0}]", Integer.toHexString(nRet));
+ }
+
+ 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 {
+ // 打印帧信息,以便调试
+ printFrameInfo(frameInfo);
+
+ // 对于黑白相机(MV-CE050-30GM),数据是单通道灰度格式
+ // 创建包含图像尺寸信息和原始数据的消息
+ // 消息格式: [width(4 bytes)][height(4 bytes)][image data]
+ byte[] message = new byte[8 + bytes.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(bytes, 0, message, 8, bytes.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) {
+ startStream();
+ }
+ }
+ }
+
+ /**
+ * 移除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()) {
+ stopStream();
+ }
+ }
+ }
+
+ /**
+ * 获取当前连接的WebSocket会话数量
+ * @return 会话数量
+ */
+ public int getWebSocketSessionCount() {
+ return webSocketSessions.size();
+ }
+
+ /**
+ * 检查相机是否正在运行
+ * @return 是否正在运行
+ */
+ public boolean isRunning() {
+ return isRunning;
+ }
+
+ /**
+ * 打印设备信息
+ * @param stDeviceInfo 设备信息
+ */
+ private void printDeviceInfo(MV_CC_DEVICE_INFO stDeviceInfo) {
+ if (null == stDeviceInfo) {
+ log.error("stDeviceInfo is null");
+ return;
+ }
+
+ if ((stDeviceInfo.transportLayerType == MV_GIGE_DEVICE) || (stDeviceInfo.transportLayerType == MV_GENTL_GIGE_DEVICE)) {
+ log.info("\tCurrentIp: {}", stDeviceInfo.gigEInfo.currentIp);
+ log.info("\tModel: {}", stDeviceInfo.gigEInfo.modelName);
+ log.info("\tUserDefinedName: {}", stDeviceInfo.gigEInfo.userDefinedName);
+ } else if (stDeviceInfo.transportLayerType == MV_USB_DEVICE) {
+ log.info("\tUserDefinedName: {}", stDeviceInfo.usb3VInfo.userDefinedName);
+ log.info("\tSerial Number: {}", stDeviceInfo.usb3VInfo.serialNumber);
+ log.info("\tDevice Number: {}", stDeviceInfo.usb3VInfo.deviceNumber);
+ } else if (stDeviceInfo.transportLayerType == MV_GENTL_CAMERALINK_DEVICE) {
+ log.info("\tUserDefinedName: {}", stDeviceInfo.cmlInfo.userDefinedName);
+ log.info("\tSerial Number: {}", stDeviceInfo.cmlInfo.serialNumber);
+ log.info("\tDevice Number: {}", stDeviceInfo.cmlInfo.DeviceID);
+ } else if (stDeviceInfo.transportLayerType == MV_GENTL_CXP_DEVICE) {
+ log.info("\tUserDefinedName: {}", stDeviceInfo.cxpInfo.userDefinedName);
+ log.info("\tSerial Number: {}", stDeviceInfo.cxpInfo.serialNumber);
+ log.info("\tDevice Number: {}", stDeviceInfo.cxpInfo.DeviceID);
+ } else if (stDeviceInfo.transportLayerType == MV_GENTL_XOF_DEVICE) {
+ log.info("\tUserDefinedName: {}", stDeviceInfo.xofInfo.userDefinedName);
+ log.info("\tSerial Number: {}", stDeviceInfo.xofInfo.serialNumber);
+ log.info("\tDevice Number: {}", stDeviceInfo.xofInfo.DeviceID);
+ } else {
+ log.error("Device is not supported! ");
+ }
+
+ log.info("\tAccessible: {}", MvCameraControl.MV_CC_IsDeviceAccessible(stDeviceInfo, MV_ACCESS_Exclusive));
+ }
+
+ /**
+ * 打印帧信息
+ * @param stFrameInfo 帧信息
+ */
+ private void printFrameInfo(MV_FRAME_OUT_INFO stFrameInfo) {
+ if (null == stFrameInfo) {
+ log.error("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()));
+
+ log.debug(frameInfo.toString());
+ }
+}
\ No newline at end of file
diff --git a/wms-webapi/src/main/java/top/wms/admin/controller/hkMVS/CameraWebSocketHandler.java b/wms-webapi/src/main/java/top/wms/admin/controller/hkMVS/CameraWebSocketHandler.java
new file mode 100644
index 0000000..48fb729
--- /dev/null
+++ b/wms-webapi/src/main/java/top/wms/admin/controller/hkMVS/CameraWebSocketHandler.java
@@ -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);
+ }
+}
\ No newline at end of file
diff --git a/wms-webapi/src/main/java/top/wms/admin/controller/hkMVS/ConvertPixelType.java b/wms-webapi/src/main/java/top/wms/admin/controller/hkMVS/ConvertPixelType.java
new file mode 100644
index 0000000..1c07680
--- /dev/null
+++ b/wms-webapi/src/main/java/top/wms/admin/controller/hkMVS/ConvertPixelType.java
@@ -0,0 +1,386 @@
+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 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 (Exception 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;
+ Handle hCamera = null;
+ scanner = new Scanner(System.in);
+ ArrayList 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;
+ }
+
+ 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();
+ }
+}