Web page TWAIN scanning is implemented through a combination of techniques, see the Flowchart:
Preparatory work
Read:
How to Run
Run Java application in Eclipse, select Uimain
When this window is started, the Websocket server has been initialized for completion.
Load: Load local picture
Send: Push the picture to the Web client
Scan: Scans files via scanner, automatically sends images to web client
Select a scanner source in Chrome
Show TWAIN scanned file images
Twain Web Scanning Process parsing
Start by importing all the relevant libraries (DLLs & jars) into the Eclipse project:
Combining the code for two articles in preparation, create a class sourcemanager:
Package com.data;import java.awt.event.actionevent;import java.awt.event.actionlistener;import java.io.file;import java.util.arraylist; import javatwain. Dotnetscanner;import javatwain. Ijavaproxy;import javatwain. inativeproxy; import javax.swing.timer; import com.server.wshandler;import com.util.imageutil; import net.sf.jni4net.bridge; public class sourcemanager implements inativeproxy { private ijavaproxy mscanner; private String[] mSources; private ScanAction Mscanaction; public sourcemanager () { inittwain (); mscanaction = new scanaction (); } private void inittwain () { try { bridge.init (); Bridge.loadandregisterassemblyfrom (New java.io.file ("Libs\\jni\\javatwain.j4n.dll")); } catch (Exception e) { e.printstacktrace (); } mscanner = new dotnetscanner (); Mscanner.registerlistener (This); msources = Mscanner.getsources (); } public string[] Getsources () { rEturn msources; } public synchronized void acquireimage (Int index) { mscanaction.setindex ( index); mscanaction.start (); } @Override public boolean notify (String message, string value) { ArrayList<WSHandler> sessions = wshandler.getallsessions (); for (wshandler session : sessions) { session.sendimage (Imageutil.getimagebytes (New file (value))); } return true; } &nbsP; public class scanaction { private int mindex; private int mdelay = 1; private Timer mTimer; public scanaction () { mtimer = new timer (Mdelay, mtaskperformer); mtimer.setrepeats (False); } private actionlistener mtaskperformer = new actionlistener () { @Override public void actionperforMed (actionevent evt) { mscanner.acquireimage (Mindex); actionlistener taskperformer = new actionlistener () { @Override public void actionperformed (ACTIONEVENT&NBSP;EVT) { mscanner.closesource (); } }; int delay = 1; timer timer = new timer (delay, Taskperformer); timer.setrepeats (False); timer.start (); } }; public void setindex (Int index) { mIndex = index; } public&nbSp;void start () { Mtimer.start (); } }}
The Java Websocket server communicates with the JavaScript client through JSON.
To send JSON data using Java creation:
Jsonobject jsonobj = new Jsonobject (); Jsonarray Jsonarray = new Jsonarray (); string[] sources = msourcemanager.getsources (), if (sources! = null) {for (String source:sources) {Jsonarray. Add (new jsonprimitive (source)); }} jsonobj.add (Msg.msg_sources, Jsonarray); String s = jsonobj.tostring (); try {session.getremote (). sendstring (s);} catch (IOException e) {//TODO auto-generated catch block E.printstack Trace ();}
Parsing JSON data using Java parsing:
Public void onmessage (string message) { Jsonparser parser = new jsonparser (); Boolean isjson = true; jsonelement element = null; try { element = parser.parse (message); } catch (jsonparseexception e) { system.out.println (" exception: " + e); isjson = false; } if (isjson&Nbsp;&& element != null) { jsonobject obj = element.getasjsonobject (); element = obj.get (msg.msg_message); if (element != null) { switch ( Element.getasstring ()) { case Msg.MSG_SOURCE: int index = obj.get (Msg.MSG_INDEX). Getasint (); msourcemanager.acquireimaGE (Index); break; } } } system.out.println (" message: " + message); }
Use JavaScript to create a send JSON data:
var json = {};json. Message = Msg_source;json. Index = I;var msg = json.stringify (JSON); Ws.send (msg);
Parsing JSON data using JavaScript parsing:
Ws.onmessage = function (evt) {var data = Evt.data; var json = json.parse (data); var value = json[msg_sources]; Showsources (value); }
Create a class scanaction, where you create a Timerthat runs in the UI thread to handle the scan event:
public class scanaction { private int mindex; private int mdelay = 1; private Timer mTimer; public scanaction () { mtimer = new timer (Mdelay, mtaskperformer); mtimer.setrepeats (false); } private actionlistener mtaskperformer = new actionlistener () { @Override public void Actionperformed (ActionevenT&NBSP;EVT) { mscanner.acquireimage (Mindex); actionlistener taskperformer = new actionlistener () { @Override public void actionperformed (ACTIONEVENT&NBSP;EVT) { mscanner.closesource (); } }; int delay = 1; timer timer = new timer (Delay, taskperformer); timer.setrepeats ( False); Timer.start (); } }; public void Setindex (Int index) { mindex = index; } public void sTart () { mtimer.start (); } }
Once the data scan is complete, it can be sent to the Web page:
public boolean Notify (String message, String value) {arraylist<wshandler> sessions = Wshandler.getallsession S (); for (Wshandler session:sessions) {session.sendimage (imageutil.getimagebytes (new File)); } return true; }