0% found this document useful (0 votes)
53 views10 pages

Android Fingerprint Scanner Code

This document contains code for an Android application that uses a fingerprint scanner device. It imports necessary libraries, defines an activity class that implements the scanner device callbacks, and includes methods for initializing the scanner, capturing fingerprints, and extracting fingerprint template and image data.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views10 pages

Android Fingerprint Scanner Code

This document contains code for an Android application that uses a fingerprint scanner device. It imports necessary libraries, defines an activity class that implements the scanner device callbacks, and includes methods for initializing the scanner, capturing fingerprints, and extracting fingerprint template and image data.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

package [Link].

FingerprintScanner;

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link].mfs100.MFS100;
import [Link].mfs100.MFS100Event;

import [Link];
import [Link];
import [Link];

public class MFS100CodeHubs extends Activity implements MFS100Event {


private static long Threshold = 1500;
private static long mLastClkTime = 0;
byte[] Enroll_Template;
byte[] Verify_Template;
Button btnClearLog;
Button btnExtractAnsi;
Button btnExtractISOImage;
Button btnExtractWSQImage;
Button btnInit;
Button btnMatchISOTemplate;
Button btnStopCapture;
Button btnSyncCapture;
Button btnUninit;
CheckBox cbFastDetection;
ImageView imgFinger;
private boolean isCaptureRunning = false;
private FingerData lastCapFingerData = null;
TextView lblMessage;
private long mLastAttTime = 0;
long mLastDttTime = 0;
MFS100 mfs100 = null;
ScannerAction scannerAction = [Link];
int timeout = 10000;
EditText txtEventLog;

/* access modifiers changed from: private */


public enum ScannerAction {
Capture,
Verify
}

/* access modifiers changed from: protected */


public void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_mfs100_sample);
FindFormControls();
try {
getWindow().setSoftInputMode(3);
} catch (Exception e) {
Log.e("Error", [Link]());
}
try {
this.mfs100 = new MFS100(this);
[Link](this);
} catch (Exception e2) {
[Link]();
}
}

/* access modifiers changed from: protected */


public void onStart() {
try {
if (this.mfs100 == null) {
this.mfs100 = new MFS100(this);
[Link](this);
} else {
InitScanner();
}
} catch (Exception e) {
[Link]();
}
[Link]();
}

/* access modifiers changed from: protected */


public void onStop() {
try {
if ([Link]) {
[Link]();
}
[Link](500);
} catch (InterruptedException e) {
[Link]();
}
[Link]();
}

/* access modifiers changed from: protected */


public void onResume() {
[Link]();
}

/* access modifiers changed from: protected */


public void onDestroy() {
try {
if (this.mfs100 != null) {
[Link]();
}
} catch (Exception e) {
[Link]();
}
[Link]();
}

public void FindFormControls() {


try {
[Link] = (Button) findViewById([Link]);
[Link] = (Button) findViewById([Link]);
[Link] = (Button)
findViewById([Link]);
[Link] = (Button)
findViewById([Link]);
[Link] = (Button) findViewById([Link]);
[Link] = (Button)
findViewById([Link]);
[Link] = (Button) findViewById([Link]);
[Link] = (TextView) findViewById([Link]);
[Link] = (EditText) findViewById([Link]);
[Link] = (ImageView) findViewById([Link]);
[Link] = (Button) findViewById([Link]);
[Link] = (Button) findViewById([Link]);
[Link] = (CheckBox) findViewById([Link]);
} catch (Exception e) {
[Link]();
}
}

public void onControlClicked(View v) {


if ([Link]() - mLastClkTime >= Threshold) {
mLastClkTime = [Link]();
try {
switch ([Link]()) {
case [Link]:
ClearLog();
return;
case [Link]:
ExtractANSITemplate();
return;
case [Link]:
ExtractISOImage();
return;
case [Link]:
ExtractWSQImage();
return;
case [Link]:
InitScanner();
return;
case [Link]:
[Link] = [Link];
if (![Link]) {
StartSyncCapture();
return;
}
return;
case [Link]:
StopCapture();
return;
case [Link]:
[Link] = [Link];
if (![Link]) {
StartSyncCapture();
return;
}
return;
case [Link]:
UnInitScanner();
return;
default:
return;
}
} catch (Exception e) {
[Link]();
}
}
}

