我在读取通过 USB 连接的扫描仪时遇到问题。该方法 (LSConnect) 的返回值始终是相同的“未找到设备”。通过阅读.NET中的示例,我发现他们使用了其他参数,如 IntPtr、IntPtr.Zero、ref Int ...,我必须在 JAVA 中使用 JNA 来读取本机代码。
这是 C# 中的 LSApi.dll 文档示例:
[DllImport(@"lsapi.dll")]
internal extern static int LSConnect(int hWnd, int hInst, short Peripheral,ref short hConnect); internal extern static int LSDocHandle (short hConnect, int hWnd, short Stamp, short Validate, short CodeLine,byte Side,short ScanMode,short Feeder,short Sorter, short WaitTimeout,short Beep,ref int NrDoc,short Reserved1,int Reserved2);
但是当我看到他们之前在 .NET 上所做的事情时: 1/ 声明:
public static extern int LSConnect(IntPtr hwnd, IntPtr HInst, int Peripheral, ref int hConnect);
public static extern int LSDocHandle(int hConnect,
IntPtr hWnd,
int Stamp,
int Validate,
int CodeLine,
char Side,
int ScanMode,
int Feeder,
int Sorter,
int WaitTimeout,
int Beep,
ref uint NrDoc,
int Reserved1,
int Reserved2);
2/主要始终在.Net中:
int b = -2;
uint c = 0;
IntPtr frontimg = IntPtr.Zero;
IntPtr backimg = IntPtr.Zero;
IntPtr R1 = IntPtr.Zero;
IntPtr R2 = IntPtr.Zero;
LsApi.LSConnect(IntPtr.Zero, IntPtr.Zero, 502, ref b);
LsApi.LSDocHandle(b, IntPtr.Zero, LsApi.NO_STAMP, LsApi.NO_PRINT_VALIDATE, (int)LsApi.Codeline.NO_READ_CODELINE, (char)LsApi.Side.SIDE_FRONT_IMAGE, (int)LsApi.ScanMode.SCAN_MODE_COLOR_100, LsApi.AUTO_FEED, (int)LsApi.Sorter.SORTER_BAY1, LsApi.WAIT_NO, (int)LsApi.Beep.NO_BEEP, ref c, 0, 0).ToString();
LsApi.LSReadImage(b, IntPtr.Zero, LsApi.CLEAR_ALL_BLACK, (char)LsApi.Side.SIDE_FRONT_IMAGE, 0, 0, ref frontimg, ref backimg, ref R1, ref R2);
LsApi.LSDisconnect(b, IntPtr.Zero);
我在 JAVA 中声明了我的方法,就像在 C# 中的文档示例中提到的那样,但我认为正确的方法是遵循 .Net 示例
这是我的JAVA代码:
public int LSConnect(int hWnd, int hInst, short i, ShortByReference hConnect);
public int LSDisconnect(short hConnect, IntByReference hWnd);
public int LSDocHandle(short hConnect, int hWnd, short Stamp,
short Validate, short CodeLine, byte Side, short ScanMode,
short Feeder, short Sorter, short WaitTimeout, short Beep,
IntByReference NrDoc, short Reserved1, int Reserved2);
和主班:
public class ConnectExample {
public static void main(String[] args) {
String libName = "lsApi";
JNAUser32 jnaUser32 = (JNAUser32) Native.loadLibrary(libName,
JNAUser32.class);
ShortByReference hConnect = new ShortByReference();
hConnect.setValue((short) 55);
int state = jnaUser32.LSConnect(0, 0, (short) 502, hConnect);
System.out.println(state);
}
}
我刚刚使用了 LSConnect 示例,因为: 1-我必须获得返回值“-1”,表示连接正常 2-我不知道 IntPtr、IntPtr.Zero 和 ref int 等不同参数的等效项? 我对 IntPtr 和 ref int 都使用了 intByReference。
显然相当于
IntPtr
的是 com.sun.jna.Pointer
类。
对于
ref int
来说,IntByReference
应该不错。
@Marouen 我也遇到了同样的问题,LSConnect 返回 0 但我无法运行 LSDocHandle,还有什么其他问题吗,如果你解决了请与我分享