编译Basler相机的Grab.cpp文件以创建JNI时出错

问题描述

我有一个图书馆,可以运行来自以下网站的Basler相机的cpp文件:https://www.baslerweb.com/en/sales-support/downloads/software-downloads/pylon-6-1-1-windows/。我现在想构建一个简单的JNI来从Grab.cpp文件中引用一个句子。 这是我的Java代码:

public class baslerJNI
    {
        static
        {
            System.loadLibrary("camera");
        }
        private native void runCamera();
        public static void main(String[] args)
        {
            new baslerJNI().runCamera();
        }
    }

这是我的h文件:

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class baslerJNI */

#ifndef _Included_baslerJNI
#define _Included_baslerJNI
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     baslerJNI
 * Method:    runCamera
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_baslerJNI_runCamera
  (JNIEnv *,jobject);

#ifdef __cplusplus
}
#endif
#endif

这是cpp文件,我在文件末尾添加了JNIEXPORT:

// Grab.cpp
/*
    Note: Before getting started,Basler recommends reading the "Programmer's Guide" topic
    in the pylon C++ API documentation delivered with pylon.
    If you are upgrading to a higher major version of pylon,Basler also
    strongly recommends reading the "Migrating from Previous Versions" topic in the pylon C++ API documentation.

    This sample illustrates how to grab and process images using the CInstantCamera class.
    The images are grabbed and processed asynchronously,i.e.,while the application is processing a buffer,the acquisition of the next buffer is done
    in parallel.

    The CInstantCamera class uses a pool of buffers to retrieve image data
    from the camera device. Once a buffer is filled and ready,the buffer can be retrieved from the camera object for processing. The buffer
    and additional image data are collected in a grab result. The grab result is
    held by a smart pointer after retrieval. The buffer is automatically reused
    when explicitly released or when the smart pointer object is destroyed.
*/

#include <jni.h>
#include <stdio.h>
// C:\Program Files\Java\jdk1.8.0_261\include\jni.h
#include <iostream>
#include "baslerJNI.h"

// Include files to use the pylon API.
#include <pylon/PylonIncludes.h>
//#include <C:\Users\Documents\Basler\pylon 6\Development\include\pylon\PylonIncludes.h>
#ifdef PYLON_WIN_BUILD
#    include <pylon/PylonGUI.h>
#endif

// Namespace for using pylon objects.
using namespace Pylon;

// Namespace for using cout.
using namespace std;

// Number of images to be grabbed.
static const uint32_t c_countOfImagesToGrab = 100; //look for camera.StartGrabbing()

uint32_t runBasler()
{
    uint32_t returnBuffer;
    // The exit code of the sample application.
    int exitCode = 0;

    // Before using any pylon methods,the pylon runtime must be initialized. 
    PylonInitialize();

    try
    {
        // Create an instant camera object with the camera device found first.
        CInstantCamera camera(CTlFactory::GetInstance().CreateFirstDevice());

        // Print the model name of the camera.
        cout << "Using device " << camera.GetDeviceInfo().GetModelName() << endl;

        // The parameter MaxNumBuffer can be used to control the count of buffers
        // allocated for grabbing. The default value of this parameter is 10.
        camera.MaxNumBuffer = 5;

        // Start the grabbing of c_countOfImagesToGrab images.
        // The camera device is parameterized with a default configuration which
        // sets up free-running continuous acquisition.
        //camera.StartGrabbing(c_countOfImagesToGrab); //limited number of grabbing
        camera.StartGrabbing(); //infinite number of grabbing

        // This smart pointer will receive the grab result data.
        CGrabResultPtr ptrGrabResult;

        // Camera.StopGrabbing() is called automatically by the RetrieveResult() method
        // when c_countOfImagesToGrab images have been retrieved.
        while (camera.IsGrabbing())
        {
            // Wait for an image and then retrieve it. A timeout of 5000 ms is used.
            camera.RetrieveResult(5000,ptrGrabResult,TimeoutHandling_ThrowException);

            // Image grabbed successfully?
            if (ptrGrabResult->GrabSucceeded())
            {
                // Access the image data.
                cout << "SizeX: " << ptrGrabResult->GetWidth() << endl;
                cout << "SizeY: " << ptrGrabResult->GetHeight() << endl;
                const uint8_t* pImageBuffer = (uint8_t*)ptrGrabResult->GetBuffer();
                cout << "Gray value of first pixel: " << (uint32_t)pImageBuffer[0] << endl << endl;
                returnBuffer = (uint32_t)pImageBuffer;

#ifdef PYLON_WIN_BUILD
                // Display the grabbed image.
                //Pylon::DisplayImage(1,ptrGrabResult);
#endif
            }
            else
            {
                cout << "Error: " << ptrGrabResult->GetErrorCode() << " " << ptrGrabResult->GetErrorDescription() << endl;
            }
        }
    }
    catch (const GenericException& e)
    {
        // Error handling.
        cerr << "An exception occurred." << endl
            << e.GetDescription() << endl;
        exitCode = 1;
    }

    // Comment the following two lines to disable waiting on exit.
    cerr << endl << "Press enter to exit." << endl;
    while (cin.get() != '\n');

    // Releases all pylon resources. 
    PylonTerminate();

    //return exitCode;
    return returnBuffer;
}

