package org.example; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.util.ArrayList; // Main.java 顶部增加 import org.glassfish.tyrus.server.Server; import MvCodeReaderCtrlWrapper.*; import MvCodeReaderCtrlWrapper.MvCodeReaderCtrl.*; import MvCodeReaderCtrlWrapper.MvCodeReaderCtrlDefine.*; import MvCodeReaderCtrlWrapper.ParameterException.*; import java.net.*; import java.io.*; public class Main { static Handle hHandle = null; // 保存设备列表 private static ArrayList stCamListCache = new ArrayList<>(); // 获取设备列表JSON(推送给前端) public static String getDeviceListJson() { org.json.JSONObject obj = new org.json.JSONObject(); obj.put("type", "deviceList"); org.json.JSONArray arr = new org.json.JSONArray(); for (int i = 0; i < stCamListCache.size(); i++) { MV_CODEREADER_DEVICE_INFO camInfo = stCamListCache.get(i); // System.out.println(camInfo.toString() + "设备相信信息"); System.out.println(camInfo.stGigEInfo.chModelName + camInfo.stGigEInfo.chUserDefinedName + camInfo.stGigEInfo.nCurrentIp + camInfo.stGigEInfo.chSerialNumber + camInfo.stGigEInfo.chDeviceVersion + camInfo.stGigEInfo.chManufacturerName + camInfo.stGigEInfo.chManufacturerSpecificInfo + camInfo.stGigEInfo.nIpCfgCurrent); org.json.JSONObject dev = new org.json.JSONObject(); dev.put("id", camInfo.stGigEInfo.chSerialNumber); // 序列号 dev.put("name", camInfo.stGigEInfo.chUserDefinedName != null ? camInfo.stGigEInfo.chUserDefinedName : camInfo.stGigEInfo.chModelName); dev.put("type", "scanner"); dev.put("on", true); // 默认开 arr.put(dev); } obj.put("devices", arr); return obj.toString(); } public static void saveDataToFile(byte[] dataToSave, int dataSize, String fileName) { OutputStream os = null; try { // Create saveImg directory File tempFile = new File("saveImg"); if (!tempFile.exists()) { tempFile.mkdirs(); } stCamListCache = MvCodeReaderCtrl.MV_CODEREADER_EnumDevices(); os.write(dataToSave, 0, dataSize); System.out.println("SaveImage succeed."); } catch (IOException e) { e.printStackTrace(); } finally { // Close file stream try { os.close(); } catch (IOException e) { e.printStackTrace(); } } } // CallBack function private static void printImgCBInfo(byte[] pdata, MV_CODEREADER_IMAGE_OUT_INFO_EX2 stOutInfo) { if (null == stOutInfo) { System.out.println("stOutInfo is null"); return; } System.out.print("/**CBpstOutInfo***************************************/\n"); // 示例:扫码时调用POST接口,解析扫码内容并发送JSON try { // 假设扫码内容为第一个条码内容 // String scanStr = null; // if (stOutInfo != null && stOutInfo.pstCodeListEx != null && stOutInfo.pstCodeListEx.nCodeNum > 0) { // scanStr = stOutInfo.pstCodeListEx.stBcrInfoEx.get(0).chCode; // } // if (scanStr != null && !scanStr.isEmpty()) { // String json = ScanDataUtil.buildRequestJson( // scanStr, // "raw_material", // 物品类型 // "扫码入库", // 备注 // null, // 源库位ID // 1 // 记录类型 // ); // System.out.println("json: " + json); // // 发送两次请求:修改库存并且插入一条记录 // // 插入记录 // String postUrl1 = "http://localhost:8080/wms/stockIoDetail"; // TODO: 替换为实际接口 // String postResponse = HttpRequestUtil.postJson(postUrl1, json); // System.out.println("POST接口响应: " + postResponse); // // 修改库存 // String postUrl2 = "http://localhost:8080/wms/stockIo/scanInStock"; // String postResponse2 = HttpRequestUtil.postJson(postUrl2, json); // System.out.println("修改库存POST接口响应: " + postResponse2); // } else { // System.out.println("未获取到扫码内容,未发送POST请求"); // } } catch (Exception e) { System.err.println("POST请求失败: " + e.getMessage()); } // save buffer to file follow Image Type to Save // saveDataToFile(pdata, stOutInfo.nFrameLen, "Image.jpg"); // saveDataToFile(pdata, stOutInfo.nFrameLen, "Image.raw"); System.out.print(String.format("Get One Frame: nEventID[%d], nChannelID[%d], nWidth[%d], nHeight[%d], nFrameNum[%d], nTriggerIndex[%d], nFrameLen[%d], " + " nCodeNumber[%d] \r\n", stOutInfo.nEventID, stOutInfo.nChannelID, stOutInfo.nWidth, stOutInfo.nHeight, stOutInfo.nFrameNum, stOutInfo.nTriggerIndex, stOutInfo.nFrameLen, stOutInfo.pstCodeListEx.nCodeNum)); System.out.print("Get One Code: bIsGetCode[" + stOutInfo.bIsGetCode + "]success\r\n"); System.out.print("Get GvspPixelType: MvCodeReaderGvspPixelType[" + stOutInfo.enPixelType + "]success\r\n"); // print code info for (int a = 0; a < stOutInfo.pstCodeListEx.nCodeNum; a++) { org.json.JSONObject obj = new org.json.JSONObject(); obj.put("type", "scanMessage"); // 解析码中的数据,用下划线分割,第一个为物料类型,第二个为物料id String code = stOutInfo.pstCodeListEx.stBcrInfoEx.get(a).chCode; String[] parts = code.split("__"); String itemType = parts[0]; String itemId = parts[1]; obj.put("itemType", itemType); obj.put("itemId", itemId); WsServer.broadcast(obj.toString()); System.out.print(String.format("CodeInfo: TheCodeID[%d], CodeString[%s], nCodeLen[%d], nAngle[%d], nBarType[%d]," + "sAlgoCost[%d], nIDRScore[%d], n1DIsGetQuality[%d]\r\n", a, stOutInfo.pstCodeListEx.stBcrInfoEx.get(a).chCode, stOutInfo.pstCodeListEx.stBcrInfoEx.get(a).nLen, stOutInfo.pstCodeListEx.stBcrInfoEx.get(a).nAngle, stOutInfo.pstCodeListEx.stBcrInfoEx.get(a).nBarType, stOutInfo.pstCodeListEx.stBcrInfoEx.get(a).sAlgoCost, stOutInfo.pstCodeListEx.stBcrInfoEx.get(a).nIDRScore, stOutInfo.pstCodeListEx.stBcrInfoEx.get(a).n1DIsGetQuality)); System.out.print(String.format("CodePointInfo: stCornerLeftTop.X[%d], stCornerLeftTop.Y[%d], stCornerRightTop.X[%d], stCornerRightTop.Y[%d]," + "stCornerRightBottom.X[%d], stCornerRightBottom.Y[%d], stCornerLeftBottom.X[%d], stCornerLeftBottom.Y[%d]\r\n", stOutInfo.pstCodeListEx.stBcrInfoEx.get(a).stCornerLeftTop.nX, stOutInfo.pstCodeListEx.stBcrInfoEx.get(a).stCornerLeftTop.nY, stOutInfo.pstCodeListEx.stBcrInfoEx.get(a).stCornerRightTop.nX, stOutInfo.pstCodeListEx.stBcrInfoEx.get(a).stCornerRightTop.nY, stOutInfo.pstCodeListEx.stBcrInfoEx.get(a).stCornerRightBottom.nX, stOutInfo.pstCodeListEx.stBcrInfoEx.get(a).stCornerRightBottom.nY, stOutInfo.pstCodeListEx.stBcrInfoEx.get(a).stCornerLeftBottom.nX, stOutInfo.pstCodeListEx.stBcrInfoEx.get(a).stCornerLeftBottom.nY)); System.out.print("Get CodeQuality: bIsGetQuality[" + stOutInfo.pstCodeListEx.stBcrInfoEx.get(a).bIsGetQuality + "]success\r\n"); } } private static void PrintDeviceInfo(MV_CODEREADER_DEVICE_INFO stCamInfo) { if (stCamInfo.nTLayerType == MvCodeReaderCtrlDefine.MV_CODEREADER_GIGE_DEVICE) { int nIp1 = ((stCamInfo.stGigEInfo.nCurrentIp & 0xff000000) >> 24) & 0xff; int nIp2 = ((stCamInfo.stGigEInfo.nCurrentIp & 0x00ff0000) >> 16); int nIp3 = ((stCamInfo.stGigEInfo.nCurrentIp & 0x0000ff00) >> 8); int nIp4 = (stCamInfo.stGigEInfo.nCurrentIp & 0x000000ff); System.out.print("CurrentIp: " + nIp1 + "." + nIp2 + "." + nIp3 + "." + nIp4 + "\r\n"); System.out.print(String.format("GiGEInfo: UserDefinedName:[%s], chSerialNumber:[%s] \r\n\r\n", stCamInfo.stGigEInfo.chUserDefinedName, stCamInfo.stGigEInfo.chSerialNumber)); } } public static void main(String[] args) throws InterruptedException { System.out.print(" ***************Begin****************** \r\n"); String strVersion = MvCodeReaderCtrl.MV_CODEREADER_GetSDKVersion(); System.out.print("Get version " + strVersion + "\r\n"); ArrayList stCamList = MvCodeReaderCtrl.MV_CODEREADER_EnumDevices(); Main.stCamListCache = stCamList; if (stCamList == null || stCamList.isEmpty()) { System.out.print("Find No Device!\r\n"); return; } ArrayList handleList = new ArrayList<>(); for (int i = 0; i < stCamList.size(); i++) { MV_CODEREADER_DEVICE_INFO camInfo = stCamList.get(i); PrintDeviceInfo(camInfo); Handle handle = null; try { handle = MvCodeReaderCtrl.MV_CODEREADER_CreateHandle(camInfo); } catch (ParameterException e) { throw new RuntimeException(e); } if (handle == null) { System.out.print("Create handle failed for device " + i + "!\r\n"); continue; } int nRet = MvCodeReaderCtrl.MV_CODEREADER_OpenDevice(handle); if (nRet != 0) { System.out.print("Open Device fail for device " + i + "! nRet[" + String.format("0x%x", nRet) + "]\r\n"); continue; } nRet = MvCodeReaderCtrl.MV_CODEREADER_RegisterImageCallBackEx2(handle, new ImageCallBack() { @Override public int OnImageCallBack(byte[] pdata, MV_CODEREADER_IMAGE_OUT_INFO_EX2 stOutInfo) { printImgCBInfo(pdata, stOutInfo); return 0; } }); if (nRet != 0) { System.out.print("RegisterImageCallBackEx2 Failed for device " + i + "! nRet[" + String.format("0x%x", nRet) + "]\r\n"); continue; } nRet = MvCodeReaderCtrl.MV_CODEREADER_StartGrabbing(handle); if (nRet != 0) { System.out.print("StartGrabbing Failed for device " + i + "! nRet[" + String.format("0x%x", nRet) + "]\r\n"); continue; } handleList.add(handle); System.out.print("Device " + i + " startGrabbing success!\r\n"); } // 阻塞主线程直到退出 System.out.println("所有扫码枪已开始监听"); // 启动 Socket 服务器 Server server = new Server("localhost", 9000, "/", null, WsServer.class); try { server.start(); System.out.println("WebSocket服务器已启动,端口9000"); System.in.read(); // 阻塞主线程直到回车 } catch (Exception e) { throw new RuntimeException(e); } finally { server.stop(); } } }