Controlling the camera flash on the Moto Q (and other Windows Mobile devices?)

There is an application out there for the Moto Q that allows you to enable / disable the camera flash and use it like a flashlight. I think it’s a great idea and could be a very useful application. Being cheap and a programmer I set out to code my own solution rather than fork over the $4.95 or make a keygen (seriously, why wrap the flash control functions in a C++ DLL but expose your key verification function in an unobfuscated IL binary ;). Ok you caught me I wrote a keygen and then moved on heh )

Thanks to the awesome support team at MOTODEV for pointing me towards the DeviceIoControl API:

The camera flash light can be controlled as a COM-port using “CAM1:” as a device.

Use the CreateFile() call to get a handle the camera driver, with the filename “CAM1:”.

Then use this file handle to make a call to DeviceIoControl() using the IOCTL (see the code below) and set the ‘Value’ value with a DWORD variable. 0=flash off and any other value=flash on.

The code shown below will change the state of the flash on a Moto Q. I’ve started work on a Win32 DLL to expose the native functions to a VB.NET or C# project. I’d like to start using C# more so I’ll probably write a frontend to control the flash.

HANDLE hCam = NULL;  

CSPROPERTY_CAMERACONTROL_S InputBuffer;
CSPROPERTY_CAMERACONTROL_S OutputBuffer;
ULONG DataLength = sizeof(CSPROPERTY_CAMERACONTROL_S);
ULONG ReturnedLength; // Number of bytes returned in output buffer  

hCam = CreateFile(TEXT("CAM1:"), GENERIC_WRITE | GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);  

InputBuffer.Property.Set = PROPSETID_VIDCAP_CAMERACONTROL;
InputBuffer.Property.Id = CSPROPERTY_CAMERACONTROL_FLASH;
InputBuffer.Property.Flags = CSPROPERTY_TYPE_SET;
InputBuffer.Flags = CSPROPERTY_CAMERACONTROL_FLAGS_MANUAL;
InputBuffer.Value = 1;  //1==flash on, 0==flash off  

if (!DeviceIoControl(hCam, IOCTL_CS_PROPERTY, &InputBuffer, DataLength, &OutputBuffer, DataLength, &ReturnedLength, NULL))
{
    DWORD errCode = GetLastError();
}

Leave a Reply

You must be logged in to post a comment.