2008年5月28日 星期三

CameraCaptureDialog

延續上一篇,Windows Mobile 5 SDK提供了CameraCaptureDialog物件讓programer能夠存取device上的camera,算是一大福音啦,不過"拍照"這個動作卻一定要使用者用"手"去觸發,真是個半殘的東東. 真慘,非得用low level的function去實現:
Imports System.Runtime.InteropServices

<DllImport("CoreDll")> _
Public Shared Function FindWindow(ByVal className As String, _
ByVal WindowsName As String) As IntPtr
End Function

<DllImport("CoreDll")> _
Public Shared Function SendMessage(ByVal hWnd As IntPtr, _
ByVal wMsg As Integer, ByVal wParam As Integer, _
ByVal lParam As Integer) As Integer
End Function
* 注意"角括號"
* FindWindow可以用GetForegroundWindow()來取代
* SendMessage跟PostMessage相似,差別在於同步

關於SendMessage()內的做什麼用的可以參考這裡. 不過按下快門...算是那一類的Event?又帶哪些參數? 本來這個訊息是可以透過Remote Spy來獲取的,不過我的Spy卻有個大問題,是每次與device連線後,只能截取到第一則message,所以我一直重複開開關關就為了按下的一瞬間.
最後終結果 - Message:WM_KEYDOWN(0x100), WPARAM:0x0D, LPARAM:0
當然這東西會因不同的PDA而不同啦(我使用的是ASUS P535),你也可以在KEYDOWN_Enent中,WPARAM從0一直試上去...

Implement:利用程式去控制PDA自動拍照
按下button開啟CameraCaptureDialo,再利用timer去送出拍照及關閉的動作.
Imports Microsoft.WindowsMobile.Forms '這個要先安裝WM5 SDK,並且自行加入參考

Public WM_KEYDOWN As Integer = Val("&H100")
Public WM_CLOSE As Integer = Val("&H010")

Private Sub Timer1_Tick(...) Handles Timer1.Tick
Dim hwnd As IntPtr = FindWindow(Nothing, "Asus Camera")
PostMessage(hwnd, WM_KEYDOWN, Val("&H0D"), 0)
Timer1.Enabled = False
End Sub

Private Sub Timer2_Tick(...) Handles Timer2.Tick
Dim hwnd As IntPtr = FindWindow(Nothing, "Asus Camera")
PostMessage(hwnd, WM_CLOSE, 0, 0) '這樣關閉Camera會有問題
Timer2.Enabled = False
End Sub

Private Sub Button1_Click(...) Handles Button1.Click
Timer1.Interval = 5000
Timer1.Enabled = True
Timer2.Interval = 8000
Timer2.Enabled = True

Dim CCD As New CameraCaptureDialog
CCD.Owner = Me
CCD.Title = "MyCamera"
CCD.Mode = CameraCaptureMode.Still
CCD.InitialDirectory = "\My Documents"
CCD.DefaultFileName = "picture.jpg"

Try
CCD.ShowDialog() '這個function會在整個拍照動作完成才結束
Catch ex As Exception
'MsgBox(ex.ToString)
End Try

PictureBox1.Image = New Bitmap(CCD.InitialDirectory & "\" & CCD.DefaultFileName)
End Sub

關於這個問題找到四篇討論串:
1. SendKeys - MSDN Forums
2. How to press ok button to take picture automatically with CameraCaptureDialog?
3. CameraCaptureDialog in VS2005/CF2 - xda-developers
4. How to close Camera app after opening it using CameraCaptureDialog

* 意外發現一個好用的library-OpenNETCF,其針對Windows Mobile or Windows CE所設計,算是用來補足.Net Compact Framework 2.0. 至於有哪些好東東請參閱Table of Content
這個library是要錢的,若想試用可以考慮到對岸的網站找找

Windows Mobile開發相關:
Code Samples for Windows Mobile
Microsoft .NET Compact Framework 開發常見問題解答
AppleSeeker's Tech Blog - 博客园

1 意見:

匿名 提到...

這篇文章幫助頗大. 但請教當 CameraCaptureDialog 在前景 (foreground) 時, 我的測試是背景程式的timer 不會計數...