On Scanners, Cameras and their USB modes, and lifting the lid on how they can be scripted.

Long title, and I’m afraid I’ve been on a bit of a voyage of discovery about some of the things Windows 7 (and Vista) can do with photos and first thing I wanted to cover here was something I’ve been trying to ignore: Cameras have two USB modes.

In “Mass Storage Class” (MSC) mode, the computer sees the storage card with its blocks and filesystem and so forth like any other disk. Since the computer can write to the disk all kinds of problems could break out if the camera tried to access the disk, so when  connected the camera functions need to turn themselves off. In MSC mode the camera is becomes a USB card reader and acts like any other USB disk. (That’s the point of MSC devices)

In “Picture Transfer Protocol” (PTP) mode – and its superset, the media transfer protocol (MTP) – the camera acts as a server – the computer requests a list of files, properties of files, contents of files, but it has no access to the underlying file system so the camera can continue to take pictures and write to the disk. This offers the chance to shoot and have the PC interact with the camera at the same time,  provided that the camera maker doesn’t shut all the functions down when connected in PTP mode. Sadly Pentax do; I put my wife’s Panasonic compact in PTP mode and it was the same. On my the little Canon I take on diving trips there is no “PTP mode”, but it does have Pictbridge support. PTP is the transport protocol for PictBridge and enabling pictbridge got it to work like the Panasonic and Pentax – i.e. all the controls are locked out. From what I’ve read Olympus are the same. Of course I haven’t got the information for every camera made by every manufacturer! I’ll come back to this towards the end of the post, but it changes the way your camera appears…

Click for a full size image  Click for a full size image  Click for a full size image    Click for a full size image

From left to right with my Pentax K7 in PTP mode the camera doesn’t show up as a drive, but as a portable device in Explorer. (I could have used the Canon or Panasonic here).  When you look in the devices and printers part of control panel of Windows 7 you see the camera. If you click through the K7 here gave options to browse, import or configure options. Something which seems different to the other cameras is the option is to automatically import photos when it the camera is plugged in (The K7 does not disappear when unplugged which all the other cameras did.). Not every imaging device which shows up in control panel is a WIA device. In the screen shot below you can see I’ve unplugged my K7 – the icon is greyed out – and plugged in my Web cam; which doesn’t show up in WIA.  The reverse is also true – there is a WIA driver for Windows Mobile devices, but my phone doesn’t show up in devices and printers (at least not as a phone or a camera, only as a potential networking device) but it does show up, with a phone icon, under Portable Devices in Explorer where it has access to the same photo import wizard that the cameras have.

Click for a full size image

Linked in with this there is a Windows Image Acquisition (WIA) driver for PTP enabled cameras - so you can fetch pictures from the camera in a program which understands scanners. Generally, programs that were written for WIA will talk about “Scanner or Camera” – as in the screenshot from Windows 7’s version of Paint below, although WIA allows a program to restrict its choice to scanners only or cameras only. (Windows Fax and Scan won’t accept camera input, for example).  WIA also provides a translation layer to support programs which were written to the older TWAIN interface: these usually talk about acquiring an image from a scanner. When a device appears through the translation layer its name in the TWAIN world is prefixed with “WIA”. Some scanners include both WIA and TWAIN drivers – though the TWAIN ones are redundant on Windows Vista and 7 - and in which case the scanner gets two entries in the TWAIN dialogs (one with WIA in front of the name and one without).  I’ve got a bad track record choosing scanners and the latest piece of junk I’ve bought has a WIA driver which does not work and a TWAIN driver (which does). Hunting down the 64 bit drivers was an undertaking in itself, and for reasons only known to the scanner driver writer it appears in some dialogs when it is not plugged in. [I could go off an huge rant here, at least my ancient HP scanner has a driver on Windows update, although it doesn’t support “Transparent Materials Adapter”, so I bought this one to scan film. How hard is it to produce a driver which works properly and supports full functionality of the scanner? Why are scanners, and cameras bundled with so much useless application software to provide things like “browsing pictures” less well than the OS does it when the vendor can’t get the basics right ? OK enough ranting….] So here in Paint my new film scanner appears alongside the K7. Any attempt to use that driver will fail…grrr… but my old scanner (in page scanning mode only) or the cameras or smartphone will transfer images straight into the application.  

