新增record_type字段区分扫码枪录入还是采购录入明细表,以及注释扫码枪代码,修改出入库移库逻辑

This commit is contained in:
2025-07-23 16:04:23 +08:00
parent 4422ca9099
commit 7db38923c9
10 changed files with 619 additions and 595 deletions

View File

@@ -1,247 +1,247 @@
package com.klp.reader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import MvCodeReaderCtrlWrapper.*;
import MvCodeReaderCtrlWrapper.MvCodeReaderCtrl.*;
import MvCodeReaderCtrlWrapper.MvCodeReaderCtrlDefine.*;
import MvCodeReaderCtrlWrapper.ParameterException.*;
public class CallBackDemo {
static Handle hHandle = null;
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();
}
os = new FileOutputStream(tempFile.getPath() + File.separator + fileName);
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");
// 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++)
{
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 {
int nRet = 0;
int i = 0;
System.out.print(" ***************Begin****************** \r\n");
String strVersion = MvCodeReaderCtrl.MV_CODEREADER_GetSDKVersion();
System.out.print("Get version " + strVersion + "\r\n");
do
{
// Enum device
ArrayList<MV_CODEREADER_DEVICE_INFO> stCamList = MvCodeReaderCtrl.MV_CODEREADER_EnumDevices();
if (stCamList == null)
{
System.out.print("Find No Device!\r\n");
break;
}
for(MV_CODEREADER_DEVICE_INFO stCamInfo:stCamList)
{
System.out.print("[device " + i + "]:\r\n");
PrintDeviceInfo(stCamInfo);
i++;
}
int nIndex = 0;
System.out.print("Please input camera index: ");
try
{
nIndex = System.in.read() - 48;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.print("Input Error!\r\n");
break;
}
try
{
MV_CODEREADER_DEVICE_INFO deviceInfo = new MV_CODEREADER_DEVICE_INFO();
hHandle = MvCodeReaderCtrl.MV_CODEREADER_CreateHandle(stCamList.get(nIndex));
if(null == hHandle)
{
System.out.print("Create handle failed! \r\n");
break;
}
}
catch(Exception e)
{
e.printStackTrace();
System.out.print("Create Handle Failed \r\n");
break;
}
nRet = MvCodeReaderCtrl.MV_CODEREADER_OpenDevice(hHandle);
if(0 != nRet){
System.out.print("Open Device fail! nRet[" + String.format("0x%x", nRet) + "]\r\n");
break;
}
else
{
System.out.print("Open device success!\r\n");
}
// Register MvCodeReaderCtrl.MV_CODEREADER_RegisterImageCallBackEx2
nRet = MvCodeReaderCtrl.MV_CODEREADER_RegisterImageCallBackEx2(hHandle, new ImageCallBack()
{
@Override
public int OnImageCallBack(byte[] pdata, MV_CODEREADER_IMAGE_OUT_INFO_EX2 stOutInfo) {
// TODO Auto-generated method stub
printImgCBInfo( pdata, stOutInfo);
return 0;
}
});
if(0 != nRet)
{
System.out.print("MV_CODEREADER_RegisterImageCallBackEx2 Failed! nRet[" + String.format("0x%x", nRet) + "]\r\n");
break;
}
System.out.print("MV_CODEREADER_RegisterImageCallBackEx2 ! nRet[" + String.format("0x%x", nRet) + "]\r\n");
nRet = MvCodeReaderCtrl.MV_CODEREADER_StartGrabbing(hHandle);
if(0 != nRet){
System.out.print("StartGrabbing Failed! nRet[" + String.format("0x%x", nRet) + "]\r\n");
break;
}
else
{
System.out.print("Device startGrabbing success!\r\n");
}
// Wait CallBack Data
try
{
Thread.sleep(1000 * 1000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
MvCodeReaderCtrl.MV_CODEREADER_StopGrabbing(hHandle);
if(0 != nRet){
System.out.print("StopGrabbing failed! nRet[" + String.format("0x%x", nRet) + "]\r\n");
break;
}
else
{
System.out.print("Device stopGrabbing success!\r\n");
}
}while(false);
if (null != hHandle)
{
MvCodeReaderCtrl.MV_CODEREADER_DestroyHandle(hHandle);
}
System.out.print("exit.\r\n");
}
}
//package com.klp.reader;
//
//import java.io.File;
//import java.io.FileOutputStream;
//import java.io.IOException;
//import java.io.OutputStream;
//import java.util.ArrayList;
//
//import MvCodeReaderCtrlWrapper.*;
//import MvCodeReaderCtrlWrapper.MvCodeReaderCtrl.*;
//import MvCodeReaderCtrlWrapper.MvCodeReaderCtrlDefine.*;
//import MvCodeReaderCtrlWrapper.ParameterException.*;
//
//public class CallBackDemo {
//
// static Handle hHandle = null;
//
// 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();
// }
//
// os = new FileOutputStream(tempFile.getPath() + File.separator + fileName);
// 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");
//
// // 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++)
// {
// 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 {
//
// int nRet = 0;
// int i = 0;
//
// System.out.print(" ***************Begin****************** \r\n");
//
// String strVersion = MvCodeReaderCtrl.MV_CODEREADER_GetSDKVersion();
// System.out.print("Get version " + strVersion + "\r\n");
//
// do
// {
// // Enum device
// ArrayList<MV_CODEREADER_DEVICE_INFO> stCamList = MvCodeReaderCtrl.MV_CODEREADER_EnumDevices();
//
// if (stCamList == null)
// {
// System.out.print("Find No Device!\r\n");
// break;
// }
//
// for(MV_CODEREADER_DEVICE_INFO stCamInfo:stCamList)
// {
//
// System.out.print("[device " + i + "]:\r\n");
// PrintDeviceInfo(stCamInfo);
// i++;
// }
//
// int nIndex = 0;
// System.out.print("Please input camera index: ");
//
// try
// {
// nIndex = System.in.read() - 48;
//
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// System.out.print("Input Error!\r\n");
// break;
// }
//
// try
// {
// MV_CODEREADER_DEVICE_INFO deviceInfo = new MV_CODEREADER_DEVICE_INFO();
//
// hHandle = MvCodeReaderCtrl.MV_CODEREADER_CreateHandle(stCamList.get(nIndex));
// if(null == hHandle)
// {
// System.out.print("Create handle failed! \r\n");
// break;
// }
// }
// catch(Exception e)
// {
// e.printStackTrace();
// System.out.print("Create Handle Failed \r\n");
// break;
// }
//
// nRet = MvCodeReaderCtrl.MV_CODEREADER_OpenDevice(hHandle);
// if(0 != nRet){
// System.out.print("Open Device fail! nRet[" + String.format("0x%x", nRet) + "]\r\n");
// break;
// }
// else
// {
// System.out.print("Open device success!\r\n");
// }
//
// // Register MvCodeReaderCtrl.MV_CODEREADER_RegisterImageCallBackEx2
// nRet = MvCodeReaderCtrl.MV_CODEREADER_RegisterImageCallBackEx2(hHandle, new ImageCallBack()
// {
// @Override
// public int OnImageCallBack(byte[] pdata, MV_CODEREADER_IMAGE_OUT_INFO_EX2 stOutInfo) {
// // TODO Auto-generated method stub
// printImgCBInfo( pdata, stOutInfo);
//
// return 0;
// }
// });
//
// if(0 != nRet)
// {
// System.out.print("MV_CODEREADER_RegisterImageCallBackEx2 Failed! nRet[" + String.format("0x%x", nRet) + "]\r\n");
// break;
// }
//
// System.out.print("MV_CODEREADER_RegisterImageCallBackEx2 ! nRet[" + String.format("0x%x", nRet) + "]\r\n");
//
// nRet = MvCodeReaderCtrl.MV_CODEREADER_StartGrabbing(hHandle);
// if(0 != nRet){
// System.out.print("StartGrabbing Failed! nRet[" + String.format("0x%x", nRet) + "]\r\n");
// break;
// }
// else
// {
// System.out.print("Device startGrabbing success!\r\n");
// }
//
// // Wait CallBack Data
// try
// {
// Thread.sleep(1000 * 1000);
// }
// catch (InterruptedException e)
// {
// e.printStackTrace();
// }
//
// MvCodeReaderCtrl.MV_CODEREADER_StopGrabbing(hHandle);
// if(0 != nRet){
// System.out.print("StopGrabbing failed! nRet[" + String.format("0x%x", nRet) + "]\r\n");
// break;
// }
// else
// {
// System.out.print("Device stopGrabbing success!\r\n");
// }
//
// }while(false);
//
// if (null != hHandle)
// {
// MvCodeReaderCtrl.MV_CODEREADER_DestroyHandle(hHandle);
// }
//
// System.out.print("exit.\r\n");
// }
//
//}
//

View File

@@ -1,275 +1,275 @@
package com.klp.reader;
import MvCodeReaderCtrlWrapper.MvCodeReaderCtrl;
import MvCodeReaderCtrlWrapper.MvCodeReaderCtrlDefine;
import MvCodeReaderCtrlWrapper.MvCodeReaderCtrlDefine.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
public class MvIDDemo {
static Handle hHandle = null;
// Save Image API
public static void saveDataToFile(byte[] dataToSave, int dataSize, String fileName)
{
OutputStream os = null;
try
{
// Create directory
File tempFile = new File("ImageSave");
if (!tempFile.exists())
{
tempFile.mkdirs();
}
os = new FileOutputStream(tempFile.getPath() + File.separator + fileName);
os.write(dataToSave, 0, dataSize);
os.flush();
os.close();
System.out.println("SaveImage succeed.");
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
// Close file stream
try
{
os.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
// Print Device Info
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));
}
}
// Grabbing thread API
public static class GrabbingThread extends Thread
{
public GrabbingThread()
{
}
public void run()
{
MV_CODEREADER_IMAGE_OUT_INFO_EX2 stOutInfo = new MV_CODEREADER_IMAGE_OUT_INFO_EX2();
MV_CODEREADER_CAM_INTVALUE stValue = new MV_CODEREADER_CAM_INTVALUE();
int nRet = 0;
long payloadSize = 0;
nRet = MvCodeReaderCtrl.MV_CODEREADER_GetIntValue(hHandle, "PayloadSize", stValue);
if (0 == nRet)
{
payloadSize = stValue.nCurValue;
System.out.print("GetOneFrame GetOptimalPacketSize[" + String.format("%d", payloadSize) + "]\r\n");
}
else
{
System.out.print("GetIntValue Failed, nRet[" + String.format("0x%x", nRet) + "]\r\n");
}
MV_CODEREADER_SAVE_IMAGE_PARAM_EX stParam = new MV_CODEREADER_SAVE_IMAGE_PARAM_EX();
byte[] pdata = new byte[(int)payloadSize];
for(int i = 0; i < 5; i++)
{
if(null != hHandle)
{
nRet = MvCodeReaderCtrl.MV_CODEREADER_GetOneFrameTimeoutEx2(hHandle, pdata, stOutInfo, 1000);
if(0 != nRet)
{
System.out.print("GetOneFrame Failed, nRet[" + String.format("0x%x", nRet) + "]\r\n");
}
else
{
// save buffer to file follow Image Type to Save
// saveDataToFile(pdata, stOutInfo.nFrameLen, "Image.jpg");
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++)
{
System.out.print(String.format("CodeInfo: TheCodeID[%d], CodeString[%s], nCodeLen[%d], nAngle[%d], nBarType[%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));
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));
}
}
}
}
}
}
public static void main(String[] args) throws InterruptedException {
int nRet = 0;
int i = 0;
System.out.print(" *****************Begin******************* \r\n");
String strVersion = MvCodeReaderCtrl.MV_CODEREADER_GetSDKVersion();
System.out.print("Get version " + strVersion + "\r\n");
do
{
// Enum device
ArrayList<MV_CODEREADER_DEVICE_INFO> stCamList = MvCodeReaderCtrl.MV_CODEREADER_EnumDevices();
if (stCamList == null)
{
System.out.print("Find No Device!\r\n");
break;
}
for(MV_CODEREADER_DEVICE_INFO stCamInfo:stCamList)
{
System.out.print("[device " + i + "]:\r\n");
PrintDeviceInfo(stCamInfo);
i++;
}
int nIndex = 0;
System.out.print("Please input camera index: ");
try
{
nIndex = System.in.read() - 48;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.print("Input Error!\r\n");
break;
}
System.out.print("input camera index is [" + nIndex + "]\r\n" );
try
{
hHandle = MvCodeReaderCtrl.MV_CODEREADER_CreateHandle(stCamList.get(nIndex));
if(null == hHandle)
{
System.out.print("Create handle failed! \r\n");
break;
}
}
catch(Exception e)
{
e.printStackTrace();
System.out.print("Create handle failed\r\n");
break;
}
nRet = MvCodeReaderCtrl.MV_CODEREADER_OpenDevice(hHandle);
if(0 != nRet){
System.out.print("Open Device fail! nRet[" + String.format("0x%x", nRet) + "]\r\n");
break;
}
else
{
System.out.print("Open device success!\r\n");
}
// register exception cb
nRet = MvCodeReaderCtrl.MV_CODEREADER_RegisterExceptionCallBack(hHandle, new ExceptionCallBack()
{
@Override
public int OnExceptionCallBack(int nMsg)
{
System.out.print("Open Device MV_CODEREADER_RegisterExceptionCallBack! nMsg[" + String.format("0x%x", nMsg) + "]\r\n");
return 0;
}
});
if(0 != nRet)
{
System.out.print("MVID_CR_CAM_RegisterImageCallBack Failed! nRet[" + String.format("0x%x", nRet) + "]\r\n");
break;
}
nRet = MvCodeReaderCtrl.MV_CODEREADER_StartGrabbing(hHandle);
if(0 != nRet){
System.out.print("StartGrabbing Failed! nRet[" + String.format("0x%x", nRet) + "]\r\n");
break;
}
else
{
System.out.print("Device startGrabbing success!\r\n");
}
// Create recv thread
try
{
GrabbingThread grabThread = new MvIDDemo.GrabbingThread();
grabThread.start();
Thread.sleep(12000);
grabThread.interrupt();
} catch (InterruptedException e) {
e.printStackTrace();
}
nRet = MvCodeReaderCtrl.MV_CODEREADER_StopGrabbing(hHandle);
if(0 != nRet){
System.out.print("StopGrabbing failed! nRet[" + String.format("0x%x", nRet) + "]\r\n");
break;
}
else
{
System.out.print("\r\nDevice stopGrabbing success!\r\n");
}
}while(false);
if (null != hHandle)
{
MvCodeReaderCtrl.MV_CODEREADER_DestroyHandle(hHandle);
}
System.out.print("Exit.\r\n");
}
}
//package com.klp.reader;
//
//import MvCodeReaderCtrlWrapper.MvCodeReaderCtrl;
//import MvCodeReaderCtrlWrapper.MvCodeReaderCtrlDefine;
//import MvCodeReaderCtrlWrapper.MvCodeReaderCtrlDefine.*;
//
//import java.io.File;
//import java.io.FileOutputStream;
//import java.io.IOException;
//import java.io.OutputStream;
//import java.util.ArrayList;
//
//public class MvIDDemo {
//
// static Handle hHandle = null;
//
// // Save Image API
// public static void saveDataToFile(byte[] dataToSave, int dataSize, String fileName)
// {
// OutputStream os = null;
//
// try
// {
// // Create directory
// File tempFile = new File("ImageSave");
// if (!tempFile.exists())
// {
// tempFile.mkdirs();
// }
//
// os = new FileOutputStream(tempFile.getPath() + File.separator + fileName);
// os.write(dataToSave, 0, dataSize);
// os.flush();
// os.close();
// System.out.println("SaveImage succeed.");
// }
// catch (IOException e)
// {
// e.printStackTrace();
// }
// finally
// {
// // Close file stream
// try
// {
// os.close();
// }
// catch (IOException e)
// {
// e.printStackTrace();
// }
// }
// }
//
// // Print Device Info
// 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));
// }
// }
//
// // Grabbing thread API
// public static class GrabbingThread extends Thread
// {
// public GrabbingThread()
// {
// }
//
// public void run()
// {
// MV_CODEREADER_IMAGE_OUT_INFO_EX2 stOutInfo = new MV_CODEREADER_IMAGE_OUT_INFO_EX2();
// MV_CODEREADER_CAM_INTVALUE stValue = new MV_CODEREADER_CAM_INTVALUE();
// int nRet = 0;
// long payloadSize = 0;
// nRet = MvCodeReaderCtrl.MV_CODEREADER_GetIntValue(hHandle, "PayloadSize", stValue);
// if (0 == nRet)
// {
// payloadSize = stValue.nCurValue;
// System.out.print("GetOneFrame GetOptimalPacketSize[" + String.format("%d", payloadSize) + "]\r\n");
// }
// else
// {
// System.out.print("GetIntValue Failed, nRet[" + String.format("0x%x", nRet) + "]\r\n");
// }
//
//
// MV_CODEREADER_SAVE_IMAGE_PARAM_EX stParam = new MV_CODEREADER_SAVE_IMAGE_PARAM_EX();
// byte[] pdata = new byte[(int)payloadSize];
//
// for(int i = 0; i < 5; i++)
// {
// if(null != hHandle)
// {
// nRet = MvCodeReaderCtrl.MV_CODEREADER_GetOneFrameTimeoutEx2(hHandle, pdata, stOutInfo, 1000);
// if(0 != nRet)
// {
// System.out.print("GetOneFrame Failed, nRet[" + String.format("0x%x", nRet) + "]\r\n");
// }
// else
// {
// // save buffer to file follow Image Type to Save
// // saveDataToFile(pdata, stOutInfo.nFrameLen, "Image.jpg");
//
// 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++)
// {
// System.out.print(String.format("CodeInfo: TheCodeID[%d], CodeString[%s], nCodeLen[%d], nAngle[%d], nBarType[%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));
//
// 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));
//
// }
//
// }
// }
// }
// }
// }
//
//
// public static void main(String[] args) throws InterruptedException {
//
// int nRet = 0;
// int i = 0;
//
// System.out.print(" *****************Begin******************* \r\n");
//
// String strVersion = MvCodeReaderCtrl.MV_CODEREADER_GetSDKVersion();
// System.out.print("Get version " + strVersion + "\r\n");
//
// do
// {
// // Enum device
// ArrayList<MV_CODEREADER_DEVICE_INFO> stCamList = MvCodeReaderCtrl.MV_CODEREADER_EnumDevices();
//
// if (stCamList == null)
// {
// System.out.print("Find No Device!\r\n");
// break;
// }
//
// for(MV_CODEREADER_DEVICE_INFO stCamInfo:stCamList)
// {
//
// System.out.print("[device " + i + "]:\r\n");
// PrintDeviceInfo(stCamInfo);
// i++;
// }
//
// int nIndex = 0;
// System.out.print("Please input camera index: ");
//
// try
// {
// nIndex = System.in.read() - 48;
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// System.out.print("Input Error!\r\n");
// break;
// }
//
// System.out.print("input camera index is [" + nIndex + "]\r\n" );
//
// try
// {
// hHandle = MvCodeReaderCtrl.MV_CODEREADER_CreateHandle(stCamList.get(nIndex));
// if(null == hHandle)
// {
// System.out.print("Create handle failed! \r\n");
// break;
// }
//
// }
// catch(Exception e)
// {
// e.printStackTrace();
// System.out.print("Create handle failed\r\n");
// break;
// }
//
// nRet = MvCodeReaderCtrl.MV_CODEREADER_OpenDevice(hHandle);
// if(0 != nRet){
// System.out.print("Open Device fail! nRet[" + String.format("0x%x", nRet) + "]\r\n");
// break;
// }
// else
// {
// System.out.print("Open device success!\r\n");
// }
//
// // register exception cb
// nRet = MvCodeReaderCtrl.MV_CODEREADER_RegisterExceptionCallBack(hHandle, new ExceptionCallBack()
// {
// @Override
// public int OnExceptionCallBack(int nMsg)
// {
// System.out.print("Open Device MV_CODEREADER_RegisterExceptionCallBack! nMsg[" + String.format("0x%x", nMsg) + "]\r\n");
// return 0;
// }
// });
//
// if(0 != nRet)
// {
// System.out.print("MVID_CR_CAM_RegisterImageCallBack Failed! nRet[" + String.format("0x%x", nRet) + "]\r\n");
// break;
// }
//
// nRet = MvCodeReaderCtrl.MV_CODEREADER_StartGrabbing(hHandle);
// if(0 != nRet){
// System.out.print("StartGrabbing Failed! nRet[" + String.format("0x%x", nRet) + "]\r\n");
// break;
// }
// else
// {
// System.out.print("Device startGrabbing success!\r\n");
// }
//
// // Create recv thread
// try
// {
// GrabbingThread grabThread = new MvIDDemo.GrabbingThread();
// grabThread.start();
// Thread.sleep(12000);
// grabThread.interrupt();
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
//
// nRet = MvCodeReaderCtrl.MV_CODEREADER_StopGrabbing(hHandle);
// if(0 != nRet){
// System.out.print("StopGrabbing failed! nRet[" + String.format("0x%x", nRet) + "]\r\n");
// break;
// }
// else
// {
// System.out.print("\r\nDevice stopGrabbing success!\r\n");
// }
//
// }while(false);
//
// if (null != hHandle)
// {
// MvCodeReaderCtrl.MV_CODEREADER_DestroyHandle(hHandle);
// }
//
// System.out.print("Exit.\r\n");
// }
//
//}

