gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 03:02:43 25 4
gpt4 key购买 nike

我有一个包含 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 编写一个“桥接”库(单独的 .dll)来包装 lsapi.dll 并将 JNI 调用约定转换为 lsapi 调用约定。

我强烈推荐使用 JNA而不是编写您自己的 JNI DLL。使用 JNA 调用 .dll 非常简单。网上有很多资源可以提供帮助,在 stackoverflow 上搜索 Java + JNA 会得到很多帮助。

关于java - 如何使用 JNI 用 java 调用 C 方法(扫描仪 API),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20352229/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com