public void onConfigurationChanged(Configuration newConfig) {


[Link](newConfig);
setContentView([Link].activity_mfs100_sample);
FindFormControls();
try {
if (this.mfs100 == null) {
this.mfs100 = new MFS100(this);
[Link](this);
}
if ([Link] && this.mfs100 != null) {
[Link]();
}
} catch (Exception e) {
[Link]();
}
}

private void InitScanner() {


try {
int ret = [Link]();
if (ret != 0) {
SetTextOnUIThread([Link](ret));
return;
}
SetTextOnUIThread("Init success");
SetLogOnUIThread("Serial: " + [Link]().SerialNo() +
" Make: " + [Link]().Make() + " Model: " +
[Link]().Model() + "\nCertificate: " +
[Link]());
} catch (Exception e) {
[Link](getApplicationContext(), "Init failed, unhandled
exception", 1).show();
SetTextOnUIThread("Init failed, unhandled exception");
}
}

private void StartSyncCapture() {


new Thread(new Runnable() {
/* class MFS100Test.AnonymousClass1 */

public void run() {


[Link]("");
[Link] = true;
try {
FingerData fingerData = new FingerData();
int ret = [Link](fingerData,
[Link], [Link]());
Log.e("[Link]", "" + ret);
if (ret != 0) {

[Link]([Link](ret));
} else {
[Link] = fingerData;
final Bitmap bitmap =
[Link]([Link](), 0,
[Link]().length);
[Link](new Runnable() {
/* class MFS100Test.AnonymousClass1.AnonymousClass1 */

public void run() {

[Link](bitmap);
}
});
[Link]("Capture Success");
[Link]("\nQuality: " +
[Link]() + "\nNFIQ: " + [Link]() + "\nWSQ Compress Ratio: " +
[Link]() + "\nImage Dimensions (inch): " +
[Link]() + "\" X " + [Link]() + "\"\nImage Area (inch): "
+ [Link]() + "\"\nResolution (dpi/ppi): " + [Link]() + "\
nGray Scale: " + [Link]() + "\nBits Per Pixal: " + [Link]() +
"\nWSQ Info: " + [Link]());
[Link].SetData2(fingerData);
}
} catch (Exception e) {
[Link]("Error");
} catch (Throwable th) {
[Link] = false;
throw th;
}
[Link] = false;
}
}).start();
}

private void StopCapture() {


try {
[Link]();
} catch (Exception e) {
SetTextOnUIThread("Error");
}
}

private void ExtractANSITemplate() {


try {
if ([Link] == null) {
SetTextOnUIThread("Finger not capture");
return;
}
byte[] tempData = new byte[2000];
int dataLen =
[Link]([Link](), tempData);
if (dataLen > 0) {
byte[] ansiTemplate = new byte[dataLen];
[Link](tempData, 0, ansiTemplate, 0, dataLen);
WriteFile("[Link]", ansiTemplate);
SetTextOnUIThread("Extract ANSI Template Success");
} else if (dataLen == 0) {
SetTextOnUIThread("Failed to extract ANSI Template");
} else {
SetTextOnUIThread([Link](dataLen));
}
} catch (Exception e) {
Log.e("Error", "Extract ANSI Template Error", e);
}
}

private void ExtractISOImage() {


try {
if ([Link] == null) {
SetTextOnUIThread("Finger not capture");
return;
}
byte[] tempData = new byte[(([Link]().Width() *
[Link]().Height()) + 1078)];
int dataLen =
[Link]([Link](), tempData, 2);
if (dataLen > 0) {
byte[] isoImage = new byte[dataLen];
[Link](tempData, 0, isoImage, 0, dataLen);
WriteFile("[Link]", isoImage);
SetTextOnUIThread("Extract ISO Image Success");
} else if (dataLen == 0) {
SetTextOnUIThread("Failed to extract ISO Image");
} else {
SetTextOnUIThread([Link](dataLen));
}
} catch (Exception e) {
Log.e("Error", "Extract ISO Image Error", e);
}
}

private void ExtractWSQImage() {


try {
if ([Link] == null) {
SetTextOnUIThread("Finger not capture");
return;
}
byte[] tempData = new byte[(([Link]().Width() *
[Link]().Height()) + 1078)];
int dataLen =
[Link]([Link](), tempData);
if (dataLen > 0) {
byte[] wsqImage = new byte[dataLen];
[Link](tempData, 0, wsqImage, 0, dataLen);
WriteFile("[Link]", wsqImage);
SetTextOnUIThread("Extract WSQ Image Success");
} else if (dataLen == 0) {
SetTextOnUIThread("Failed to extract WSQ Image");
} else {
SetTextOnUIThread([Link](dataLen));
}
} catch (Exception e) {
Log.e("Error", "Extract WSQ Image Error", e);
}
}

private void UnInitScanner() {


try {
int ret = [Link]();
if (ret != 0) {
SetTextOnUIThread([Link](ret));
return;
}
SetLogOnUIThread("Uninit Success");
SetTextOnUIThread("Uninit Success");
[Link] = null;
} catch (Exception e) {
Log.e("[Link]", [Link]());
}
}

private void WriteFile(String filename, byte[] bytes) {


try {
String path = [Link]() +
"//FingerData";
File file = new File(path);
if (![Link]()) {
[Link]();
}
String path2 = path + "//" + filename;
File file2 = new File(path2);
if (![Link]()) {
[Link]();
}
FileOutputStream stream = new FileOutputStream(path2);
[Link](bytes);
[Link]();
} catch (Exception e1) {
[Link]();
}
}

private void WriteFileString(String filename, String data) {


try {
String path = [Link]() +
"//FingerData";
File file = new File(path);
if (![Link]()) {
[Link]();
}
String path2 = path + "//" + filename;
File file2 = new File(path2);
if (![Link]()) {
[Link]();
}
FileOutputStream stream = new FileOutputStream(path2);
OutputStreamWriter writer = new OutputStreamWriter(stream);
[Link](data);
[Link]();
[Link]();
[Link]();
} catch (Exception e1) {
[Link]();
}
}

private void ClearLog() {


[Link](new Runnable() {
/* class MFS100Test.AnonymousClass2 */

public void run() {


try {
[Link]("",
[Link]);
} catch (Exception e) {
[Link]();
}
}
});
}

/* access modifiers changed from: private */


/* access modifiers changed from: public */
private void SetTextOnUIThread(final String str) {
[Link](new Runnable() {
/* class MFS100Test.AnonymousClass3 */

public void run() {


try {
[Link](str);
} catch (Exception e) {
[Link]();
}
}
});
}

/* access modifiers changed from: private */


/* access modifiers changed from: public */
private void SetLogOnUIThread(final String str) {
[Link](new Runnable() {
/* class MFS100Test.AnonymousClass4 */

public void run() {


try {
EditText editText = [Link];
[Link]("\n" + str);
} catch (Exception e) {
[Link]();
}
}
});
}

public void SetData2(FingerData fingerData) {


try {
if ([Link]([Link])) {
this.Enroll_Template = new byte[[Link]().length];
[Link]([Link](), 0, this.Enroll_Template,
0, [Link]().length);
} else if ([Link]([Link])) {
if (this.Enroll_Template != null) {
this.Verify_Template = new
byte[[Link]().length];
[Link]([Link](), 0,
this.Verify_Template, 0, [Link]().length);
int ret = [Link](this.Enroll_Template,
this.Verify_Template);
if (ret < 0) {
SetTextOnUIThread("Error: " + ret + "(" +
[Link](ret) + ")");
} else if (ret >= 96) {
SetTextOnUIThread("Finger matched with score: " + ret);
} else {
SetTextOnUIThread("Finger not matched, score: " + ret);
}
} else {
return;
}
}
} catch (Exception e) {
[Link]();
}
try {
WriteFile("[Link]", [Link]());
WriteFile("[Link]", [Link]());
WriteFile("[Link]", [Link]());
} catch (Exception e2) {
[Link]();
}
}

@Override // [Link].mfs100.MFS100Event
public void OnDeviceAttached(int vid, int pid, boolean hasPermission) {
if ([Link]() - [Link] >= Threshold) {
[Link] = [Link]();
if (!hasPermission) {
SetTextOnUIThread("Permission denied");
} else if (vid != 1204 && vid != 11279) {
} else {
if (pid == 34323) {
try {
int ret = [Link]();
if (ret != 0) {
SetTextOnUIThread([Link](ret));
} else {
SetTextOnUIThread("Load firmware success");
}
} catch (Exception e) {
[Link]();
}
} else if (pid == 4101) {
int ret2 = [Link]();
if (ret2 == 0) {
showSuccessLog("Without Key");
} else {
SetTextOnUIThread([Link](ret2));
}
}
}
}
}

private void showSuccessLog(String key) {


try {
SetTextOnUIThread("Init success");
SetLogOnUIThread("\nKey: " + key + "\nSerial: " +
[Link]().SerialNo() + " Make: " +
[Link]().Make() + " Model: " +
[Link]().Model() + "\nCertificate: " +
[Link]());
} catch (Exception e) {
}
}

@Override // [Link].mfs100.MFS100Event
public void OnDeviceDetached() {
try {
if ([Link]() - [Link] >= Threshold) {
[Link] = [Link]();
UnInitScanner();
SetTextOnUIThread("Device removed");
}
} catch (Exception e) {
}
}

@Override // [Link].mfs100.MFS100Event
public void OnHostCheckFailed(String err) {
try {
SetLogOnUIThread(err);
[Link](getApplicationContext(), err, 1).show();
} catch (Exception e) {
}
}
}

