import ij.*; import ij.process.*; import ij.measure.*; import ij.gui.*; import java.awt.*; import ij.plugin.*; import SK.gnome.twain.*; public class Twain_ implements PlugIn { public void run(String arg) { ImagePlus imp = scan(); if (imp!=null) imp.show(); } public ImagePlus scan() { Image image = null; try {image = Toolkit.getDefaultToolkit().createImage(new Twain());} catch (Throwable e) {needsGnome(e);} TwainException twainException=Twain.getLastException(); if(twainException!=null) IJ.showMessage("TWAIN Error", ""+twainException); if (image==null) return null; ImagePlus imp = new ImagePlus("Untitled", image); getUnits(imp); return imp; } void needsGnome(Throwable e) { if (!IJ.isMacintosh()) IJ.error("The Twain plug-in requires the Java Twain Package from\n" + "http://www.gnome.sk/. The files 'twain.jar' and 'JavaTwain32.dll'\n" + "from this package must be placed in the ImageJ folder and twain.jar \n" + "must be added to the -cp option of the command that runs ImageJ.\n \n" + e); } void getUnits(ImagePlus imp) { String unit; int code = TwainEx.getCurrentUnits(); if (code==0) unit = "inch"; else if (code==1) unit = "cm"; else unit = ""; double xResolution = TwainEx.getXResolution(); double yResolution = TwainEx.getYResolution(); if (xResolution!=0.0 && yResolution!=0.0) { Calibration cal = imp.getCalibration(); cal.pixelWidth = 1/xResolution; cal.pixelHeight = 1/yResolution; cal.setUnit(unit); } } }