Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Point;
- import java.awt.image.BufferedImage;
- import java.awt.image.DataBufferByte;
- import java.awt.image.Raster;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.net.URI;
- import java.net.URISyntaxException;
- import java.nio.file.Files;
- import java.nio.file.Path;
- import java.nio.file.Paths;
- import javax.imageio.ImageIO;
- import com.google.gson.Gson;
- import com.google.gson.JsonElement;
- import com.google.gson.JsonObject;
- import com.hosopy.actioncable.*;
- import SecuGen.FDxSDKPro.jni.*;
- import java.util.Base64;
- import java.util.Random;
- public class FingerprintScanner {
- public static JSGFPLib fpScanner = new JSGFPLib();
- public static Random rand = new Random();
- public static void main(String[] args) throws URISyntaxException {
- // TODO Auto-generated method stub
- System.out.println("Hello World");
- URI uri = new URI("ws://10.154.188.135:3000/cable");
- Consumer consumer = ActionCable.createConsumer(uri);
- consumer.connect();
- Channel fingerprintChannel = new Channel("FingerprintChannel");
- fingerprintChannel.addParam("scan", "value");
- Subscription subscription = consumer.getSubscriptions().create(fingerprintChannel);
- subscription
- .onConnected(new Subscription.ConnectedCallback() {
- @Override
- public void call() {
- System.out.println("Connected!");
- // while(true)
- // try {
- // byte[] imageBuffer = scan(subscription);
- // byte[] template = registerFingerprint(imageBuffer, false, null);
- // validateFingerprintTemplate(subscription, template);
- // } catch (IOException e) {
- // // TODO Auto-generated catch block
- // e.printStackTrace();
- // }
- }
- }).onRejected(new Subscription.RejectedCallback() {
- @Override
- public void call() {
- System.out.println("Rejected!");
- }
- }).onReceived(new Subscription.ReceivedCallback() {
- @Override
- public void call(JsonElement data) {
- System.out.println("Recieved data!");
- String message = data.getAsJsonObject().get("message").getAsString();
- System.out.println(message);
- if(message.equals("Validate Fingerprint")) {
- boolean valid = false;
- int timeout = 0;
- while(!valid && timeout < 1)
- try {
- byte[] imageBuffer = scan(subscription);
- byte[] template = registerFingerprint(subscription, imageBuffer, false, null);
- valid = validateFingerprintTemplate(subscription, template);
- timeout++;
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- if(!valid) {
- JsonObject params = new JsonObject();
- params.addProperty("message", "Timeout");
- subscription.perform("scan", params);
- }
- } else if(message.split(":")[0].equals("Register Fingerprint")) {
- byte[] template = null;
- int registerTimeout = 0;
- while(template == null) {
- byte[] imageBuffer;
- try {
- imageBuffer = scan(subscription);
- template = registerFingerprint(subscription, imageBuffer, true, message.split(":")[1]);
- } catch (IOException e) {
- e.printStackTrace();
- }
- registerTimeout++;
- }
- System.out.println("\n\n\n\n Exited While loop");
- }
- }
- }).onDisconnected(new Subscription.DisconnectedCallback() {
- @Override
- public void call() {
- System.out.println("Disconnected!");
- }
- }).onFailed(new Subscription.FailedCallback() {
- @Override
- public void call(ActionCableException e) {
- System.out.println(e.toString());
- }
- });
- }
- @SuppressWarnings("null")
- public static byte[] scan(Subscription subscription) throws IOException {
- System.out.println(fpScanner.Init(SGFDxDeviceName.SG_DEV_AUTO));
- System.out.println(fpScanner.OpenDevice(SGFDxDeviceName.SG_DEV_AUTO));
- SGDeviceInfoParam info = new SGDeviceInfoParam();
- System.out.println(fpScanner.GetDeviceInfo(info));
- byte[] imageBuffer = new byte[info.imageHeight*info.imageWidth];
- fpScanner.GetImageEx(imageBuffer, 5000, 0, 80);
- JsonObject params = new JsonObject();
- params.addProperty("message", "Scanned fingerprint");
- subscription.perform("scan", params);
- final File outputfile = new File("C:\\Users\\psyha3\\Documents\\fingerprints\\outputimage.png");
- String base64Image = buffer2Image(imageBuffer, "buffer", "outputimage.png");
- JsonObject Base64params = new JsonObject();
- Base64params.addProperty("message", "Base 64 Image: " + base64Image);
- subscription.perform("scan", Base64params);
- return imageBuffer;
- }
- public static byte[] registerFingerprint(Subscription subscription, byte[] imageBuffer, boolean save, String userId) {
- byte[] minBuffer = new byte[400];
- SGFingerInfo finger_info = new SGFingerInfo();
- System.out.println(fpScanner.CreateTemplate(finger_info, imageBuffer, minBuffer));
- if(save)
- try {
- System.out.println("Registering fingerprint");
- String path = "C:\\Users\\psyha3\\Documents\\fingerprints\\templates\\" + userId;
- FileOutputStream stream = new FileOutputStream(path);
- try {
- stream.write(minBuffer);
- JsonObject params = new JsonObject();
- params.addProperty("message", "Registered user:"+userId);
- subscription.perform("scan", params);
- } finally {
- stream.close();
- System.out.println("Closed file");
- }
- } catch (IOException e) {
- System.out.println("Couldn't register fingerprint");
- System.out.println(e);
- }
- return minBuffer;
- }
- public static boolean validateFingerprintTemplate(Subscription subscription, byte[] templateBinary) throws IOException {
- final File folder = new File("C:\\Users\\psyha3\\Documents\\fingerprints\\templates");
- final File outputfile = new File("C:\\Users\\psyha3\\Documents\\fingerprints\\outputimage.png");
- if(!outputfile.exists()) {
- JsonObject params = new JsonObject();
- params.addProperty("message", "Invalid authentication");
- subscription.perform("scan", params);
- return false;
- }
- outputfile.delete();
- for (final File fileEntry : folder.listFiles()) {
- System.out.println("File?");
- if (!fileEntry.isDirectory()) {
- byte[] matchBinary = Files.readAllBytes(Paths.get(fileEntry.getAbsolutePath()));
- boolean[] matched = new boolean[1];
- long sl = SGFDxSecurityLevel.SL_NORMAL;
- fpScanner.MatchTemplate(templateBinary, matchBinary, sl, matched);
- System.out.println(matched);
- if(matched[0] == true) {
- JsonObject params = new JsonObject();
- params.addProperty("message", "Valid authentication");
- subscription.perform("scan", params);
- return true;
- }
- }
- }
- JsonObject params = new JsonObject();
- params.addProperty("message", "Invalid authentication");
- subscription.perform("scan", params);
- return false;
- }
- public static String buffer2Image(byte[] imageBuffer, String bufferName, String imageName) throws IOException {
- BufferedImage img1gray = new BufferedImage(300,400, BufferedImage.TYPE_BYTE_GRAY);
- byte[] imageBuffer11 = ((java.awt.image.DataBufferByte) img1gray.getRaster().getDataBuffer()).getData();
- // Path path = Paths.get(bufferName);
- imageBuffer11 = imageBuffer;
- Files.write(Paths.get(bufferName), imageBuffer11);
- img1gray.setData(Raster.createRaster(img1gray.getSampleModel(), new DataBufferByte(imageBuffer11, imageBuffer11.length), new Point() ) );
- File f = new File("C:\\\\Users\\\\psyha3\\\\Documents\\\\fingerprints\\" + imageName);
- ImageIO.write(img1gray, "JPG", f);
- String encodedImage = Base64.getEncoder().encodeToString(Files.readAllBytes(Paths.get("C:\\\\Users\\\\psyha3\\\\Documents\\\\fingerprints", imageName)));
- return encodedImage;
- }
- private class DataMessage {
- private String message;
- DataMessage(String message) {
- this.message = message;
- }
- }
- }
Add Comment
Please, Sign In to add comment