添加海康工业相机demo代码

This commit is contained in:
zc
2026-03-10 16:54:45 +08:00
parent fb05360c5e
commit 41e0b0b5b4
3 changed files with 314 additions and 0 deletions

View 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%

Binary file not shown.

View File

@@ -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();
}
}