int main(int argc,char* argv[])
{
    uint32_t returnArray = runBasler();
    //return exitCode;

}

JNIEXPORT void JNICALL Java_baslerJNI_runCamera(JNIEnv* env,jobject thisObj)
{
    cout << "sending from cpp to java" << endl;
    //uint32_t returnArray = runBasler();
    return;
}

我的环境是Windows10。我正在使用Visual Studio2019。单击重建时,会出现很多错误。

enter image description here

我也使用了该网站上的代码来制作dll文件:https://www3.ntu.edu.sg/home/ehchua/programming/java/JavaNativeInterface.html 我的文件夹中有以下文件:

2020-09-04  11:12 AM    <DIR>          .
2020-09-04  11:12 AM    <DIR>          ..
2020-09-03  07:41 PM            12,288 .baslerJNI.java.swo
2020-09-03  07:42 PM               529 .baslerJNI.java.un~
2020-09-03  07:44 PM               453 baslerJNI.class
2020-09-03  07:44 PM               401 baslerJNI.h
2020-09-03  07:42 PM               196 baslerJNI.java
2020-09-03  12:43 PM               198 baslerJNI.java~
2020-09-01  12:36 PM    <DIR>          Debug
2020-09-04  11:01 AM             4,972 Grab.cpp
2020-09-01  01:38 PM            10,275 Grab.vcxproj
2020-09-01  01:38 PM               631 Grab.vcxproj.filters
2020-09-01  12:36 PM               168 Grab.vcxproj.user
2020-09-04  11:02 AM    <DIR>          Release_x64
2020-09-03  01:04 PM                 0 x86_64-w64-mingw32-g++
              11 File(s)         30,111 bytes
               4 Dir(s)  155,886,912 bytes free

这是根据网站编译cpp并生成dll的代码:

x86_64-w64-mingw32-g++ -I"%JAVA_HOME%\include" -I"%JAVA_HOME%\include\win32" -shared -o camera.dll Grab.cpp

我正在使用Visual Studio打开的命令提示符。但是,出现此错误:

Grab.cpp:28:10: fatal error: pylon/PylonIncludes.h: No such file or directory
   28 | #include <pylon/PylonIncludes.h>
      |          ^~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.

在不使用任何JNI代码的情况下编译Grab.cpp时,我没有收到此错误。我尝试将其地址添加到环境变量(C:\Users\Documents\Basler\pylon 6\Development\include)中的PATH中,但是仍然遇到相同的错误。 抱歉,问题很长。该项目非常复杂。如果您能帮助我解决这个问题,非常感谢您的帮助。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...