Common questions

Powered by AI

WriteFile and WriteFileString are methods used for file operations. WriteFile handles writing binary data to files, which is useful for storing fingerprint templates and images by creating directories and files as needed. WriteFileString is similar but designed to handle string data, facilitating the writing of textual representations of logs or template data. The paths are created and managed in the external storage system of the device, ensuring data persistence .

If no fingerprint is captured, potential reasons could include improper scanner initialization, hardware issues, or incorrect user interactions during capture. Solutions include verifying the scanner is correctly initialized with InitScanner(), checking for hardware connectivity issues, and ensuring proper placement of fingers on the scanner. Moreover, if AutoCapture returns a non-zero code, error handling via GetErrorMsg() should be undertaken to identify specific failures in capture .

The key steps in initializing the fingerprint scanner in the MFS100CodeHubs class include creating an instance of the MFS100 class, setting the application context, and calling the Init() method on the MFS100 instance. If the initialization is successful (i.e., Init() returns 0), a message indicating success is shown on the UI through SetTextOnUIThread, along with logging further device information. Any errors during initialization are displayed using GetErrorMsg().

The application employs the method post() on UI components such as lblMessage and txtEventLog to ensure that UI updates occur on the main thread. It involves posting a Runnable to these views, thus handling UI changes safely outside UI thread operations to prevent UnauthorizedAccessException and ensuring thread safety during concurrent operations .

