I wanted to be able to take pictures using the internal camera of an Asus P750 right from my application.
I've first tried the "native" approach and downloaded the code of Wimo (worth a look).
However the pictures taken were distorted and the whole process was unstable (crashing from time to time).
My second attempt was to call the CameraCaptureDialog which has a "managed" api. Dzt has already created a wrapper for it: http://www.b4x.com/forum/additional-libraries/1155-ccd-camera-library.html#post6033
I wanted to show the camera dialog and then send a keypress message that will take the picture and then somehow close the dialog.
However this didn't work as well, throwing InvalidOperationException every time.
After searching a little bit it is clear that it is a common problem and this api is not supported by all devices.
The third attempt seems to work.
Instead of calling the CameraCaptureDialog we are launching camera.exe.
The drawback is that we can't change the default settings like resolution and file name.
Using a timer we are waiting 5 seconds before sending the "Ok" key message which takes a picture and then after another 5 seconds we close the process with Process.CloseMainWindow.
This program requires agraham's threading library (only for the Process object).
The source code is attached.
I've first tried the "native" approach and downloaded the code of Wimo (worth a look).
However the pictures taken were distorted and the whole process was unstable (crashing from time to time).
My second attempt was to call the CameraCaptureDialog which has a "managed" api. Dzt has already created a wrapper for it: http://www.b4x.com/forum/additional-libraries/1155-ccd-camera-library.html#post6033
I wanted to show the camera dialog and then send a keypress message that will take the picture and then somehow close the dialog.
However this didn't work as well, throwing InvalidOperationException every time.
After searching a little bit it is clear that it is a common problem and this api is not supported by all devices.
The third attempt seems to work.
Instead of calling the CameraCaptureDialog we are launching camera.exe.
The drawback is that we can't change the default settings like resolution and file name.
Using a timer we are waiting 5 seconds before sending the "Ok" key message which takes a picture and then after another 5 seconds we close the process with Process.CloseMainWindow.
This program requires agraham's threading library (only for the Process object).
The source code is attached.
B4X:
Sub Globals
tick = 0
End Sub
Sub App_Start
Form1.Show
process.New1
hardware.New1
timer1.Interval = 5000
End Sub
Sub btnTakePicture_Click
timer1.Enabled = True
process.Start("\windows\camera.exe","")
End Sub
Sub AfterPicture
file = FindLastFile("\my documents\my pictures\","pic")
label1.Text = FileName(file)
image1.Image = file
End Sub
Sub FindLastFile (path, prefix)
ArrayList1.Clear
FileSearch(ArrayList1,path,prefix & "*.jpg")
'We assume that the last taken image name will be the last name in the sorted list.
'This assumption might be wrong.
ArrayList1.Sort(cCaseSensitive)
Return ArrayList1.Item(ArrayList1.Count-1)
End Sub
Sub Timer1_Tick
tick = tick + 1
If tick = 1 Then 'take picture
hardware.KeyPress(13)
Else
timer1.Enabled = False
process.CloseMainWindow
tick = 0
AfterPicture
End If
End Sub