如何使用JNI用java调用Scanner API的C方法



我有一个包含DLL(LsApi.DLL)的扫描程序API,我必须使用JNI将其方法与JAVA一起使用。我尝试使用第一种方法(LSConnect),如果设备连接正确,则返回整数LS_OKAY,如果没有连接,则返回LS_PERIPHERAL_NOT_FOUND,但不幸的是,我总是收到错误。

这是我的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#定义

命名空间WindowsApplication1

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;

您不能仅通过调用LoadLibrary并定义本机方法(与C#不同)就直接通过JNI调用.dll。您需要用C编写一个"桥接"库(separate.dll),它封装lsapi.dll并将JNI调用约定转换为lsapi调用约定。

我强烈建议使用JNA,而不是编写自己的JNI DLL。使用JNA调用.dll几乎是微不足道的。网络上有很多资源可以提供帮助,在stackoverflow上搜索Java+JNA会得到很多帮助。

最新更新