Skip to content

Instantly share code, notes, and snippets.

@DevendraKumarL
Last active October 13, 2023 11:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DevendraKumarL/0f958c8dddde54c9d5c91439642e7f9f to your computer and use it in GitHub Desktop.
Save DevendraKumarL/0f958c8dddde54c9d5c91439642e7f9f to your computer and use it in GitHub Desktop.
A sample Java GUI program to demonstrate FP capture for 3M Cogent CSD200 Fingerprint Scanner.
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.FlowLayout;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.border.LineBorder;
import javax.swing.filechooser.FileNameExtensionFilter;
import org.apache.commons.codec.binary.Base64;
import java.awt.Color;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import java.awt.Toolkit;
import java.awt.Font;
import javax.swing.SwingConstants;
import mmm.cogent.fpCaptureApi.CapturedImageData;
import mmm.cogent.fpCaptureApi.DeviceInfo;
import mmm.cogent.fpCaptureApi.IFingerprintCaptureAPI;
import mmm.cogent.fpCaptureApi.IFingerprintCaptureCallbackAPI;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.awt.image.WritableRaster;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.Calendar;
import java.util.Random;
import javax.swing.JTextArea;
public class TestFPCaptureDemo {
mmm.cogent.fpCaptureApi.IFingerprintCaptureAPI fingerprintCaptureApi;
int currentSessionId = -1;
JFrame frame;
JPanel panel;
JButton captureBtn;
JButton initializeBtn;
JButton deinitBtn;
JButton saveRawImgBtn;
JButton saveBMPImgBtn;
JButton resetBtn;
JButton matchBtn;
JLabel captureImgLabel1;
JLabel captureImgLabel2;
JLabel nfiq1;
JLabel nfiq2;
JCheckBox img1Check;
JCheckBox img2Check;
Image fpImageToDisplay1;
Image fpImageToDisplay2;
byte[] displayedImageByteArray1;
byte[] fpBMPBytes1;
byte[] fpFMRBytes1;
byte[] displayedImageByteArray2;
byte[] fpBMPBytes2;
byte[] fpFMRBytes2;
boolean oneFPCaptured = false;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new TestFPCaptureDemo().init();
}
});
}
public void init() {
frame = new JFrame("TestFPCaptureDemo");
frame.setBounds(100, 100, 600, 600);
panel = new JPanel();
panel.setBackground(Color.white);
panel.setLayout(null);
fpBMPBytes1 = null;
fpFMRBytes1 = null;
fingerprintCaptureApi = new mmm.cogent.fpCaptureApi.MMMCogentCSD200DeviceImpl();
fingerprintCaptureApi.initDevice();
matchBtn = new JButton("Match");
matchBtn.setSize(100, 25);
matchBtn.setLocation(250, 50);
panel.add(matchBtn);
matchBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
if(fpFMRBytes1 != null && fpFMRBytes2 != null)
{
boolean bMatch = fingerprintCaptureApi.matchIso19794_2Templates(fpFMRBytes1, fpFMRBytes2);
if(bMatch == true)
{
JOptionPane.showMessageDialog(matchBtn, "Matched.","",JOptionPane.ERROR_MESSAGE);
}
else
{
JOptionPane.showMessageDialog(matchBtn, "Not Matched.","",JOptionPane.INFORMATION_MESSAGE);
}
}
else
{
JOptionPane.showMessageDialog(null, "Unable to match two fingers.");
}
}
});
initializeBtn = new JButton("Initialize");
initializeBtn.setSize(100, 25);
initializeBtn.setLocation(100, 100);
panel.add(initializeBtn);
initializeBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
setDisplayedImageByteArray1(null);
setDisplayedImageByteArray2(null);
Thread t = new Thread(new initWorker());
t.start();
}
});
captureBtn = new JButton("Capture");
panel.add(captureBtn);
captureBtn.setSize(100, 25);
captureBtn.setLocation(250, 100);
captureBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
System.out.println("isDeviceConnected: "+ fingerprintCaptureApi.isDeviceConnected());
System.out.println("isDeviceInitialized: "+ fingerprintCaptureApi.isDeviceInitialized());
if(fingerprintCaptureApi.isDeviceConnected() && fingerprintCaptureApi.isDeviceInitialized() )
{
Random random = new Random(Calendar.getInstance().getTimeInMillis());
currentSessionId = random.nextInt();
fingerprintCaptureApi.startCapture(new CaptureImplementation(), currentSessionId, 30);
} else {
System.out.println("Capture error, initialize the device manually");
}
}
});
deinitBtn = new JButton("Deinit");
panel.add(deinitBtn);
deinitBtn.setSize(100, 25);
deinitBtn.setLocation(400, 100);
deinitBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int deinit = fingerprintCaptureApi.deinitDevice();
if (deinit < 0) {
System.out.println("Deinitialization failed, error code : " + deinit);
} else {
System.out.println("Deinitialization success");
resetEverything();
}
}
});
resetBtn = new JButton("Reset");
resetBtn.setSize(100, 25);
resetBtn.setLocation(250, 450);
panel.add(resetBtn);
resetBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
resetEverything();
}
});
nfiq1 = new JLabel("NFIQ1");
nfiq1.setSize(100, 25);
nfiq1.setLocation(100, 180);
nfiq1.setHorizontalAlignment(SwingConstants.CENTER);
panel.add(nfiq1);
img1Check = new JCheckBox("Cature 1");
img1Check.setSize(100, 25);
img1Check.setLocation(100, 160);
panel.add(img1Check);
captureImgLabel1 = new JLabel("");
captureImgLabel1.setSize(100, 200);
captureImgLabel1.setLocation(100, 200);
captureImgLabel1.setBorder(new LineBorder(Color.black));
panel.add(captureImgLabel1);
nfiq2 = new JLabel("NFIQ2");
nfiq2.setSize(100, 25);
nfiq2.setLocation(300, 180);
nfiq2.setHorizontalAlignment(SwingConstants.CENTER);
panel.add(nfiq2);
img2Check = new JCheckBox("Cature 2");
img2Check.setSize(100, 25);
img2Check.setLocation(300, 160);
panel.add(img2Check);
captureImgLabel2 = new JLabel("");
captureImgLabel2.setSize(100, 200);
captureImgLabel2.setLocation(300, 200);
captureImgLabel2.setBorder(new LineBorder(Color.black));
panel.add(captureImgLabel2);
try {
loadDefaultFpImg();
} catch (IOException e1) {
e1.printStackTrace();
}
saveBMPImgBtn = new JButton("Save BMP Image");
saveBMPImgBtn.setSize(150, 25);
saveBMPImgBtn.setLocation(100, 490);
panel.add(saveBMPImgBtn);
saveBMPImgBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
exportImage(saveBMPImgBtn, "bmp");
}
});
saveRawImgBtn = new JButton("Save Raw Image");
saveRawImgBtn.setSize(150, 25);
saveRawImgBtn.setLocation(350, 490);
panel.add(saveRawImgBtn);
saveRawImgBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
exportImage(saveRawImgBtn, "raw");
}
});
frame.setContentPane(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
protected void exportImage(JButton btn, String imgType) {
if(getDisplayedImageByteArray1() != null)
{
JFileChooser jfc = new JFileChooser("./");
String desp = imgType.equals("bmp") ? "Bitmap Images" : "Raw Images";
String extension = imgType.equals("bmp") ? "bmp" : "raw";
FileNameExtensionFilter filter = new FileNameExtensionFilter(desp, extension);
jfc.setFileFilter(filter);
int retVal = jfc.showSaveDialog(btn);
if(retVal == JFileChooser.APPROVE_OPTION)
{
try{
FileOutputStream fos = null;
try {
fos = new FileOutputStream(jfc.getSelectedFile());
byte[] byteArray = getDisplayedImageByteArray1();
if(byteArray != null){
fos.write(getDisplayedImageByteArray1());
}
} catch (FileNotFoundException ex) {
e.printStackTrace();
}
fos.close();
} catch (IOException exIO) {
e.printStackTrace();
}catch (NullPointerException npe) {
e.printStackTrace();
}
}
}
else
{
JOptionPane.showMessageDialog(null, "No images to save");
}
}
public void loadDefaultFpImg() throws IOException {
captureImgLabel1.setIcon(new ImageIcon(ImageIO.read(new File("C:\\Program Files (x86)\\3M Cogent\\3MCogent-CSD200\\Java\\3M_Logo.gif"))));
captureImgLabel2.setIcon(new ImageIcon(ImageIO.read(new File("C:\\Program Files (x86)\\3M Cogent\\3MCogent-CSD200\\Java\\3M_Logo.gif"))));
}
protected void resetEverything() {
setDisplayedImageByteArray1(null);
setDisplayedImageByteArray2(null);
fpBMPBytes1 = null;
fpFMRBytes1 = null;
fpBMPBytes2 = null;
fpFMRBytes2 = null;
fingerprintCaptureApi.deinitDevice();
try {
loadDefaultFpImg();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Reset Everything");
}
final class initWorker implements Runnable {
public initWorker(){
}
@Override
public void run() {
if (! fingerprintCaptureApi.isDeviceInitialized()) {
System.out.println("Initializing CSD200");
int init = fingerprintCaptureApi.initDevice();
if (init < 0) {
System.out.println("Error iniliazing device, error code : " + init);
} else {
System.out.println("Initialization Success");
}
} else {
System.out.println("Device already initialized");
}
}
}
class CaptureImplementation implements IFingerprintCaptureCallbackAPI {
@Override
public void onFingerprintCaptureCompleted(int sessionId, CapturedImageData capturedImageData) {
System.out.println("FingerprintCaptureCompleted");
byte[] capturedBytes = capturedImageData.getBmpImageData();
printCapturedFPImageDetails(capturedImageData);
if (img1Check.isSelected())
setDisplayedImageByteArray1(capturedBytes);
else if (img2Check.isSelected())
setDisplayedImageByteArray2(capturedBytes);
else {
System.out.println("--- Check one of image checkboxes ---");
return;
}
Toolkit.getDefaultToolkit().beep();
if(null == capturedBytes && IFingerprintCaptureAPI.ERR_CAPTURE_TIMEOUT == capturedImageData.getErrorCode()){
System.out.println("Error: Capture Timeout.");
}
else if(null == capturedBytes && IFingerprintCaptureAPI.ERR_CAPTURING == capturedImageData.getErrorCode())
{
System.out.println("Error: Previous Capture is in progress.");
}
if(capturedBytes != null)
{
if (img1Check.isSelected()) {
fpBMPBytes1 = capturedBytes;
fpFMRBytes1 = capturedImageData.getIso19794_2Template();
nfiq1.setText("NFIQ1 : "+ capturedImageData.getNfiq());
if(fpFMRBytes1 != null){
System.out.println("Fp FMR Size: " + fpFMRBytes1.length);
System.out.println("Fp Minutiae Count: "+ getMinutiaeCount(fpFMRBytes1));
}
System.out.println("NFIQ1 : " + capturedImageData.getNfiq());
showImage1();
} else if (img2Check.isSelected()) {
fpBMPBytes2 = capturedBytes;
fpFMRBytes2 = capturedImageData.getIso19794_2Template();
nfiq2.setText("NFIQ2 : "+ capturedImageData.getNfiq());
if(fpFMRBytes2 != null){
System.out.println("Fp FMR Size: " + fpFMRBytes2.length);
System.out.println("Fp Minutiae Count: "+ getMinutiaeCount(fpFMRBytes2));
}
System.out.println("NFIQ2 : " + capturedImageData.getNfiq());
showImage2();
}
}
}
@Override
public void onPreviewImageAvailable(int sessionId, byte[] bmpBytes, int width, int height) {
if(bmpBytes != null){
if (img1Check.isSelected())
setDisplayedImageByteArray1(bmpBytes);
else if (img2Check.isSelected())
setDisplayedImageByteArray2(bmpBytes);
else {
System.out.println("--- Check one of image checkboxes ---");
return;
}
}
}
private int getMinutiaeCount(byte[] fmrBytes) {
int mntCount = -1;
if(fmrBytes != null && fmrBytes.length > 27){
mntCount = fmrBytes[27];
}
return mntCount;
}
}
public void printCapturedFPImageDetails(CapturedImageData img) {
System.out.println("***** FPCapture Data *****");
System.out.println("BmpImgData : " + img.getBmpImageData().toString());
System.out.println("Width : " + img.getImageWidth() + " Height : " + img.getImageHeight());
System.out.println("Nfiq : " + img.getNfiq());
System.out.println("Iso19794_Template : " + Base64.encodeBase64String(img.getIso19794_2Template()));
System.out.println("Iso19794_Template Len : " + Base64.encodeBase64String(img.getIso19794_2Template()).length());
System.out.println("***** End *****");
}
public void showImage1() {
Image img = getFpImageToDisplay1();
if (img != null) {
ImageIcon imgIcon = new ImageIcon(img.getScaledInstance(-1, 170, Image.SCALE_DEFAULT));
captureImgLabel1.setIcon(imgIcon);
}
}
public void showImage2() {
Image img = getFpImageToDisplay1();
if (img != null) {
ImageIcon imgIcon = new ImageIcon(img.getScaledInstance(-1, 170, Image.SCALE_DEFAULT));
captureImgLabel2.setIcon(imgIcon);
}
}
public byte[] getDisplayedImageByteArray1() {
return displayedImageByteArray1;
}
public void setDisplayedImageByteArray1(byte[] displayedImageByteArray) {
if(displayedImageByteArray == null){
setFpImageToDisplay1(null);
}
else{
BufferedImage capturedImage = null;
try {
capturedImage = ImageIO.read(new ByteArrayInputStream(displayedImageByteArray));
} catch (IOException e) {
e.printStackTrace();
}
setFpImageToDisplay1(capturedImage);
}
this.displayedImageByteArray1 = displayedImageByteArray;
}
public Image getFpImageToDisplay1() {
if(fpImageToDisplay1 == null ){
return new ImageIcon().getImage();
}
return fpImageToDisplay1;
}
public void setFpImageToDisplay1(Image fpImageToDisplay) {
this.fpImageToDisplay1 = fpImageToDisplay;
}
public byte[] getDisplayedImageByteArray2() {
return displayedImageByteArray2;
}
public void setDisplayedImageByteArray2(byte[] displayedImageByteArray) {
if(displayedImageByteArray == null){
setFpImageToDisplay2(null);
}
else{
BufferedImage capturedImage = null;
try {
capturedImage = ImageIO.read(new ByteArrayInputStream(displayedImageByteArray));
} catch (IOException e) {
e.printStackTrace();
}
setFpImageToDisplay2(capturedImage);
}
this.displayedImageByteArray2 = displayedImageByteArray;
}
public Image getFpImageToDisplay2() {
if(fpImageToDisplay2 == null ){
return new ImageIcon().getImage();
}
return fpImageToDisplay2;
}
public void setFpImageToDisplay2(Image fpImageToDisplay) {
this.fpImageToDisplay2 = fpImageToDisplay;
}
}
@meghadhabali
Copy link

Hello Sir,

I am getting this error "The method encodeBase64String(byte[]) is undefined for the type Base64".
Please help me to resolve it.

@premmohanram
Copy link

is any jar files available there?

How to import IFingerprintCaptureAPI.

please help me,thanks

@DevendraKumarL
Copy link
Author

DevendraKumarL commented Aug 16, 2019

@premmohanram For the import to work you will have to add the jars to your project provided 3M cogent drivers.

@DevendraKumarL
Copy link
Author

DevendraKumarL commented Aug 16, 2019

@meghadhabali You can try this instead for the above error: https://stackoverflow.com/questions/29887351/java-the-method-encodebase64. It is just used to print the data as a string.

@heribertofrois
Copy link

is any jar files available there?

How to import IFingerprintCaptureAPI.

please help me,thanks

@MohIrfankhan
Copy link

I not found IFingerprintCaptureAPI. Please give link where i download

@ramzzshanko
Copy link

Where can I get the library for 3M

@ankits57
Copy link

i have pasted link for CSD200

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment