1. What is JNA?
JNA(Java Native Access) is an open source framework that calls native code in Java, providing a simple and efficient way to access local dynamic link libraries (e.g..dll
、.so
document).
2. Steps to use JNA
1. Introduce dependencies
existAdd the following content to the file:
<dependency> <groupId></groupId> <artifactId>jna</artifactId> <version>5.15.0</version> </dependency>
2. Define the interface
Jinglun Card Reader Official Website provides a 32-bitdocument. First load this DLL file in the interface file, and then define the method that needs to be called in the interface file.
Interfaces need inheritance. The detailed code of the local interface is as follows:
import ; import ; /** * Jinglun ID card reader */ public interface JLLibrary extends Library { JLLibrary INSTANCE = ("D:\\23-demo\\Sdtapi", ); /** * Open the serial port * @param port port number * @return 1 - Correct; Others - Failed */ int InitComm(int port); /** * Card certification * @return 1 - Correct; Others - Failed */ int Authenticate(); /** * Read card information * @param name * @param gender * @param nation * @param birthday date of birth, format: yyyyMMdd * @param code ID number * @param address * @param agency visa agency information * @param beginDate Validity (start date) * @param endDate Validity (end date) * @return 1 - Correct; Others - Failed */ int ReadBaseInfos(byte[] name, byte[] gender, byte[] nation, byte[] birthday, byte[] code, byte[] address, byte[] agency, byte[] beginDate, byte[] endDate); /** * Close the serial port * @return 1 - Correct; Others - Failed */ int CloseComm(); }
The method name, parameters and return results are exactly the same as those in the docking documents provided by Jinglun.
3. Call interface method
The process of reading ID card information by Jinglun reader:
1. Open the serial port 2. Card authentication 3. Read the ID card information 4. Close the serial port
The next thing to do is to call the interface method according to the above steps. Part of the code that calls the interface method is as follows:
// 1. Open the serial port, refer to the official docking document for specific port valuesint deviceResult = (1001); if (deviceResult == 1) { // 2. Card certification (); byte[] name = new byte[31]; byte[] gender = new byte[3]; byte[] nation = new byte[10]; byte[] birthday = new byte[9]; byte[] code = new byte[19]; byte[] address = new byte[71]; byte[] agency = new byte[31]; byte[] beginDate = new byte[9]; byte[] endDate = new byte[9]; // 3. Read basic information int readResult = (name, gender, nation, birthday, code, address, agency, beginDate, endDate); // Analysis (new String(name, "GBK")); (new String(gender, "GBK")); (new String(nation, "GBK")); (new String(birthday, "GBK")); (new String(code, "GBK")); (new String(address, "GBK")); (new String(agency, "GBK")); (new String(beginDate, "GBK")); (new String(endDate, "GBK")); // 4. Close the serial port deviceResult = (); if (deviceResult == 1) { ("Identity reader is shut down successfully."); } else { ("Identity reader shutdown failed!"); } } else { ("Identity card reader failed to open!"); }
4. Run the program
There is no need to tell the step of running the program itself. It’s just that the computer developed by the author Wang Xiaocheng is a WIN10 64-bit system and cannot call a 32-bit DLL file, so the program cannot be run directly in IDEA. The method used by the author Wang Xiaocheng here is to type the program into a JAR, then install a 32-bit JDK on the development computer, and finally use the 32-bit JDK to run the packaged JAR file.
This is the end of this article about Java using JNA to call DLL files. For more related Java JNA to call DLL files, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!