Click for a full size image Click for a full size image

Click for a full size image

The oldest piece of software I still use is Paintshop pro 5 (dated 1999) and it uses TWAIN. In the left picture you can see that it sees the translated K7 WIA driver and the TWAIN driver for the scanner (which isn’t plugged in). Unplug the K7 and plugging in the scanner and the dialog presents the options on the right – with WIA translated and Native TWAIN drivers – only the latter works.

Click for a full size image Click for a full size image

image

It’s possible access scanners and cameras from a scripting environment. I’m not going to advocate that everyone transfers pictures via PowerShell but it can be useful for diagnostics purposes. You can pop up a PowerShell prompt and enter the following

  PS > $WIAdialog = New-Object -ComObject "WIA.CommonDialog"     PS > $Device    = $WIAdialog.ShowSelectDevice()

If I do this with no camera or scanner connected I get this error :

  Exception calling "ShowSelectDevice" with "0" argument(s): "No WIA device of the selected type is available."

But if I do it with the rotten scanner connected I get this:

  Exception calling "ShowSelectDevice" with "0" argument(s): "The WIA device is not online."

Assuming the commands is successful one can dig a bit deeper into the properties of a scanner or camera - I've cut the list down a little to save space.

PS > $device.Properties | sort name | format-table –autosize propertyID,name,value,type,isreadonly                                                                                              PropertyID Name                   Value                Type IsReadOnly ---------- ----                   -----                ---- ----------           4 Description            K-7                    16       True       1028 Device Time            System.__ComObject    104       True         15 Driver Version         6.1.7600.16385         16       True        1026 Firmware Version       1.01                   16       True           3 Manufacturer           PENTAX                 16       True          7 Name                   K-7                    16       True       2050 Pictures Taken         419                     5       True   

As well as the properties collection, the device has an Items collection, which contains the pictures currently in the camera. Here’s the view of one item.

PS > $device.items.item(1).properties | sort name | format-table propertyID,name,value,type,isreadonly –autosize PropertyID Name                             Value Type IsReadOnly ---------- ----                             ----- ---- ----------        5125 Audio Available                      0    5       True       5127 Audio Data          System.__ComObject  102       True        4110 Bits Per Channel                     8    5       True       4104 Bits Per Pixel                      24    5      False        4109 Channels Per Pixel                   3    5       True        4123 Filename extension                 JPG   16       True        4099 Full Item Name               o506400A5   16       True        4098 Item Name                     IMG40165   16       True       4116 Item Size                      6663701    5       True        4114 Number of Lines                   3104    5       True       4112 Pixels Per Line                   4672    5       True

As well as having methods to work with the item, there are two useful wizards. The first one pops up a scanning wizard – if a plug in my other scanner, and it will automatically save pictures in a folder under My Pictures – the folder is created with the current date.

$WIAdialog.ShowAcquisitionWizard($device)  

And the second will work with scanners or cameras and returns the image as an object which can be manipulated before being saved

$i=$WIAdialog.ShowAcquireImage()           $i.SaveFile("$pwd\test.$($i.fileExtension)")

The last things about the device object which I wanted to mention were the Events and Commands, properties. The Pentax and Canon both have events which a script can watch for to respond to changes in the files stored on the camera. This is would be useful on cameras which didn’t lock out all the controls while connected – because that means the files can only be changed from the computer end. Similarly on all three of my cameras the list of commands is disappointingly small. 

PS > $device.commands

CommandID                                Name            Description       
---------                                ----            ----------- 
{9B26B7B2-ACAD-11D2-A093-00C04F72DC3C}   Synchronize     Synchronize

 

But on some cameras there are more commands , including one named Take Picture, which has an ID of {AF933CAC-ACAD-11D2-A093-00C04F72DC3C}
I can’t test this myself (one blog I found seems to be looking for cameras which do support it, among other things) it seems NOT having the controls locked out is a pre-requisite for this. If it shows up on your camera (and it seems to be mostly Nikons which support it) you should be able to take a picture and acquire it with

$I = $device.ExecuteCommand(“{AF933CAC-ACAD-11D2-A093-00C04F72DC3C}”)

and save it as in the previous example.  [Anyone who wants to post a comment about cameras where this works (or not) would be most welcome]

I’ll come back to WIA and some of the related technology in a future post, but that’s quite enough for now.