From 526e78cd5b6c7efddf88652a80624050770cd71e Mon Sep 17 00:00:00 2001 From: Ruslan Nagimov Date: Sat, 9 Feb 2019 17:08:44 -0800 Subject: [PATCH] initial release 0.1 --- README.md | 41 +++++++++++++- wia-cmd-scanner.vb | 130 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 170 insertions(+), 1 deletion(-) create mode 100644 wia-cmd-scanner.vb diff --git a/README.md b/README.md index 859f399..67f0d52 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,41 @@ # wia-cmd-scanner -Command-line scanner utility for WIA-compatible scanners + +This (very) small utility (~30 KB executable) provides an easy to use command-line interface to WIA-compatible scanners for Windows OS. If scanner is accessible using `Windows Fax and Scan` application, it is very likely to be compatible with this tool. Compiled binaries can be downloaded from [Releases](https://github.com/nagimov/wia-cmd-scanner/releases) + +The utility is built around WIA (Microsoft Windows Image Acquisition Library). Depending from version of Windows OS, WIA library (included file `Interop.WIA.dll`) can be required to be placed next to `wia-cmd-scanner.exe` executable. No other external dependencies are required (targeted .NET framework 3.5 comes pre-installed starting from Windows 7). The utility is portable and requires no installation. Both 32-bit and 64-bit versions of Windows OS are supported. + +## Usage + +``` +Usage: wia-cmd-scanner [OPTIONS]... + +All arguments are mandatory. Arguments must be ordered as follows: + + /dpi {150,200,300,600} scan resolution, dots per inch + /color {RGB,GRAY,BW} scan color mode + /format {BMP,PNG,GIF,JPG,TIF} output image format + /output FILEPATH path to output image file + +e.g.: + wia-cmd-scanner /dpi 300 /color RGB /format PNG /output .\scan.png +``` + +## Build + +The utility is compiled using Microsoft Visual Studio 2012 Express. + +No Visual Studio project files are provided, since the code can be imported into Visual Studio project from scratch in few easy steps (shown according to Visual Studio 2012 layout): + +* `File` -> `New Project` +* `Installed` -> `Templates` -> `Visual Basic` -> `Windows` -> `Console Application` +* copy-paste code from [`wia-cmd-scanner.vb`](https://github.com/nagimov/wia-cmd-scanner/raw/master/wia-cmd-scanner.vb) to `Module1.vb` (empty file will be opened in editor) +* right-click on `ConsoleApplication` in `Solution Explorer` and choose `Add Reference...` + + choose `COM` -> `Type Libraries` + + search for `image` and select `Microsoft Windows Image Acquisition Library v2.0` +* compile via `BUILD` -> `Build Solution` + +## Scripting and automation + +Build your own automation tools around `wia-cmd-scanner.exe` binary using batch/powershell, or check out the source code. + +The project is simple and tiny (~130 lines of VB code) and very easy to modify to fit your own needs. diff --git a/wia-cmd-scanner.vb b/wia-cmd-scanner.vb new file mode 100644 index 0000000..52295e1 --- /dev/null +++ b/wia-cmd-scanner.vb @@ -0,0 +1,130 @@ +Module Module1 + + Sub printUsage() + Const version = "0.1" + Console.WriteLine("wia-cmd-scanner (version " & version & ") ") + Console.WriteLine("License GPLv3+: GNU GPL version 3 or later ") + Console.WriteLine(" ") + Console.WriteLine("Command-line scanner utility for WIA-compatible scanners ") + Console.WriteLine("Online help, docs & bug reports: ") + Console.WriteLine(" ") + Console.WriteLine("Usage: wia-cmd-scanner [OPTIONS]... ") + Console.WriteLine(" ") + Console.WriteLine("All arguments are mandatory. Arguments must be ordered as follows: ") + Console.WriteLine(" ") + Console.WriteLine(" /dpi {150,200,300,600} scan resolution, dots per inch ") + Console.WriteLine(" /color {RGB,GRAY,BW} scan color mode ") + Console.WriteLine(" /format {BMP,PNG,GIF,JPG,TIF} output image format ") + Console.WriteLine(" /output FILEPATH path to output image file ") + Console.WriteLine(" ") + Console.WriteLine("e.g.: ") + Console.WriteLine(" wia-cmd-scanner /dpi 300 /color RGB /format PNG /output .\scan.png ") + End Sub + + Sub Main() + ' parse command line arguments + Dim clArgs() As String = Environment.GetCommandLineArgs() + + If (clArgs.Length < 8) Then + printUsage() + Exit Sub + End If + + If Not (clArgs(1) = "/dpi" And clArgs(3) = "/color" And clArgs(5) = "/format" And clArgs(7) = "/output") Then + printUsage() + Exit Sub + End If + + Dim dpi As Integer = clArgs(2) + Dim color As String = clArgs(4) + Dim format As String = clArgs(6) + Dim output As String = clArgs(8) + + If Not (dpi = 150 Or dpi = 200 Or dpi = 300 Or dpi = 600) Then + printUsage() + Exit Sub + End If + + Dim colorcode As Integer + If color = "RGB" Then + colorcode = 1 + ElseIf color = "GRAY" Then + colorcode = 2 + ElseIf color = "BW" Then + colorcode = 4 + Else + printUsage() + Exit Sub + End If + + Dim fileformat As String + Const wiaFormatBMP = "{B96B3CAB-0728-11D3-9D7B-0000F81EF32E}" + Const wiaFormatPNG = "{B96B3CAF-0728-11D3-9D7B-0000F81EF32E}" + Const wiaFormatGIF = "{B96B3CB0-0728-11D3-9D7B-0000F81EF32E}" + Const wiaFormatJPG = "{B96B3CAE-0728-11D3-9D7B-0000F81EF32E}" + Const wiaFormatTIF = "{B96B3CB1-0728-11D3-9D7B-0000F81EF32E}" + + If format = "BMP" Then + fileformat = wiaFormatBMP + ElseIf format = "PNG" Then + fileformat = wiaFormatPNG + ElseIf format = "GIF" Then + fileformat = wiaFormatGIF + ElseIf format = "JPG" Then + fileformat = wiaFormatJPG + ElseIf format = "TIF" Then + fileformat = wiaFormatTIF + Else + printUsage() + Exit Sub + End If + + If System.IO.File.Exists(output) = True Then + Console.WriteLine("Destination file exists!") + Exit Sub + End If + + ' scan the image + Dim DeviceManager = CreateObject("WIA.DeviceManager") ' create device manager + If DeviceManager.DeviceInfos.Count < 1 Then + Console.WriteLine("No compatible scanners found") + Exit Sub + End If + For i = 1 To DeviceManager.DeviceInfos.Count ' check all available devices + If DeviceManager.DeviceInfos(i).Type = 1 Then ' find first device of type "scanner" (exclude webcams, etc.) + Dim TimeStart = DateAndTime.Second(Now) + (DateAndTime.Minute(Now) * 60) + (DateAndTime.Hour(Now) * 3600) + Dim Scanner As WIA.Device = DeviceManager.DeviceInfos(i).connect ' connect to scanner + If IsNothing(Scanner) Then + Console.WriteLine("Scanner " & i & " not recognized") + Else + Console.WriteLine("Scanning to file " & output & " (dpi = " & dpi & ", color mode '" & color & "', output format '" & format & "')") + Try + ' set scan parameters + With Scanner.Items(1) + .Properties("6146").Value = colorcode + .Properties("6147").Value = dpi ' horizontal dpi + .Properties("6148").Value = dpi ' vertical dpi + End With + ' scan image as BMP... + Dim Img As WIA.ImageFile = Scanner.Items(1).Transfer(wiaFormatBMP) + ' ...and convert it to desired format + Dim ImgProc As Object = CreateObject("WIA.ImageProcess") + ImgProc.Filters.Add(ImgProc.FilterInfos("Convert").FilterID) + ImgProc.Filters(1).Properties("FormatID") = fileformat + ImgProc.Filters(1).Properties("Quality") = 75 + Img = ImgProc.Apply(Img) + ' ...and save it to file + Img.SaveFile(output) + Catch ex As Exception + Console.WriteLine("Exception occured: " & ex.Message) + Exit Sub + End Try + End If + Dim TimeEnd = DateAndTime.Second(Now) + (DateAndTime.Minute(Now) * 60) + (DateAndTime.Hour(Now) * 3600) + Console.WriteLine("Scan finished in " & (TimeEnd - TimeStart) & " seconds") + Exit Sub ' if successfully found and scanned, quit + End If + Next + End Sub + +End Module