Wednesday, May 04, 2011

Controlling a digital camera with Delphi

I want to create an application that will control a digital camera using Delphi, so that I can take pictures by issuing commands from the computer and then transfer them to my hard disk, all through the USB connection. I thought this would be relatively easy, but I managed to get lost pretty quickly.
First of all, I tried to implement the Picture Transfer Protocol (PTP), which is a relatively low level interface. I tried code by Miguel Lucero (http://www.delphi3000.com/articles/article_4077.asp) and Mike Heydon (http://www.delphi3000.com/articles/article_4841.asp) and managed to get the device name of the camera from the registry. Then I tried to communicate with it through the USB interface using the CreateFile and WriteFile/ReadFile functions. The CreateFile function seemed to work fine, returning what appeared as a valid handle:

For an example see http://members.fortunecity.com/sanya_k/oly/registry.htm, and the same code in Delphi:

hcamIN := CreateFile(PWideChar(pipeIn),
GENERIC_READ,
FILE_SHARE_READ,
nil, // no SECURITY_ATTRIBUTES structure
OPEN_EXISTING, // No special create flags
0, // No special attributes
0); // No template file

The problem was with the ReadFile and WriteFile functions, which always returned a “Request is not supported” error (error 50), no matter what I tried. Then I tried the DeviceIoControl function, but this also failed. I do not know what the problem is. I am running Delphi 2009 on Windows 7. I tried running the application in administrator mode, but that did not work any better. I thought that perhaps the camera (a Nikon D70) does not support PTP or USB commands, but this is not true, because the Nikon Camera Control Pro software works perfectly and I could see the USB commands going through, with the help of a USB sniffer program (I tried SnoopyPro and USBlyzer). Finally, I gave up, after almost a week of trying. Perhaps it is Delphi’s fault. I would appreciate any suggestions. If anyone has had a similar experience, please let me know.

After abandoning the USB low-level path, I decided to use the Windows Image Acquisition library (WIA), but soon realized that it is deprecated and that Windows Portable Devices interface (WPD) has taken its place. So, I started to code some basic stuff using WPD. I have made some progress and will report on it soon (I hope).

No comments:

Post a Comment