/* * Copyright (C) 2013 SecuGen Corporation * */ package SecuGen.Demo; import java.io.*; import java.nio.ByteBuffer; import SecuGen.Demo.R; import android.app.Activity; import android.app.AlertDialog; import android.app.PendingIntent; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.IntentFilter; import android.content.BroadcastReceiver; import android.hardware.usb.UsbDevice; import android.hardware.usb.UsbManager; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.ColorMatrix; import android.graphics.ColorMatrixColorFilter; import android.graphics.Paint; import SecuGen.FDxSDKPro.*; public class JSGDActivity extends Activity implements View.OnClickListener, java.lang.Runnable, SGFingerPresentEvent { private static final String TAG = "SecuGen USB"; private Button mCapture; private Button mButtonRegister; private Button mButtonMatch; private Button mButtonLed; private Button mSDKTest; private EditText mEditLog; private android.widget.TextView mTextViewResult; private android.widget.CheckBox mCheckBoxMatched; private android.widget.ToggleButton mToggleButtonSmartCapture; private android.widget.ToggleButton mToggleButtonAutoOn; private PendingIntent mPermissionIntent; private ImageView mImageViewFingerprint; private ImageView mImageViewRegister; private ImageView mImageViewVerify; private byte[] mRegisterImage; private byte[] mVerifyImage; private byte[] mRegisterTemplate; private byte[] mVerifyTemplate; private int[] mMaxTemplateSize; private int mImageWidth; private int mImageHeight; private int[] grayBuffer; private Bitmap grayBitmap; private IntentFilter filter; //2014-04-11 private SGAutoOnEventNotifier autoOn; private boolean mLed; private boolean mAutoOnEnabled; private JSGFPLib sgfplib; private void debugMessage(String message) { this.mEditLog.append(message); this.mEditLog.invalidate(); //TODO trying to get Edit log to update after each line written } //RILEY //This broadcast receiver is necessary to get user permissions to access the attached USB device private static final String ACTION_USB_PERMISSION = "com.android.example.USB_PERMISSION"; private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() { public void onReceive(Context context, Intent intent) { String action = intent.getAction(); //DEBUG Log.d(TAG,"Enter mUsbReceiver.onReceive()"); if (ACTION_USB_PERMISSION.equals(action)) { synchronized (this) { UsbDevice device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE); if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) { if(device != null){ //DEBUG Log.d(TAG, "Vendor ID : " + device.getVendorId() + "\n"); //DEBUG Log.d(TAG, "Product ID: " + device.getProductId() + "\n"); debugMessage("Vendor ID : " + device.getVendorId() + "\n"); debugMessage("Product ID: " + device.getProductId() + "\n"); } else Log.e(TAG, "mUsbReceiver.onReceive() Device is null"); } else Log.e(TAG, "mUsbReceiver.onReceive() permission denied for device " + device); } } } }; //RILEY //This message handler is used to access local resources not //accessible by SGFingerPresentCallback() because it is called by //a separate thread. public Handler fingerDetectedHandler = new Handler(){ // @Override public void handleMessage(Message msg) { //Handle the message CaptureFingerPrint(); if (mAutoOnEnabled) { mToggleButtonAutoOn.toggle(); EnableControls(); } } }; public void EnableControls(){ this.mCapture.setClickable(true); this.mCapture.setTextColor(getResources().getColor(android.R.color.white)); this.mButtonRegister.setClickable(true); this.mButtonRegister.setTextColor(getResources().getColor(android.R.color.white)); this.mButtonMatch.setClickable(true); this.mButtonMatch.setTextColor(getResources().getColor(android.R.color.white)); } public void DisableControls(){ this.mCapture.setClickable(false); this.mCapture.setTextColor(getResources().getColor(android.R.color.black)); this.mButtonRegister.setClickable(false); this.mButtonRegister.setTextColor(getResources().getColor(android.R.color.black)); this.mButtonMatch.setClickable(false); this.mButtonMatch.setTextColor(getResources().getColor(android.R.color.black)); } //RILEY @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.launcher); mCapture = (Button)findViewById(R.id.buttonCapture); mCapture.setOnClickListener(this); mButtonRegister = (Button)findViewById(R.id.buttonRegister); mButtonRegister.setOnClickListener(this); mButtonMatch = (Button)findViewById(R.id.buttonMatch); mButtonMatch.setOnClickListener(this); mButtonLed = (Button)findViewById(R.id.buttonLedOn); mButtonLed.setOnClickListener(this); mSDKTest = (Button)findViewById(R.id.buttonSDKTest); mSDKTest.setOnClickListener(this); mEditLog = (EditText)findViewById(R.id.editLog); mTextViewResult = (android.widget.TextView)findViewById(R.id.textViewResult); mCheckBoxMatched = (android.widget.CheckBox) findViewById(R.id.checkBoxMatched); mToggleButtonSmartCapture = (android.widget.ToggleButton) findViewById(R.id.toggleButtonSmartCapture); mToggleButtonSmartCapture.setOnClickListener(this); mToggleButtonAutoOn = (android.widget.ToggleButton) findViewById(R.id.toggleButtonAutoOn); mToggleButtonAutoOn.setOnClickListener(this); mImageViewFingerprint = (ImageView)findViewById(R.id.imageViewFingerprint); mImageViewRegister = (ImageView)findViewById(R.id.imageViewRegister); mImageViewVerify = (ImageView)findViewById(R.id.imageViewVerify); grayBuffer = new int[JSGFPLib.MAX_IMAGE_WIDTH_ALL_DEVICES*JSGFPLib.MAX_IMAGE_HEIGHT_ALL_DEVICES]; for (int i=0; i> 16) & 0xFF; int g = (color >> 8) & 0xFF; int b = color & 0xFF; int gray = (r+g+b)/3; color = Color.rgb(gray, gray, gray); //color = Color.rgb(r/3, g/3, b/3); bmpGrayscale.setPixel(x, y, color); } } return bmpGrayscale; } //Converts image to binary (OLD) public Bitmap toBinary(Bitmap bmpOriginal) { int width, height; height = bmpOriginal.getHeight(); width = bmpOriginal.getWidth(); Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565); Canvas c = new Canvas(bmpGrayscale); Paint paint = new Paint(); ColorMatrix cm = new ColorMatrix(); cm.setSaturation(0); ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm); paint.setColorFilter(f); c.drawBitmap(bmpOriginal, 0, 0, paint); return bmpGrayscale; } public void DumpFile(String fileName, byte[] buffer) { //Uncomment section below to dump images and templates to SD card /* try { File myFile = new File("/sdcard/Download/" + fileName); myFile.createNewFile(); FileOutputStream fOut = new FileOutputStream(myFile); fOut.write(buffer,0,buffer.length); fOut.close(); } catch (Exception e) { debugMessage("Exception when writing file" + fileName); } */ } public void SGFingerPresentCallback (){ autoOn.stop(); fingerDetectedHandler.sendMessage(new Message()); } public void CaptureFingerPrint(){ long dwTimeStart = 0, dwTimeEnd = 0, dwTimeElapsed = 0; this.mCheckBoxMatched.setChecked(false); byte[] buffer = new byte[mImageWidth*mImageHeight]; dwTimeStart = System.currentTimeMillis(); long result = sgfplib.GetImage(buffer); DumpFile("capture.raw", buffer); dwTimeEnd = System.currentTimeMillis(); dwTimeElapsed = dwTimeEnd-dwTimeStart; debugMessage("getImage() ret:" + result + " [" + dwTimeElapsed + "ms]\n"); mTextViewResult.setText("getImage() ret: " + result + " [" + dwTimeElapsed + "ms]\n"); Bitmap b = Bitmap.createBitmap(mImageWidth,mImageHeight, Bitmap.Config.ARGB_8888); b.setHasAlpha(false); int[] intbuffer = new int[mImageWidth*mImageHeight]; for (int i=0; i