Guest User

Untitled

a guest
May 20th, 2018
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.07 KB | None | 0 0
  1. import java.awt.Point;
  2. import java.awt.image.BufferedImage;
  3. import java.awt.image.DataBufferByte;
  4. import java.awt.image.Raster;
  5. import java.io.File;
  6. import java.io.FileOutputStream;
  7. import java.io.IOException;
  8. import java.net.URI;
  9. import java.net.URISyntaxException;
  10. import java.nio.file.Files;
  11. import java.nio.file.Path;
  12. import java.nio.file.Paths;
  13.  
  14. import javax.imageio.ImageIO;
  15.  
  16. import com.google.gson.Gson;
  17. import com.google.gson.JsonElement;
  18. import com.google.gson.JsonObject;
  19. import com.hosopy.actioncable.*;
  20.  
  21. import SecuGen.FDxSDKPro.jni.*;
  22.  
  23. import java.util.Base64;
  24. import java.util.Random;
  25.  
  26.  
  27. public class FingerprintScanner {
  28. public static JSGFPLib fpScanner = new JSGFPLib();
  29. public static Random rand = new Random();
  30. public static void main(String[] args) throws URISyntaxException {
  31. // TODO Auto-generated method stub
  32. System.out.println("Hello World");
  33. URI uri = new URI("ws://10.154.188.135:3000/cable");
  34. Consumer consumer = ActionCable.createConsumer(uri);
  35. consumer.connect();
  36.  
  37. Channel fingerprintChannel = new Channel("FingerprintChannel");
  38. fingerprintChannel.addParam("scan", "value");
  39. Subscription subscription = consumer.getSubscriptions().create(fingerprintChannel);
  40.  
  41. subscription
  42. .onConnected(new Subscription.ConnectedCallback() {
  43. @Override
  44. public void call() {
  45. System.out.println("Connected!");
  46. // while(true)
  47. // try {
  48. // byte[] imageBuffer = scan(subscription);
  49. // byte[] template = registerFingerprint(imageBuffer, false, null);
  50. // validateFingerprintTemplate(subscription, template);
  51. // } catch (IOException e) {
  52. // // TODO Auto-generated catch block
  53. // e.printStackTrace();
  54. // }
  55. }
  56. }).onRejected(new Subscription.RejectedCallback() {
  57. @Override
  58. public void call() {
  59. System.out.println("Rejected!");
  60. }
  61. }).onReceived(new Subscription.ReceivedCallback() {
  62. @Override
  63. public void call(JsonElement data) {
  64. System.out.println("Recieved data!");
  65. String message = data.getAsJsonObject().get("message").getAsString();
  66.  
  67. System.out.println(message);
  68. if(message.equals("Validate Fingerprint")) {
  69. boolean valid = false;
  70. int timeout = 0;
  71. while(!valid && timeout < 1)
  72. try {
  73. byte[] imageBuffer = scan(subscription);
  74. byte[] template = registerFingerprint(subscription, imageBuffer, false, null);
  75. valid = validateFingerprintTemplate(subscription, template);
  76. timeout++;
  77. } catch (IOException e) {
  78. // TODO Auto-generated catch block
  79. e.printStackTrace();
  80. }
  81. if(!valid) {
  82. JsonObject params = new JsonObject();
  83. params.addProperty("message", "Timeout");
  84. subscription.perform("scan", params);
  85. }
  86.  
  87. } else if(message.split(":")[0].equals("Register Fingerprint")) {
  88. byte[] template = null;
  89. int registerTimeout = 0;
  90. while(template == null) {
  91. byte[] imageBuffer;
  92. try {
  93. imageBuffer = scan(subscription);
  94. template = registerFingerprint(subscription, imageBuffer, true, message.split(":")[1]);
  95. } catch (IOException e) {
  96. e.printStackTrace();
  97. }
  98. registerTimeout++;
  99.  
  100. }
  101. System.out.println("\n\n\n\n Exited While loop");
  102.  
  103. }
  104.  
  105. }
  106. }).onDisconnected(new Subscription.DisconnectedCallback() {
  107. @Override
  108. public void call() {
  109. System.out.println("Disconnected!");
  110. }
  111. }).onFailed(new Subscription.FailedCallback() {
  112. @Override
  113. public void call(ActionCableException e) {
  114. System.out.println(e.toString());
  115. }
  116. });
  117. }
  118. @SuppressWarnings("null")
  119. public static byte[] scan(Subscription subscription) throws IOException {
  120.  
  121. System.out.println(fpScanner.Init(SGFDxDeviceName.SG_DEV_AUTO));
  122. System.out.println(fpScanner.OpenDevice(SGFDxDeviceName.SG_DEV_AUTO));
  123. SGDeviceInfoParam info = new SGDeviceInfoParam();
  124. System.out.println(fpScanner.GetDeviceInfo(info));
  125. byte[] imageBuffer = new byte[info.imageHeight*info.imageWidth];
  126. fpScanner.GetImageEx(imageBuffer, 5000, 0, 80);
  127. JsonObject params = new JsonObject();
  128. params.addProperty("message", "Scanned fingerprint");
  129. subscription.perform("scan", params);
  130.  
  131. final File outputfile = new File("C:\\Users\\psyha3\\Documents\\fingerprints\\outputimage.png");
  132.  
  133. String base64Image = buffer2Image(imageBuffer, "buffer", "outputimage.png");
  134.  
  135. JsonObject Base64params = new JsonObject();
  136. Base64params.addProperty("message", "Base 64 Image: " + base64Image);
  137. subscription.perform("scan", Base64params);
  138.  
  139. return imageBuffer;
  140. }
  141.  
  142. public static byte[] registerFingerprint(Subscription subscription, byte[] imageBuffer, boolean save, String userId) {
  143.  
  144. byte[] minBuffer = new byte[400];
  145. SGFingerInfo finger_info = new SGFingerInfo();
  146.  
  147. System.out.println(fpScanner.CreateTemplate(finger_info, imageBuffer, minBuffer));
  148.  
  149. if(save)
  150. try {
  151. System.out.println("Registering fingerprint");
  152. String path = "C:\\Users\\psyha3\\Documents\\fingerprints\\templates\\" + userId;
  153. FileOutputStream stream = new FileOutputStream(path);
  154. try {
  155. stream.write(minBuffer);
  156. JsonObject params = new JsonObject();
  157. params.addProperty("message", "Registered user:"+userId);
  158. subscription.perform("scan", params);
  159. } finally {
  160. stream.close();
  161. System.out.println("Closed file");
  162. }
  163. } catch (IOException e) {
  164. System.out.println("Couldn't register fingerprint");
  165. System.out.println(e);
  166. }
  167. return minBuffer;
  168. }
  169.  
  170. public static boolean validateFingerprintTemplate(Subscription subscription, byte[] templateBinary) throws IOException {
  171. final File folder = new File("C:\\Users\\psyha3\\Documents\\fingerprints\\templates");
  172. final File outputfile = new File("C:\\Users\\psyha3\\Documents\\fingerprints\\outputimage.png");
  173. if(!outputfile.exists()) {
  174. JsonObject params = new JsonObject();
  175. params.addProperty("message", "Invalid authentication");
  176. subscription.perform("scan", params);
  177.  
  178. return false;
  179. }
  180. outputfile.delete();
  181. for (final File fileEntry : folder.listFiles()) {
  182. System.out.println("File?");
  183. if (!fileEntry.isDirectory()) {
  184. byte[] matchBinary = Files.readAllBytes(Paths.get(fileEntry.getAbsolutePath()));
  185. boolean[] matched = new boolean[1];
  186. long sl = SGFDxSecurityLevel.SL_NORMAL;
  187. fpScanner.MatchTemplate(templateBinary, matchBinary, sl, matched);
  188. System.out.println(matched);
  189. if(matched[0] == true) {
  190. JsonObject params = new JsonObject();
  191. params.addProperty("message", "Valid authentication");
  192. subscription.perform("scan", params);
  193. return true;
  194. }
  195. }
  196. }
  197. JsonObject params = new JsonObject();
  198. params.addProperty("message", "Invalid authentication");
  199. subscription.perform("scan", params);
  200.  
  201. return false;
  202. }
  203.  
  204.  
  205. public static String buffer2Image(byte[] imageBuffer, String bufferName, String imageName) throws IOException {
  206. BufferedImage img1gray = new BufferedImage(300,400, BufferedImage.TYPE_BYTE_GRAY);
  207. byte[] imageBuffer11 = ((java.awt.image.DataBufferByte) img1gray.getRaster().getDataBuffer()).getData();
  208. // Path path = Paths.get(bufferName);
  209. imageBuffer11 = imageBuffer;
  210. Files.write(Paths.get(bufferName), imageBuffer11);
  211. img1gray.setData(Raster.createRaster(img1gray.getSampleModel(), new DataBufferByte(imageBuffer11, imageBuffer11.length), new Point() ) );
  212. File f = new File("C:\\\\Users\\\\psyha3\\\\Documents\\\\fingerprints\\" + imageName);
  213. ImageIO.write(img1gray, "JPG", f);
  214. String encodedImage = Base64.getEncoder().encodeToString(Files.readAllBytes(Paths.get("C:\\\\Users\\\\psyha3\\\\Documents\\\\fingerprints", imageName)));
  215. return encodedImage;
  216. }
  217.  
  218. private class DataMessage {
  219. private String message;
  220. DataMessage(String message) {
  221. this.message = message;
  222. }
  223. }
  224.  
  225. }
Add Comment
Please, Sign In to add comment