The enum ScannerAction in MFS100CodeHubs defines distinct scanner operations, 'Capture' and 'Verify.' It dictates the scanner's operational mode during fingerprint operations. For instance, Capture mode tries to enroll fingerprints into Enroll_Template, while Verify compares fingerprints by matching ISOTemplates and providing match scores, indicating different procedural flows for fingerprint processing based on mode set .

When a device is attached, the application checks for permission using the hasPermission flag. If permission is not granted, a 'Permission denied' message is set on the UI using SetTextOnUIThread. If permission is granted, it further checks the device's vendor and product IDs (vid and pid). If the pid matches expected values, it attempts to load firmware or initialize the device based on the pid value, logging success messages if these operations succeed .

Upon scanner initiation failure, the application utilizes GetErrorMsg() to retrieve meaningful error messages corresponding to returned error codes. These messages are displayed on the UI with SetTextOnUIThread, ensuring users are informed about the issues encountered during initialization processes . Additionally, a Toast message is shown to provide user feedback on the error .

To extract an ANSI template, the application checks if the last capture contains fingerprint data. It uses the ExtractANSITemplate() method on the mfs100 instance with the RawData() from FingerData. If the extraction is successful, the template data is written to a file using System.arraycopy and WriteFile functions. Success or failure messages are subsequently set on the UI .

The FastDetection option, accessible via the cbFastDetection CheckBox, enhances the capture operation by adjusting the AutoCapture method's parameters to possibly expedite recognition processes. When enabled, it likely influences threshold values or detection models to quickly assess and capture fingerprints, prioritizing speed over potentially extensive data validation, thereby optimizing the capture process under certain scenarios .

The application manages device connections using the MFS100Event interface's methods; OnDeviceAttached handles device attachment by verifying permissions and initiates loading firmware or initializing based on device identification. Device detachment is managed by OnDeviceDetached, which uninitializes the scanner using UnInitScanner and notifies the user via the UI. This approach ensures that each connection state transitions are smoothly handled, maintaining or releasing resources appropriately .

You might also like