|
|
@ -0,0 +1,340 @@ |
|
|
|
#include <jni.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <pylonc/PylonC.h>
|
|
|
|
#include "com_my_graphiteDigesterBg_diframe_component_baslerCamera_DiComBaslerCamera.h"
|
|
|
|
|
|
|
|
#define CAMERA(methodName) Java_com_my_graphiteDigesterBg_diframe_component_baslerCamera_DiComBaslerCamera_##methodName
|
|
|
|
|
|
|
|
/**
|
|
|
|
* throw exception to java |
|
|
|
*/ |
|
|
|
void throwException(JNIEnv * env, const char* message) { |
|
|
|
jclass exceptionClass = env->FindClass("java/lang/Exception"); |
|
|
|
if (exceptionClass != nullptr) { |
|
|
|
env->ThrowNew(exceptionClass, message); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/**
|
|
|
|
* throw exception to java by error code |
|
|
|
*/ |
|
|
|
void throwExceptionByErrorCode(JNIEnv* env, GENAPIC_RESULT ret) { |
|
|
|
char msg[128]; |
|
|
|
sprintf_s(msg, "error : %ld", ret); |
|
|
|
throwException(env, msg); |
|
|
|
} |
|
|
|
|
|
|
|
/**
|
|
|
|
* initialize |
|
|
|
*/ |
|
|
|
JNIEXPORT void JNICALL CAMERA(initialize) (JNIEnv* env, jobject) { |
|
|
|
PylonInitialize(); |
|
|
|
} |
|
|
|
|
|
|
|
/**
|
|
|
|
* enumerateDevices |
|
|
|
*/ |
|
|
|
JNIEXPORT jint JNICALL CAMERA(enumerateDevices) (JNIEnv* env, jobject) { |
|
|
|
size_t numDevices = 0; |
|
|
|
GENAPIC_RESULT ret = PylonEnumerateDevices(&numDevices); |
|
|
|
if (GENAPI_E_OK != ret) { |
|
|
|
throwExceptionByErrorCode(env, ret); |
|
|
|
return -1; |
|
|
|
} |
|
|
|
return (jint)numDevices; |
|
|
|
} |
|
|
|
|
|
|
|
/**
|
|
|
|
* terminate |
|
|
|
*/ |
|
|
|
JNIEXPORT void JNICALL CAMERA(terminate)(JNIEnv*, jobject) { |
|
|
|
PylonTerminate(); |
|
|
|
} |
|
|
|
|
|
|
|
/**
|
|
|
|
* createDeviceByIndex |
|
|
|
*/ |
|
|
|
JNIEXPORT jlong JNICALL CAMERA(createDeviceByIndex) (JNIEnv * env, jobject, jint index) { |
|
|
|
/* Get a handle for the first device found. */ |
|
|
|
PYLON_DEVICE_HANDLE * hDev = (PYLON_DEVICE_HANDLE*)malloc(sizeof(PYLON_DEVICE_HANDLE)); |
|
|
|
GENAPIC_RESULT ret = PylonCreateDeviceByIndex(0, hDev); |
|
|
|
if (GENAPI_E_OK != ret) { |
|
|
|
throwExceptionByErrorCode(env, ret); |
|
|
|
return -1; |
|
|
|
} |
|
|
|
return reinterpret_cast<jlong>(hDev); |
|
|
|
} |
|
|
|
|
|
|
|
/**
|
|
|
|
* createDeviceByIndex |
|
|
|
*/ |
|
|
|
JNIEXPORT void JNICALL CAMERA(deviceOpen) (JNIEnv* env, jobject, jlong hDevAddr, jint accessMode) { |
|
|
|
PYLON_DEVICE_HANDLE * hDev = reinterpret_cast<PYLON_DEVICE_HANDLE *>(hDevAddr); |
|
|
|
GENAPIC_RESULT ret = PylonDeviceOpen(*hDev, accessMode); |
|
|
|
if (GENAPI_E_OK != ret) { |
|
|
|
throwExceptionByErrorCode(env, ret); |
|
|
|
return ; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: com_my_graphiteDigesterBg_diframe_component_baslerCamera_DiComBaslerCamera |
|
|
|
* Method: deviceFeatureIsReadable |
|
|
|
* Signature: (JLjava/lang/String;)I |
|
|
|
*/ |
|
|
|
JNIEXPORT jboolean JNICALL CAMERA(deviceFeatureIsReadable) (JNIEnv* env, jobject , jlong hDevAddr, jstring name) { |
|
|
|
PYLON_DEVICE_HANDLE * hDev = reinterpret_cast<PYLON_DEVICE_HANDLE*>(hDevAddr); |
|
|
|
const char* fName = env->GetStringUTFChars(name, NULL); |
|
|
|
if (NULL == fName) { |
|
|
|
throwException(env, "get string value from jstring failed."); |
|
|
|
return JNI_FALSE; |
|
|
|
} |
|
|
|
|
|
|
|
bool isReadable = PylonDeviceFeatureIsReadable(*hDev, fName); |
|
|
|
env->ReleaseStringUTFChars(name, fName); |
|
|
|
return isReadable ? JNI_TRUE : JNI_FALSE; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: com_my_graphiteDigesterBg_diframe_component_baslerCamera_DiComBaslerCamera |
|
|
|
* Method: deviceFeatureToString |
|
|
|
* Signature: (JLjava/lang/String;I)Ljava/lang/String; |
|
|
|
*/ |
|
|
|
JNIEXPORT jstring JNICALL CAMERA(deviceFeatureToString) (JNIEnv* env, jobject, jlong hDevAddr, jstring name, jint size) { |
|
|
|
PYLON_DEVICE_HANDLE* hDev = reinterpret_cast<PYLON_DEVICE_HANDLE*>(hDevAddr); |
|
|
|
const char* fName = env->GetStringUTFChars(name, NULL); |
|
|
|
if (NULL == fName) { |
|
|
|
throwException(env, "get string value from jstring failed."); |
|
|
|
return nullptr; |
|
|
|
} |
|
|
|
|
|
|
|
char * buf = (char *)malloc(size); |
|
|
|
size_t bufSize = size; |
|
|
|
|
|
|
|
GENAPIC_RESULT ret = PylonDeviceFeatureToString(*hDev, fName, buf, &bufSize); |
|
|
|
if (GENAPI_E_OK != ret) { |
|
|
|
throwExceptionByErrorCode(env, ret); |
|
|
|
return nullptr; |
|
|
|
} |
|
|
|
env->ReleaseStringUTFChars(name, fName); |
|
|
|
jstring value = env->NewStringUTF(buf); |
|
|
|
free(buf); |
|
|
|
return value; |
|
|
|
} |
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: com_my_graphiteDigesterBg_diframe_component_baslerCamera_DiComBaslerCamera |
|
|
|
* Method: deviceFeatureIsAvailable |
|
|
|
* Signature: (JLjava/lang/String;)I |
|
|
|
*/ |
|
|
|
JNIEXPORT jboolean JNICALL CAMERA(deviceFeatureIsAvailable) (JNIEnv* env, jobject, jlong hDevAddr, jstring name) { |
|
|
|
PYLON_DEVICE_HANDLE* hDev = reinterpret_cast<PYLON_DEVICE_HANDLE*>(hDevAddr); |
|
|
|
const char* fName = env->GetStringUTFChars(name, NULL); |
|
|
|
if (NULL == fName) { |
|
|
|
throwException(env, "get string value from jstring failed."); |
|
|
|
return JNI_FALSE; |
|
|
|
} |
|
|
|
|
|
|
|
bool isAvail = PylonDeviceFeatureIsAvailable(*hDev, "EnumEntry_PixelFormat_Mono8"); |
|
|
|
env->ReleaseStringUTFChars(name, fName); |
|
|
|
return isAvail; |
|
|
|
} |
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: com_my_graphiteDigesterBg_diframe_component_baslerCamera_DiComBaslerCamera |
|
|
|
* Method: deviceFeatureFromString |
|
|
|
* Signature: (JLjava/lang/String;Ljava/lang/String;)V |
|
|
|
*/ |
|
|
|
JNIEXPORT void JNICALL CAMERA(deviceFeatureFromString)(JNIEnv* env, jobject, jlong hDevAddr, jstring name, jstring value ) { |
|
|
|
PYLON_DEVICE_HANDLE * hDev = reinterpret_cast<PYLON_DEVICE_HANDLE*>(hDevAddr); |
|
|
|
const char* fName = env->GetStringUTFChars(name, NULL); |
|
|
|
if (NULL == fName) { |
|
|
|
throwException(env, "get string value from jstring failed."); |
|
|
|
return; |
|
|
|
} |
|
|
|
const char* fValue = env->GetStringUTFChars(value, NULL); |
|
|
|
if (NULL == fValue) { |
|
|
|
throwException(env, "get string value from jstring failed."); |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
GENAPIC_RESULT ret = PylonDeviceFeatureFromString(*hDev, fName, fValue); |
|
|
|
env->ReleaseStringUTFChars(name, fName); |
|
|
|
env->ReleaseStringUTFChars(value, fValue); |
|
|
|
if (GENAPI_E_OK != ret) { |
|
|
|
throwExceptionByErrorCode(env, ret); |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: com_my_graphiteDigesterBg_diframe_component_baslerCamera_DiComBaslerCamera |
|
|
|
* Method: deviceFeatureIsWritable |
|
|
|
* Signature: (JLjava/lang/String;)I |
|
|
|
*/ |
|
|
|
JNIEXPORT jboolean JNICALL CAMERA(deviceFeatureIsWritable) (JNIEnv* env, jobject, jlong hDevAddr, jstring name) { |
|
|
|
PYLON_DEVICE_HANDLE* hDev = reinterpret_cast<PYLON_DEVICE_HANDLE*>(hDevAddr); |
|
|
|
const char* fName = env->GetStringUTFChars(name, NULL); |
|
|
|
if (NULL == fName) { |
|
|
|
throwException(env, "get string value from jstring failed."); |
|
|
|
return JNI_FALSE; |
|
|
|
} |
|
|
|
|
|
|
|
bool isAvail = PylonDeviceFeatureIsWritable(*hDev, fName); |
|
|
|
env->ReleaseStringUTFChars(name, fName); |
|
|
|
return isAvail ? JNI_TRUE : JNI_FALSE; |
|
|
|
} |
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: com_my_graphiteDigesterBg_diframe_component_baslerCamera_DiComBaslerCamera |
|
|
|
* Method: deviceSetIntegerFeature |
|
|
|
* Signature: (JLjava/lang/String;I)V |
|
|
|
*/ |
|
|
|
JNIEXPORT void JNICALL CAMERA(deviceSetIntegerFeature) (JNIEnv* env, jobject, jlong hDevAddr, jstring name, jint value) { |
|
|
|
PYLON_DEVICE_HANDLE* hDev = reinterpret_cast<PYLON_DEVICE_HANDLE*>(hDevAddr); |
|
|
|
const char* fName = env->GetStringUTFChars(name, NULL); |
|
|
|
if (NULL == fName) { |
|
|
|
throwException(env, "get string value from jstring failed."); |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
GENAPIC_RESULT ret = PylonDeviceSetIntegerFeature(*hDev, fName, value); |
|
|
|
env->ReleaseStringUTFChars(name, fName); |
|
|
|
if (GENAPI_E_OK != ret) { |
|
|
|
throwExceptionByErrorCode(env, ret); |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: com_my_graphiteDigesterBg_diframe_component_baslerCamera_DiComBaslerCamera |
|
|
|
* Method: deviceGetIntegerFeatureInt32 |
|
|
|
* Signature: (JLjava/lang/String;)I |
|
|
|
*/ |
|
|
|
JNIEXPORT jint JNICALL CAMERA(deviceGetIntegerFeatureInt32) (JNIEnv* env, jobject , jlong hDevAddr, jstring name) { |
|
|
|
PYLON_DEVICE_HANDLE* hDev = reinterpret_cast<PYLON_DEVICE_HANDLE*>(hDevAddr); |
|
|
|
const char* fName = env->GetStringUTFChars(name, NULL); |
|
|
|
if (NULL == fName) { |
|
|
|
throwException(env, "get string value from jstring failed."); |
|
|
|
return -1; |
|
|
|
} |
|
|
|
int32_t value; |
|
|
|
|
|
|
|
GENAPIC_RESULT ret = PylonDeviceGetIntegerFeatureInt32(*hDev, fName, &value); |
|
|
|
env->ReleaseStringUTFChars(name, fName); |
|
|
|
if (GENAPI_E_OK != ret) { |
|
|
|
throwExceptionByErrorCode(env, ret); |
|
|
|
return -1; |
|
|
|
} |
|
|
|
return value; |
|
|
|
} |
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: com_my_graphiteDigesterBg_diframe_component_baslerCamera_DiComBaslerCamera |
|
|
|
* Method: deviceGrabSingleFrame |
|
|
|
* Signature: (JI)[B |
|
|
|
*/ |
|
|
|
JNIEXPORT jobject JNICALL CAMERA(deviceGrabSingleFrame)(JNIEnv* env, jobject, jlong hDevAddr, jint channel) { |
|
|
|
PYLON_DEVICE_HANDLE* hDev = reinterpret_cast<PYLON_DEVICE_HANDLE*>(hDevAddr); |
|
|
|
|
|
|
|
int32_t payloadSize = 0; |
|
|
|
GENAPIC_RESULT ret = PylonDeviceGetIntegerFeatureInt32(*hDev, "PayloadSize", &payloadSize); |
|
|
|
if (GENAPI_E_OK != ret) { |
|
|
|
throwExceptionByErrorCode(env, ret); |
|
|
|
return nullptr; |
|
|
|
} |
|
|
|
|
|
|
|
jclass javaGrabResultClass = env->FindClass("com/my/graphiteDigesterBg/diframe/component/baslerCamera/DiComBaslerCamera$GrabResult"); |
|
|
|
if (javaGrabResultClass == NULL) { |
|
|
|
throwException(env, "unable to find java grab result class"); |
|
|
|
return NULL; |
|
|
|
} |
|
|
|
|
|
|
|
jmethodID javaGrabResultConstructor = env->GetMethodID(javaGrabResultClass, "<init>", "()V"); |
|
|
|
if (javaGrabResultConstructor == NULL) { |
|
|
|
throwException(env, "unable to find java grab result class constructor method"); |
|
|
|
return NULL; |
|
|
|
} |
|
|
|
|
|
|
|
jobject javaGrabResult = env->NewObject(javaGrabResultClass, javaGrabResultConstructor); |
|
|
|
if (javaGrabResult == NULL) { |
|
|
|
throwException(env, "failed to create java grab result object"); |
|
|
|
return NULL; |
|
|
|
} |
|
|
|
|
|
|
|
unsigned char* imgBuf; |
|
|
|
imgBuf = (unsigned char*)malloc(payloadSize); |
|
|
|
if (NULL == imgBuf ) { |
|
|
|
throwException(env, "failed to alloc memory for grabbing"); |
|
|
|
return NULL; |
|
|
|
} |
|
|
|
|
|
|
|
PylonGrabResult_t grabResult; |
|
|
|
_Bool bufferReady; |
|
|
|
ret = PylonDeviceGrabSingleFrame(*hDev, channel, imgBuf, payloadSize, &grabResult, &bufferReady, 500); |
|
|
|
if (GENAPI_E_OK == ret && !bufferReady) { |
|
|
|
free(imgBuf); |
|
|
|
throwException(env, "failed to grab frame : timeout"); |
|
|
|
return NULL; |
|
|
|
} |
|
|
|
if (GENAPI_E_OK != ret) { |
|
|
|
free(imgBuf); |
|
|
|
throwExceptionByErrorCode(env, ret); |
|
|
|
return NULL; |
|
|
|
} |
|
|
|
if (grabResult.Status == Failed) { |
|
|
|
free(imgBuf); |
|
|
|
throwException(env, "failed to grab grame"); |
|
|
|
return NULL; |
|
|
|
} |
|
|
|
|
|
|
|
jfieldID sizeXFieldID = env->GetFieldID(javaGrabResultClass, "sizeX", "I"); |
|
|
|
if (sizeXFieldID != NULL) { |
|
|
|
env->SetIntField(javaGrabResult, sizeXFieldID, grabResult.SizeX); |
|
|
|
} |
|
|
|
|
|
|
|
jfieldID sizeYFieldId = env->GetFieldID(javaGrabResultClass, "sizeY", "I"); |
|
|
|
if (sizeYFieldId != NULL) { |
|
|
|
env->SetIntField(javaGrabResult, sizeYFieldId, grabResult.SizeY); |
|
|
|
} |
|
|
|
|
|
|
|
jfieldID imageBufferFieldId = env->GetFieldID(javaGrabResultClass, "imageBuffer", "[B"); |
|
|
|
if (NULL != imageBufferFieldId) { |
|
|
|
jbyteArray jImageBuffer = env->NewByteArray(payloadSize); |
|
|
|
env->SetByteArrayRegion(jImageBuffer, 0, payloadSize, reinterpret_cast<const jbyte*>(imgBuf)); |
|
|
|
env->SetObjectField(javaGrabResult, imageBufferFieldId, jImageBuffer); |
|
|
|
} |
|
|
|
|
|
|
|
free(imgBuf); |
|
|
|
return javaGrabResult; |
|
|
|
} |
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: com_my_graphiteDigesterBg_diframe_component_baslerCamera_DiComBaslerCamera |
|
|
|
* Method: deviceClose |
|
|
|
* Signature: (J)V |
|
|
|
*/ |
|
|
|
JNIEXPORT void JNICALL CAMERA(deviceClose)(JNIEnv* env, jobject, jlong hDevAddr) { |
|
|
|
PYLON_DEVICE_HANDLE* hDev = reinterpret_cast<PYLON_DEVICE_HANDLE*>(hDevAddr); |
|
|
|
GENAPIC_RESULT ret = PylonDeviceClose(*hDev); |
|
|
|
if (GENAPI_E_OK != ret) { |
|
|
|
throwExceptionByErrorCode(env, ret); |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: com_my_graphiteDigesterBg_diframe_component_baslerCamera_DiComBaslerCamera |
|
|
|
* Method: destroyDevice |
|
|
|
* Signature: (J)V |
|
|
|
*/ |
|
|
|
JNIEXPORT void JNICALL CAMERA(destroyDevice)(JNIEnv* env, jobject, jlong hDevAddr) { |
|
|
|
PYLON_DEVICE_HANDLE* hDev = reinterpret_cast<PYLON_DEVICE_HANDLE*>(hDevAddr); |
|
|
|
GENAPIC_RESULT ret = PylonDeviceClose(*hDev); |
|
|
|
if (GENAPI_E_OK != ret) { |
|
|
|
throwExceptionByErrorCode(env, ret); |
|
|
|
return; |
|
|
|
} |
|
|
|
} |