B4J Library [B4j] CmdTwain Wrapper

This is a B4xlib to control the free command line Twain Scanning app CmdTwain via jShell. (Windows Only)

It allows selecting a scanner and controlling scanning options, Partial page scanning is particularly useful. You can also select jpg, bmp or pdf outputs. Not all parameters are supported on all scanners. For instance, Black and White and Grayscale scanning is not supported on my Canon MG5750 All in one, but is supported on my older Epson V200. The CmdTwain help page is here.

Basic scanning is pretty simple, you just need to provide the path to the CmdTwain executable and an output file. It uses default parameters "PAPER A4 DPI 200 RGB" and they give you an A4 sized scan at 200 dots per inch in colour. You can override one or all of the default parameters,

1660131743743.png
1660131762943.png
1660131785059.png

Usage

Download and install the CmdTwain app from their main page.
Update the ST.ScanTwainPath call with your installation path if required (Default is "C:\Program Files (x86)\GssEziSoft\CmdTwain").
Update the ST.OutputFullPath with your target output file.

Comments

I haven't been able to test all of the parameters as my scanners do not have a document hopper, so I can't test anything to do with the document feeder.

I only need a simple scan so it serves my purposes well. There is now a simple Gui as a second b4xlib and a demo app - CmdTwainCV-b4xlib / CmdTwainCV-test

Demo
The attached file CmdTwain-test.zip contains a test project and requires the CmdTwain-b4xlib

Dependencies
CmdTwain app download
There are no external B4x Dependencies.

Enjoy, and please report any bugs.

Library Updates
V 0.11 - 11 Aug 2022
  • Tidied debug logging.
  • Changed Get/SetDPI to accept strings

New demo - 11 Aug 2022

1660299790824.png

Simple Gui Form and CustomView example as a separate b4xlib and demo app. CmdTwainCV-b4xlib / CmdTwainCV-test.​
Requires CmdTwain-b4xlib V 0.11+​
Demo update
V0.12 - 12 Aug 2022​
  • Tidied Debug logging
  • Renamed layout files to lowercase
  • Save Path to CmdTwain executable if changed
  • Added Custom dpi option and field
  • Added Scan EventListener(Start,Fail,Complete)
Download CmdTwainCV-b4xlib and CmdTwainCV-test.zip​
 

Attachments

  • CmdTwain-Test.zip
    2.4 KB · Views: 138
  • CmdTwain-b4xlib.b4xlib
    3.3 KB · Views: 130
  • CmdTwainCV-b4xlib.b4xlib
    7.2 KB · Views: 120
  • CmdTwainCV-test.zip
    2.2 KB · Views: 120
  • 1660299799995.png
    1660299799995.png
    15.9 KB · Views: 119
Last edited:

stevel05

Expert
Licensed User
Longtime User
Update to V 0.11
  • Tidied debug logging.
  • Changed Get/SetDPI to accept strings

New demo
Simple Gui CustomView example as a separate b4xlib and demo app. CmdTwainCV-b4xlib / CmdTwainCV-test
 

Star-Dust

Expert
Licensed User
Longtime User
I have to say great. I have always appreciated your contributions which are always unique and open new paths.

A single note in the CmdTwainCV.test generates an error, certainly a distraction and a file is missing:
cmdtwainform._initialize (java line: 82)
java.io.FileNotFoundException: CmdTwain_Layout.bjl
at anywheresoftware.b4a.objects.streams.File.OpenInput(File.java:235)
at anywheresoftware.b4j.objects.LayoutBuilder.loadLayout(LayoutBuilder.java:66)
at anywheresoftware.b4j.objects.PaneWrapper.LoadLayout(PaneWrapper.java:101)
at b4j.example.cmdtwainform._initialize(cmdtwainform.java:82)
at b4j.example.main._appstart(main.java:56)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:111)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:98)
at b4j.example.main.start(main.java:37)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$166(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$179(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$177(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$178(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$152(WinApplication.java:177)
at java.lang.Thread.run(Thread.java:748)
 

stevel05

Expert
Licensed User
Longtime User
Hmm, it was working in my test, but I did forget my golden rule. I usually make layout names in a b4xlib lowercase. it does seem to save problems like this.

Can you just try downloading the CmdTwainCV-b4xlib file again and try it.
 

Star-Dust

Expert
Licensed User
Longtime User
Hmm, it was working in my test, but I did forget my golden rule. I usually make layout names in a b4xlib lowercase. it does seem to save problems like this.

Can you just try downloading the CmdTwainCV-b4xlib file again and try it.
work fine
 

stevel05

Expert
Licensed User
Longtime User
Demo update
V0.12 - 12 Aug 2022
  • Tidied Debug logging
  • Renamed layout files to lowercase
  • Save Path to CmdTwain executable if changed
  • Added Custom dpi option and field
  • Added Scan EventListener(Start,Fail,Complete)

Download CmdTwainCV-b4xlib and CmdTwainCV-test.zip
 
Last edited:
Top