View File

@@ -1,70 +1,70 @@
package com.klp.reader.service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.klp.reader.entity.ScanResultEntity;
import com.klp.reader.mapper.ScanResultMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.util.ArrayList;
import MvCodeReaderCtrlWrapper.*;
import MvCodeReaderCtrlWrapper.MvCodeReaderCtrl.*;
import MvCodeReaderCtrlWrapper.MvCodeReaderCtrlDefine.*;
@Service
public class ScannerService {
private Handle hHandle;
@Autowired
private ScanResultMapper scanResultMapper; // 你需要实现这个Mapper
@PostConstruct
public void init() {
ArrayList<MV_CODEREADER_DEVICE_INFO> stCamList = MvCodeReaderCtrl.MV_CODEREADER_EnumDevices();
if (stCamList == null || stCamList.isEmpty()) {
System.out.println("Find No Device!");
return;
}
MV_CODEREADER_DEVICE_INFO deviceInfo = stCamList.get(0);
try {
hHandle = MvCodeReaderCtrl.MV_CODEREADER_CreateHandle(deviceInfo);
} catch (ParameterException e) {
throw new RuntimeException(e);
}
if (hHandle == null) {
System.out.println("Create handle failed!");
return;
}
int nRet = MvCodeReaderCtrl.MV_CODEREADER_OpenDevice(hHandle);
if (nRet != 0) {
System.out.println("Open Device fail! nRet[" + String.format("0x%x", nRet) + "]");
return;
}
MvCodeReaderCtrl.MV_CODEREADER_RegisterImageCallBackEx2(hHandle, (pdata, stOutInfo) -> {
handleScanResult(stOutInfo);
return 0;
});
MvCodeReaderCtrl.MV_CODEREADER_StartGrabbing(hHandle);
}
private void handleScanResult(MV_CODEREADER_IMAGE_OUT_INFO_EX2 stOutInfo) {
for (int a = 0; a < stOutInfo.pstCodeListEx.nCodeNum; a++) {
String code = stOutInfo.pstCodeListEx.stBcrInfoEx.get(a).chCode;
// 保存到数据库
ScanResultEntity entity = new ScanResultEntity();
entity.setCode(code);
scanResultMapper.insert(entity);
}
}
@PreDestroy
public void destroy() {
if (hHandle != null) {
MvCodeReaderCtrl.MV_CODEREADER_StopGrabbing(hHandle);
MvCodeReaderCtrl.MV_CODEREADER_DestroyHandle(hHandle);
}
}
}
//package com.klp.reader.service;
//
//import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
//import com.klp.reader.entity.ScanResultEntity;
//import com.klp.reader.mapper.ScanResultMapper;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//
//import javax.annotation.PostConstruct;
//import javax.annotation.PreDestroy;
//import java.util.ArrayList;
//
//import MvCodeReaderCtrlWrapper.*;
//import MvCodeReaderCtrlWrapper.MvCodeReaderCtrl.*;
//import MvCodeReaderCtrlWrapper.MvCodeReaderCtrlDefine.*;
//
//@Service
//public class ScannerService {
// private Handle hHandle;
//
// @Autowired
// private ScanResultMapper scanResultMapper; // 你需要实现这个Mapper
//
// @PostConstruct
// public void init() {
// ArrayList<MV_CODEREADER_DEVICE_INFO> stCamList = MvCodeReaderCtrl.MV_CODEREADER_EnumDevices();
// if (stCamList == null || stCamList.isEmpty()) {
// System.out.println("Find No Device!");
// return;
// }
// MV_CODEREADER_DEVICE_INFO deviceInfo = stCamList.get(0);
// try {
// hHandle = MvCodeReaderCtrl.MV_CODEREADER_CreateHandle(deviceInfo);
// } catch (ParameterException e) {
// throw new RuntimeException(e);
// }
// if (hHandle == null) {
// System.out.println("Create handle failed!");
// return;
// }
// int nRet = MvCodeReaderCtrl.MV_CODEREADER_OpenDevice(hHandle);
// if (nRet != 0) {
// System.out.println("Open Device fail! nRet[" + String.format("0x%x", nRet) + "]");
// return;
// }
// MvCodeReaderCtrl.MV_CODEREADER_RegisterImageCallBackEx2(hHandle, (pdata, stOutInfo) -> {
// handleScanResult(stOutInfo);
// return 0;
// });
// MvCodeReaderCtrl.MV_CODEREADER_StartGrabbing(hHandle);
// }
//
// private void handleScanResult(MV_CODEREADER_IMAGE_OUT_INFO_EX2 stOutInfo) {
// for (int a = 0; a < stOutInfo.pstCodeListEx.nCodeNum; a++) {
// String code = stOutInfo.pstCodeListEx.stBcrInfoEx.get(a).chCode;
// // 保存到数据库
// ScanResultEntity entity = new ScanResultEntity();
// entity.setCode(code);
// scanResultMapper.insert(entity);
// }
// }
//
// @PreDestroy
// public void destroy() {
// if (hHandle != null) {
// MvCodeReaderCtrl.MV_CODEREADER_StopGrabbing(hHandle);
// MvCodeReaderCtrl.MV_CODEREADER_DestroyHandle(hHandle);
// }
// }
//}