Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用JNI用java调用C方法(扫描器API)_Java_C++_Java Native Interface_Usb_Native - Fatal编程技术网

如何使用JNI用java调用C方法(扫描器API)

如何使用JNI用java调用C方法(扫描器API),java,c++,java-native-interface,usb,native,Java,C++,Java Native Interface,Usb,Native,我有一个包含DLL(LsApi.DLL)的ScannerAPI,我必须使用JNI在JAVA中使用它的方法。我尝试使用第一种方法(LSConnect),如果设备连接正确,则返回一个整数LS\u ok,如果没有,则返回LS\u PERIPHERAL\u NOT\u,不幸的是,我总是会出错 这是我的Java代码: public class Scanner { public final static short LS_515_USB = 502; private static final short

我有一个包含DLL(LsApi.DLL)的ScannerAPI,我必须使用JNI在JAVA中使用它的方法。我尝试使用第一种方法(LSConnect),如果设备连接正确,则返回一个整数
LS\u ok
,如果没有,则返回
LS\u PERIPHERAL\u NOT\u
,不幸的是,我总是会出错

这是我的Java代码:

public class Scanner {

public final static short LS_515_USB = 502;
private static final short hconnect = 0;
public final int LS_OKAY = 0;

public native static int LSConnect(int hWnd, int hlnst, short Peripheral,
        short hConnect);

static {
    System.loadLibrary("lsApi");
}

/**
 * @param args
 */
public static void main(String[] args) {
    new Scanner();
    System.out.println("connexion établie: "
            + Scanner.LSConnect(0, 0, LS_515_USB, hconnect));

}
}

这是一个C#定义所有方法的示例:

C#定义

命名空间Windows应用程序1

{


您不能仅通过调用LoadLibrary和定义本机方法(与C#)直接通过JNI调用.dll。您需要在C中编写一个“桥”库(separate.dll),用于包装lsapi.dll并将JNI调用约定转换为lsapi调用约定


我强烈建议您使用JNI DLL,而不是编写自己的JNI DLL。使用JNA调用.DLL几乎是件小事。web上有大量的资源可以提供帮助,在stackoverflow上搜索Java+JNA会提供很多帮助。

您记得写过吗?@Elliott Frisch,我认为如果我们没有fin,使用存根包装器会很有帮助所有的DLL文件还没有,但是.DLL文件已经生成并且运行良好。我只需要用JAVA使用这个.DLL文件的方法。
public partial class Form1 : Form

{      



    // from lsapi.h

    [DllImport(@"lsapi.dll")]

    internal extern static int LSConnect(int hWnd, int hInst, short Peripheral,ref short hConnect);



    [DllImport(@"lsapi.dll")]

    internal extern static int LSDisconnect (short hConnect,int hWnd);


    [DllImport(@"lsapi.dll")]

    internal extern static int LSSaveJPEG( int hwnd, int hImage,int quality, string filename );



    [DllImport(@"lsapi.dll")]

    internal extern static int LSSaveDIB(int hWnd, int hImage, string filename);


    // Parameter Type peripheral

    public const short LS_5xx_SCSI    =          500;

    public const short LS_515_USB     =          502;

    public const short LS_800_USB     =          801;



    // ------------------------------------------------------------------------

    //                          REPLY-CODE

    // ------------------------------------------------------------------------



    public const int                 LS_OKAY                                                        =                      0;



    // ------------------------------------------------------------------------

    //                  ERRORS

    // ------------------------------------------------------------------------

    public const int                 LS_SYSTEM_ERROR                                       =                      -1;

    public const int                 LS_USB_ERROR                                             =                      -2;

    public const int                 LS_PERIPHERAL_NOT_FOUND                       =                      -3;

    public const int                 LS_HARDWARE_ERROR                                 =                      -4;

    public const int                 LS_PERIPHERAL_OFF_ON                              =                      -5;

    public const int                 LS_RESERVED_ERROR                                  =                      -6;

    public const int                 LS_BOURRAGE                                               =                      -7;

    public const int                 LS_PAPER_JAM                                              =                      -7;

    public const int                 LS_TARGET_BUSY                                          =                      -8;

    public const int                 LS_INVALID_COMMAND                                  =                      -9;

    public const int                 LS_DATA_LOST                                              =                      -10;

    public const int                 LS_COMMAND_IN_EXECUTION_YET               =                      -11;

    public const int                 LS_JPEG_ERROR                                           =                      -12;

    public const int                 LS_COMMAND_SEQUENCE_ERROR               =                      -13;

    public const int                 LS_PC_HW_ERROR                                        =                      -14;

    public const int                 LS_IMAGE_OVERWRITE                                 =                      -15;

    public const int                 LS_INVALID_HANDLE                                     =                      -16;

    public const int                 LS_NO_LIBRARY_LOAD                                  =                      -17;

    public const int                 LS_BMP_ERROR                                             =                      -18;

    public const int                 LS_TIFF_ERROR                                             =                      -19;

    public const int                 LS_IMAGE_NO_MORE_AVAILABLE                 =                      -20;

    public const int                 LS_IMAGE_NO_FILMED                                  =                      -21;

    public const int                 LS_IMAGE_NOT_PRESENT                              =                      -22;

    public const int                 LS_FUNCTION_NOT_AVAILABLE                     =                      -23;

    public const int                 LS_DOCUMENT_NOT_SUPPORTED                 =                      -24;

    public const int                 LS_BARCODE_ERROR                        =                      -25;

    public const int                 LS_INVALID_LIBRARY                                     =                      -26;

    public const int                 LS_INVALID_IMAGE                                        =                      -27;

    public const int                 LS_INVALID_IMAGE_FORMAT             =                      -28;

    public const int                 LS_INVALID_BARCODE_TYPE            =                      -29;

    public const int                 LS_OPEN_NOT_DONE                         =                      -30;

    public const int                 LS_INVALID_TYPE_COMMAND                       =                      -31;

    public const int                 LS_INVALID_CLEARBLACK                             =                      -32;

    public const int                 LS_INVALID_SIDE                                           =                      -33;

    public const int                 LS_MISSING_IMAGE                                       =                      -34;

    public const int                 LS_INVALID_TYPE                                          =                      -35;

    public const int                 LS_INVALID_SAVEMODE                                =                      -36;

    public const int                 LS_INVALID_PAGE_NUMBER                          =                      -37;

    public const int                 LS_INVALID_NRIMAGE                                    =                      -38;

    public const int                 LS_INVALID_STAMP                                       =                      -39;

    public const int                 LS_INVALID_WAITTIMEOUT                            =                      -40;

    public const int                 LS_INVALID_VALIDATE                                   =                      -41;

    public const int                 LS_INVALID_CODELINE_TYPE                        =                      -42;

    public const int                 LS_MISSING_NRIMAGE                                   =                      -43;

    public const int                 LS_INVALID_SCANMODE                                =                      -44;

    public const int                 LS_INVALID_BEEP                                          =                      -45;

    public const int                 LS_INVALID_FEEDER                                     =                      -46;

    public const int                 LS_INVALID_SORTER                                     =                      -47;

    public const int                 LS_INVALID_BADGE_TRACK                          =                      -48;

    public const int                 LS_MISSING_FILENAME                                 =                      -49;

    public const int                 LS_INVALID_QUALITY                                     =                      -50;

    public const int                 LS_INVALID_FILEFORMAT                              =                      -51;

    public const int                 LS_INVALID_COORDINATE                             =                      -52;

    public const int                 LS_MISSING_HANDLE_VARIABLE                   =                      -53;

    public const int                 LS_INVALID_POLO_FILTER                             =                      -54;

    public const int                 LS_INVALID_ORIGIN_MEASURES                   =                      -55;

    public const int                 LS_INVALID_SIZEH_VALUE                             =                      -56;

    public const int                 LS_INVALID_FORMAT                                     =                      -57;

    public const int                 LS_STRINGS_TOO_LONGS                              =                      -58;

    public const int                 LS_READ_IMAGE_FAILED                              =                      -59;

    public const int                 LS_INVALID_CMD_HISTORY                           =                      -60;

    public const int                 LS_MISSING_BUFFER_HISTORY                     =                      -61;

    public const int                 LS_INVALID_ANSWER                                    =                      -62;

    public const int                 LS_OPEN_FILE_ERROR_OR_NOT_FOUND       =                      -63;

    public const int                 LS_READ_TIMEOUT_EXPIRED            =                      -64;

    public const int                 LS_INVALID_METHOD                                     =                      -65;

    public const int                 LS_CALIBRATION_FAILED                              =                      -66;

    public const int                 LS_INVALID_SAVEIMAGE                               =                      -67;

    public const int                 LS_INVALID_UNIT                                           =                      -68;

    public const int                 LS_INVALID_NRWINDOWS                             =                      -71;

    public const int                 LS_INVALID_VALUE                                        =                      -72;

    public const int                 LS_ILLEGAL_REQUEST                                   =                      -73;

    public const int                 LS_INVALID_NR_CRITERIA                             =                      -74;

    public const int                 LS_MISSING_CRITERIA_STRUCTURE  =                      -75;

    public const int                 LS_INVALID_MOVEMENT                                =                      -76;

    public const int                 LS_INVALID_DEGREE                                     =                      -77;

    public const int                 LS_R0TATE_ERROR                                        =                      -78;

    public const int                 LS_MICR_VALUE_OUT_OF_RANGE                 =                      -79;

    public const int                 LS_PERIPHERAL_RESERVED                         =                      -80;

    public const int                 LS_INVALID_NCHANGE                                   =                      -81;

    public const int                 LS_BRIGHTNESS_ERROR                               =                      -82;

    public const int                 LS_CONTRAST_ERROR                                   =                      -83;

    public const int                 LS_INVALID_SIDETOPRINT                             =                      -84;

    public const int                 LS_DOUBLE_LEAFING_ERROR                       =                      -85;

    public const int                 LS_INVALID_BADGE_TIMEOUT                       =                      -86;

    public const int                 LS_INVALID_RESET_TYPE                              =                      -87;

    public const int                 LS_MISSING_SET_CALLBACK             =                      -88;

    public const int                 LS_IMAGE_NOT_200_DPI                                =                      -89;

    public const int                 LS_DOWNLOAD_ERROR                                 =                      -90;

    public const int                 LS_INVALID_SORT_ON_CHOICE                     =                      -91;

    public const int                 LS_INVALID_FONT                                          =                      -92;

    public const int                 LS_INVALID_UNIT_SPEED                              =                      -93;

    public const int                 LS_INVALID_LENGTH                                      =                      -94;



    public const int                 LS_SCAN_NETTO_IMAGE_NOT_SUPPORTED=                       -521;

    public const int                 LS_256_GRAY_NOT_SUPPORTED                   =                      -522;

    public const int                 LS_INVALID_PATH                                          =                      -523;

    public const int                 LS_MISSING_CALLBACK_FUNCTION              =                      -526;

    public const int                 LS_INVALID_OCR_IMAGE_SIDE                      =                      -558;

    public const int                 LS_PERIPHERAL_NOT_ANSWER                    =                      -599;



    public const int                 LS_INVALID_CONNECTION_HANDLE              =                      -1000;

    public const int                 LS_INVALID_CONNECT_PERIPHERAL =                      -1001;

    public const int                 LS_PERIPHERAL_NOT_YET_INTEGRATE         =                      -1002;

    public const int                 LS_UNKNOW_PERIPHERAL_REPLY                =                      -1003;