Created
July 26, 2012 16:42
-
-
Save pullmonkey/3183134 to your computer and use it in GitHub Desktop.
topaz signature pad java code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.awt.image.BufferedImage; | |
| import java.beans.Beans; | |
| import java.io.FileOutputStream; | |
| import java.io.IOException; | |
| import java.util.Date; | |
| import java.util.logging.FileHandler; | |
| import java.util.logging.Logger; | |
| import java.util.logging.SimpleFormatter; | |
| import com.sun.image.codec.jpeg.JPEGCodec; | |
| import com.sun.image.codec.jpeg.JPEGImageEncoder; | |
| import com.topaz.sigplus.SigPlus; | |
| public class SigPlusAuto implements Runnable { | |
| SigPlus sigObj = null; | |
| Thread eventThread; | |
| static Logger logger = Logger.getLogger("mylogger"); | |
| static FileHandler fh; | |
| public static void main( String Args[] ) { | |
| SigPlusAuto demo = new SigPlusAuto(); | |
| try { | |
| fh = new FileHandler("/var/log/sig_pad_image.log"); | |
| SimpleFormatter formatter = new SimpleFormatter(); | |
| fh.setFormatter(formatter); | |
| } catch (SecurityException e) { | |
| e.printStackTrace(); | |
| } catch (IOException e) { | |
| e.printStackTrace(); | |
| } | |
| logger.addHandler(fh); | |
| } | |
| public void startCollecting() { | |
| sigObj.setTabletState(0); | |
| sigObj.setTabletState(1); | |
| } | |
| public void stopCollecting() { | |
| sigObj.setTabletState(0); | |
| } | |
| public void clearTablet() { | |
| sigObj.clearTablet(); | |
| } | |
| public void captureSig() { | |
| logger.info("Capturing Signature"); | |
| eventThread = new Thread(this); | |
| eventThread.start(); | |
| } | |
| // basically look for a change in number of points within a time period | |
| // if not change in number of points for a couple seconds, then we'll move on | |
| // otherwise, we wait ... | |
| public void run() { | |
| logger.info("Running!"); | |
| int numberOfTabletPoints = 0; | |
| int currentSecondsPassedSinceLastWrite = 0; | |
| int newNumberOfTabletPoints = 0; | |
| Date d = new Date(); | |
| int lastTimeStamp = d.getSeconds(); | |
| try { | |
| while(true) { | |
| clearTablet(); | |
| startCollecting(); | |
| currentSecondsPassedSinceLastWrite = 0; | |
| logger.info("Starting while loop to wait for sig to start"); | |
| while(sigObj.numberOfTabletPoints() == 0) { | |
| Thread.sleep(200); | |
| } | |
| logger.info("Starting while loop to wait for sig to end (for a couple seconds to pass)"); | |
| while (currentSecondsPassedSinceLastWrite <= 1) { | |
| newNumberOfTabletPoints = sigObj.numberOfTabletPoints(); | |
| // if we have not seen any activity then record the num of seconds since last change | |
| if (newNumberOfTabletPoints == numberOfTabletPoints) { | |
| d = new Date(); | |
| currentSecondsPassedSinceLastWrite = d.getSeconds() - lastTimeStamp; | |
| } else { | |
| // resetting count, we have new points | |
| currentSecondsPassedSinceLastWrite = 0; | |
| d = new Date(); | |
| lastTimeStamp = d.getSeconds(); | |
| } | |
| if (currentSecondsPassedSinceLastWrite < 0) { | |
| currentSecondsPassedSinceLastWrite = 0; | |
| } | |
| numberOfTabletPoints = newNumberOfTabletPoints; | |
| Thread.sleep(200); | |
| } | |
| // 3 seconds has passed | |
| stopCollecting(); | |
| saveImage(); | |
| sendImage(); | |
| } | |
| } | |
| catch (InterruptedException e) { | |
| return; | |
| } | |
| } | |
| public SigPlusAuto() { | |
| try { | |
| ClassLoader cl = (com.topaz.sigplus.SigPlus.class).getClassLoader(); | |
| sigObj = (SigPlus)Beans.instantiate( cl, "com.topaz.sigplus.SigPlus" ); | |
| sigObj.setSize(100,100); | |
| sigObj.setTabletComPort("HID1"); //properly set up HSB tablet | |
| sigObj.setTabletModel("SignatureGem1X5"); | |
| captureSig(); // threaded loop | |
| } | |
| catch ( Exception e ) { | |
| return; | |
| } | |
| } | |
| public void sendImage() { | |
| logger.info("Calling ruby code to send the image"); | |
| try { | |
| String cmd = "ruby /etc/opt/script/send_sig_pad_image_to_server.rb"; | |
| logger.info("Calling command: " + cmd); | |
| executeCommand(cmd); | |
| } catch (Exception e) { | |
| e.printStackTrace(); | |
| return; // try again | |
| } | |
| } | |
| static void executeCommand(String cmd) throws Exception{ | |
| try { | |
| Process p = Runtime.getRuntime().exec(cmd); | |
| if(p.waitFor() != 0) { | |
| logger.info("The command " + cmd + " returned with an error)"); | |
| } | |
| } catch(Exception e) { | |
| return; // try again | |
| } | |
| } | |
| public void saveImage() { | |
| logger.info("Save Image and Exit"); | |
| try { | |
| sigObj.setTabletState(0); | |
| sigObj.setImageJustifyMode(5); | |
| sigObj.setImagePenWidth(10); | |
| sigObj.setImageXSize(1000); | |
| sigObj.setImageYSize(350); | |
| BufferedImage sigImage = sigObj.sigImage(); | |
| int w = sigImage.getWidth(null); | |
| int h = sigImage.getHeight(null); | |
| int[] pixels = new int[(w * h) * 2]; | |
| sigImage.setRGB(0, 0, 0, 0, pixels, 0, 0); | |
| FileOutputStream fos = new | |
| FileOutputStream("/tmp/sig_pad_sig_image.jpg"); | |
| JPEGImageEncoder jpeg = | |
| JPEGCodec.createJPEGEncoder(fos); | |
| jpeg.encode(sigImage); | |
| fos.close(); | |
| } | |
| catch (Throwable th) { | |
| th.printStackTrace(); | |
| return; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi,
Can you please provide Complete workspace example of this Signature Pad Implementation,
We are also using Topaz Pads