This open source software project connects the world of java with TWAIN.TWAIN is an application programming interface standard used to access scanners & digital cameras ... .

This package was developed to scan an image in order to turn it into an *.sff file and then fax it.

How does it work ? What happens ?

  1. On loading: jtwain.java loads it's C++ counter part, the library jtwain.dll.
  2. jtwain.dll will load "TWAINDSM.DLL" (Twain 2.0 / 64 bit) or "TWAIN_32.DLL" (Twain < 2.0) and get a function pointer to the DSM (Data Source Manager) entry point (twain state 2).
  3. If loading was successful jtwain.java creates a new thread that calls nstart in jtwain.cpp.
  4. Due to the fact that twain is signalling events through the OS-dependent event queues, we need to set up an application window including it's wndProc callback function.
  5. After that ninitLib will open the DSM (twain state 3). Now the DSM is ready to handle device specific requests.
  6. The native thread is then entering the event loop and calling jtwain.cbhandleGetMessage whenever it receives an event.
  7. The twain entry function must be called from within the native thread. In order to do that a command can be initiated from arbitrary java threads by calling ntrigger. This will cause the native thread to call jtwain.cbexecute.
    Two commands can be triggered:
    1. Select: The DSM will pop up a select dialog and the user can select a data source.
    2. Acquire: The data source will pop up a user dialog that allows the user to set various settings and then either scan or cancel the request. Once an image has been acquired by the twain data source as a DIB (Device Independent Bitmap) a BufferedImage object will be created and the DIB data copied to this BufferedImage. The jtwain object informs the ScannerListeners of the new image.

To see how to use this package have a look at the imageviewer application, especially class ScannerTab.

You can find a simple application here:TwainExample.java.

And uk.co.mmscomputing.device.twain.jar here which bundles all the necessary class files. In this scenario the jar file and the TwainExample.java file are in the same folder.

  • Compile:
    javac -classpath .;uk.co.mmscomputing.device.twain.jar TwainExample.java
  • Run:
    java -classpath .;uk.co.mmscomputing.device.twain.jar TwainExample


[1] TWAIN : www.twain.org [last accessed : 2009-02-20]
[2] Sheng Liang (1999), The Java Native Interface, SUN, Palo Alto

sub-directories:
  1. uk.co.mmscomputing.device.twain.applet:
    A very simple java scan applet for jtwain.
  2. uk.co.mmscomputing.device.twain.memapplet:
    A very simple java scan applet for jtwain. Saves images as pdf file.
  3. uk.co.mmscomputing.device.twain.pdfapplet:
    A very simple java scan applet for jtwain. Saves images as pdf file.
  4. uk.co.mmscomputing.device.twain.tiffapplet:
    A very simple java scan applet for jtwain. Saves images as tiff file.
  5. uk.co.mmscomputing.device.twain.win32:
    jtwain.dll : The jtwain c++ library for windows (32-bit version).
  6. uk.co.mmscomputing.device.twain.win64:
    jtwain.dll : The jtwain c++ library for windows (64-bit version).
Projects :
  1. uk.co.mmscomputing.application.imageviewer:
    A simple Multi Page Image Viewer Applet. To view multipage *.sff, *.tiff and *.gif files. Twain and SANE test application.
  2. uk.co.mmscomputing.device.scanner:
    Package scanner provides abstract parent classes to the classes in the twain and sane packages.
  3. uk.co.mmscomputing.imageio.bmp:
    Classes that enable the java imageio system to read and write *.bmp files.
  4. uk.co.mmscomputing.util:
    A few useful utility classes