Windows – Automate Windows scanning feature

automationbatch filescannerwindows 7

I have a rather older Canon scanner Pixma MP 110. Of course, Canon does not provide any useful drivers (their site is really desperate, frustrating and useless in all means) – the only software I found is a Chinese version with question marks everywhere.

However, if I go to Devices and printers manager under Windows and right-click the scanner, I may use Windows-integrated scanning manager that is in my native language. It has somewhat limited features but is still better than a bunch of buttons full of question marks.

I searched a little and found out that the feature I talk about is called WIA.

Here is the approach, however in Czech, which may sound to you like these question marks sound to me.
enter image description here

After choosing Start scanning a dialog appears. It runs under explorer.exe, so there is no change finding out what program it actually is.
enter image description here

What I want is to make a desktop shortcut, that would automatically start scanning, without me clicking the "Start scanning" and "Scan".

Also, the scanner has a button to start scanning and my computer recognizes when it's pressed. When I press it, Windows asks me what application should run for the button — however, no scanner software worked here, nor did any appear in the selection Windows gives me. I was wondering if I could hack this to run any application for that button. (that would be useful if I ever got answer for the primary question).

enter image description here

So the questions once more, if you were too lazy to read that long post:

  1. How do I make Windows automatically start scanning from installed scanner using batch script or a simple command in .lnk file?
  2. (optional) How do I assign any application to the button press of my scanner?

Best Answer

PowerShell solution

The script should work with most scanners regardless if it's a Canon, Epson or whatever as long as they are WIA compatible and support the transfer() command. The script will start scanning immediately. All options like filename, path or image format are already set via script. You just have to start the scanning process with a shortcut

  1. Save it as e.g D:\StartScan.ps1
  2. Create a new shortcut and point it to

    %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -File "D:\StartScan.ps1"
    

StartScan.ps1

# Create object to access the scanner
$deviceManager = new-object -ComObject WIA.DeviceManager
$device = $deviceManager.DeviceInfos.Item(1).Connect()

# Create object to access the scanned image later
$imageProcess = new-object -ComObject WIA.ImageProcess

# Store file format GUID strings
$wiaFormatBMP  = "{B96B3CAB-0728-11D3-9D7B-0000F81EF32E}"
$wiaFormatPNG  = "{B96B3CAF-0728-11D3-9D7B-0000F81EF32E}"
$wiaFormatGIF  = "{B96B3CB0-0728-11D3-9D7B-0000F81EF32E}"
$wiaFormatJPEG = "{B96B3CAE-0728-11D3-9D7B-0000F81EF32E}"
$wiaFormatTIFF = "{B96B3CB1-0728-11D3-9D7B-0000F81EF32E}"

# Scan the image from scanner as BMP
foreach ($item in $device.Items) {
    $image = $item.Transfer() 
}

# set type to JPEG and quality/compression level
$imageProcess.Filters.Add($imageProcess.FilterInfos.Item("Convert").FilterID)
$imageProcess.Filters.Item(1).Properties.Item("FormatID").Value = $wiaFormatJPEG
$imageProcess.Filters.Item(1).Properties.Item("Quality").Value = 5
$image = $imageProcess.Apply($image)

# Build filepath from desktop path and filename 'Scan 0'
$filename = "$([Environment]::GetFolderPath("Desktop"))\Scan {0}.jpg"

# If a file named 'Scan 0' already exists, increment the index as long as needed
$index = 0
while (test-path ($filename -f $index)) {[void](++$index)}
$filename = $filename -f $index

# Save image to 'C:\Users\<username>\Desktop\Scan {x}'
$image.SaveFile($filename)

# Show image 
& $filename

Customizations

  • Change Item("FormatID").Value = $wiaFormatJPEG to $wiaFormatPNG (or TIFF, BMP, GIF) if you need another image format
  • Change $([Environment]::GetFolderPath("Desktop"))\Scan {0}.jpg" if you need another output path. Change the extension .jpg if you previously had changed the image format

Used resources

Related Question