Vorschau PAL-Kamera

  • VB.NET

Es gibt 3 Antworten in diesem Thema. Der letzte Beitrag () ist von markusneu.

    Vorschau PAL-Kamera

    Hallo,

    ich muss für eine Facharbeit ein Programm schreiben, dass mir für eine bestimmte zeit in bestimmten Abständen Fotos macht.
    Ich habe mir (da ich eig nicht programmieren kann) aus dem Internet codeschnipsel zum anzeigen der Kamera in einem Preview-fenster (was es auch geben soll!) und zum abspeichern dieses fensters als bmp gesucht..
    Und das hat uach funktioniert, zumindestens mit der Test-Webcam.
    Als ich dir richtige Kamera geschickt bekommen hab, stellte sich heraus, dass das Bild stark flackert bzw zu 2/3 aus aussetzern besteht!
    Als ich in VLC diese Kamera geöffnet habe, ziegte sie alles ganz normal an, nachdem ich diese aber in den Einstellungen statt auf PAL auf NTSC gesetzt hatte, tauchten exakt (!!) die selben symtome auf...

    Es muss also daran liegen, dass mein code nur mit NTSC klarkommt, nicht jedoch mit PAL!!

    Hier ist der Code, vlt kann mir jemand beim lösen meines problems helfen?

    VB.NET-Quellcode

    1. Imports System.Runtime.InteropServices
    2. Public Class Form1
    3. Inherits System.Windows.Forms.Form
    4. Dim WM_CAP_DLG_VIDEOSOURCE As Integer
    5. Declare Sub Sleep Lib "kernel32" Alias "Sleep" ()
    6. Dim timeleft As Double
    7. Dim newtime As Double
    8. Friend WithEvents Text1 As System.Windows.Forms.TextBox
    9. Dim start As Boolean
    10. Dim i As Integer
    11. #Region " Windows Form Designer generated code "
    12. Public Sub New()
    13. MyBase.New()
    14. 'This call is required by the Windows Form Designer.
    15. InitializeComponent()
    16. 'Add any initialization after the InitializeComponent() call
    17. End Sub
    18. 'Form overrides dispose to clean up the component list.
    19. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
    20. If disposing Then
    21. If Not (components Is Nothing) Then
    22. components.Dispose()
    23. End If
    24. End If
    25. MyBase.Dispose(disposing)
    26. End Sub
    27. 'Required by the Windows Form Designer
    28. Private components As System.ComponentModel.IContainer
    29. 'NOTE: The following procedure is required by the Windows Form Designer
    30. 'It can be modified using the Windows Form Designer.
    31. 'Do not modify it using the code editor.
    32. Friend WithEvents picCapture As System.Windows.Forms.PictureBox
    33. Friend WithEvents lstDevices As System.Windows.Forms.ListBox
    34. Friend WithEvents lblDevice As System.Windows.Forms.Label
    35. Friend WithEvents btnStart As System.Windows.Forms.Button
    36. Friend WithEvents btnSave As System.Windows.Forms.Button
    37. Friend WithEvents btnStop As System.Windows.Forms.Button
    38. Friend WithEvents Timer1 As System.Windows.Forms.Timer
    39. Friend WithEvents sfdImage As System.Windows.Forms.SaveFileDialog
    40. <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    41. Me.components = New System.ComponentModel.Container()
    42. Me.picCapture = New System.Windows.Forms.PictureBox()
    43. Me.lstDevices = New System.Windows.Forms.ListBox()
    44. Me.lblDevice = New System.Windows.Forms.Label()
    45. Me.btnStart = New System.Windows.Forms.Button()
    46. Me.btnSave = New System.Windows.Forms.Button()
    47. Me.btnStop = New System.Windows.Forms.Button()
    48. Me.sfdImage = New System.Windows.Forms.SaveFileDialog()
    49. Me.Timer1 = New System.Windows.Forms.Timer(Me.components)
    50. Me.Text1 = New System.Windows.Forms.TextBox()
    51. Me.Label1 = New System.Windows.Forms.Label()
    52. Me.Label2 = New System.Windows.Forms.Label()
    53. Me.Button1 = New System.Windows.Forms.Button()
    54. CType(Me.picCapture, System.ComponentModel.ISupportInitialize).BeginInit()
    55. Me.SuspendLayout()
    56. '
    57. 'picCapture
    58. '
    59. Me.picCapture.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
    60. Me.picCapture.Location = New System.Drawing.Point(437, 116)
    61. Me.picCapture.Name = "picCapture"
    62. Me.picCapture.Size = New System.Drawing.Size(114, 85)
    63. Me.picCapture.TabIndex = 0
    64. Me.picCapture.TabStop = False
    65. '
    66. 'lstDevices
    67. '
    68. Me.lstDevices.Location = New System.Drawing.Point(8, 55)
    69. Me.lstDevices.Name = "lstDevices"
    70. Me.lstDevices.Size = New System.Drawing.Size(184, 238)
    71. Me.lstDevices.TabIndex = 1
    72. '
    73. 'lblDevice
    74. '
    75. Me.lblDevice.Location = New System.Drawing.Point(8, 32)
    76. Me.lblDevice.Name = "lblDevice"
    77. Me.lblDevice.Size = New System.Drawing.Size(184, 16)
    78. Me.lblDevice.TabIndex = 2
    79. Me.lblDevice.Text = "Video-Quellen"
    80. Me.lblDevice.TextAlign = System.Drawing.ContentAlignment.TopCenter
    81. '
    82. 'btnStart
    83. '
    84. Me.btnStart.Location = New System.Drawing.Point(20, 320)
    85. Me.btnStart.Name = "btnStart"
    86. Me.btnStart.Size = New System.Drawing.Size(112, 32)
    87. Me.btnStart.TabIndex = 3
    88. Me.btnStart.Text = "Vorschau Starten"
    89. '
    90. 'btnSave
    91. '
    92. Me.btnSave.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
    93. Me.btnSave.Location = New System.Drawing.Point(360, 320)
    94. Me.btnSave.Name = "btnSave"
    95. Me.btnSave.Size = New System.Drawing.Size(112, 32)
    96. Me.btnSave.TabIndex = 4
    97. Me.btnSave.Text = "Aufnahme Starten"
    98. '
    99. 'btnStop
    100. '
    101. Me.btnStop.Location = New System.Drawing.Point(184, 320)
    102. Me.btnStop.Name = "btnStop"
    103. Me.btnStop.Size = New System.Drawing.Size(112, 32)
    104. Me.btnStop.TabIndex = 5
    105. Me.btnStop.Text = "Vorschau Stoppen"
    106. '
    107. 'sfdImage
    108. '
    109. Me.sfdImage.FileName = "Webcam1"
    110. Me.sfdImage.Filter = "Bitmap|*.bmp"
    111. '
    112. 'Timer1
    113. '
    114. Me.Timer1.Interval = 1000
    115. '
    116. 'Text1
    117. '
    118. Me.Text1.Location = New System.Drawing.Point(637, 327)
    119. Me.Text1.Name = "Text1"
    120. Me.Text1.Size = New System.Drawing.Size(100, 20)
    121. Me.Text1.TabIndex = 6
    122. '
    123. 'Label1
    124. '
    125. Me.Label1.AutoSize = True
    126. Me.Label1.Location = New System.Drawing.Point(514, 333)
    127. Me.Label1.Name = "Label1"
    128. Me.Label1.Size = New System.Drawing.Size(106, 13)
    129. Me.Label1.TabIndex = 7
    130. Me.Label1.Text = "aufgenommen Bilder:"
    131. '
    132. 'Label2
    133. '
    134. Me.Label2.AutoSize = True
    135. Me.Label2.Location = New System.Drawing.Point(470, 100)
    136. Me.Label2.Name = "Label2"
    137. Me.Label2.Size = New System.Drawing.Size(55, 13)
    138. Me.Label2.TabIndex = 8
    139. Me.Label2.Text = "Vorschau:"
    140. '
    141. 'Button1
    142. '
    143. Me.Button1.Location = New System.Drawing.Point(686, 125)
    144. Me.Button1.Name = "Button1"
    145. Me.Button1.Size = New System.Drawing.Size(75, 23)
    146. Me.Button1.TabIndex = 9
    147. Me.Button1.Text = "Button1"
    148. Me.Button1.UseVisualStyleBackColor = True
    149. '
    150. 'Form1
    151. '
    152. Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
    153. Me.ClientSize = New System.Drawing.Size(819, 382)
    154. Me.Controls.Add(Me.Button1)
    155. Me.Controls.Add(Me.Label2)
    156. Me.Controls.Add(Me.Label1)
    157. Me.Controls.Add(Me.Text1)
    158. Me.Controls.Add(Me.btnStop)
    159. Me.Controls.Add(Me.btnSave)
    160. Me.Controls.Add(Me.btnStart)
    161. Me.Controls.Add(Me.lblDevice)
    162. Me.Controls.Add(Me.lstDevices)
    163. Me.Controls.Add(Me.picCapture)
    164. Me.Name = "Form1"
    165. Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
    166. Me.Text = "Video Capture"
    167. CType(Me.picCapture, System.ComponentModel.ISupportInitialize).EndInit()
    168. Me.ResumeLayout(False)
    169. Me.PerformLayout()
    170. End Sub
    171. #End Region
    172. Const WM_CAP As Short = &H400S
    173. Const WM_CAP_DRIVER_CONNECT As Integer = WM_CAP + 10
    174. Const WM_CAP_DRIVER_DISCONNECT As Integer = WM_CAP + 11
    175. Const WM_CAP_EDIT_COPY As Integer = WM_CAP + 30
    176. Const WM_CAP_SET_PREVIEW As Integer = WM_CAP + 50
    177. Const WM_CAP_SET_PREVIEWRATE As Integer = 25
    178. Const WM_CAP_SET_SCALE As Integer = WM_CAP + 53
    179. Const WS_CHILD As Integer = &H40000000
    180. Const WS_VISIBLE As Integer = &H10000000
    181. Const SWP_NOMOVE As Short = &H2S
    182. Const SWP_NOSIZE As Short = 1
    183. Const SWP_NOZORDER As Short = &H4S
    184. Const HWND_BOTTOM As Short = 1
    185. Dim iDevice As Integer = 1 ' Current device ID
    186. Dim hHwnd As Integer ' Handle to preview window
    187. Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
    188. (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, _
    189. <MarshalAs(UnmanagedType.AsAny)> ByVal lParam As Object) As Integer
    190. Declare Function SetWindowPos Lib "user32" Alias "SetWindowPos" (ByVal hwnd As Integer, _
    191. ByVal hWndInsertAfter As Integer, ByVal x As Integer, ByVal y As Integer, _
    192. ByVal cx As Integer, ByVal cy As Integer, ByVal wFlags As Integer) As Integer
    193. Declare Function DestroyWindow Lib "user32" (ByVal hndw As Integer) As Boolean
    194. Declare Function capCreateCaptureWindowA Lib "avicap32.dll" _
    195. (ByVal lpszWindowName As String, ByVal dwStyle As Integer, _
    196. ByVal x As Integer, ByVal y As Integer, ByVal nWidth As Integer, _
    197. ByVal nHeight As Short, ByVal hWndParent As Integer, _
    198. ByVal nID As Integer) As Integer
    199. Declare Function capGetDriverDescriptionA Lib "avicap32.dll" (ByVal wDriver As Short, _
    200. ByVal lpszName As String, ByVal cbName As Integer, ByVal lpszVer As String, _
    201. ByVal cbVer As Integer) As Boolean
    202. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    203. i = 1
    204. LoadDeviceList()
    205. If lstDevices.Items.Count > 0 Then
    206. btnStart.Enabled = True
    207. lstDevices.SelectedIndex = 0
    208. btnStart.Enabled = True
    209. Else
    210. lstDevices.Items.Add("No Capture Device")
    211. btnStart.Enabled = False
    212. End If
    213. btnStop.Enabled = False
    214. btnSave.Enabled = False
    215. picCapture.SizeMode = PictureBoxSizeMode.StretchImage
    216. End Sub
    217. Private Sub LoadDeviceList()
    218. Dim strName As String = Space(100)
    219. Dim strVer As String = Space(100)
    220. Dim bReturn As Boolean
    221. Dim x As Integer = 0
    222. '
    223. ' Load name of all avialable devices into the lstDevices
    224. '
    225. Do
    226. '
    227. ' Get Driver name and version
    228. '
    229. bReturn = capGetDriverDescriptionA(x, strName, 100, strVer, 100)
    230. '
    231. ' If there was a device add device name to the list
    232. '
    233. If bReturn Then lstDevices.Items.Add(strName.Trim)
    234. x += 1
    235. Loop Until bReturn = False
    236. End Sub
    237. Private Sub OpenPreviewWindow()
    238. Dim iHeight As Integer = 352
    239. Dim iWidth As Integer = 570
    240. '
    241. ' Open Preview window in picturebox
    242. '
    243. hHwnd = capCreateCaptureWindowA(iDevice, WS_VISIBLE Or WS_CHILD, 0, 0, 700, _
    244. 700, picCapture.Handle.ToInt32, 0)
    245. '
    246. ' Connect to device
    247. '
    248. If SendMessage(hHwnd, WM_CAP_DRIVER_CONNECT, iDevice, 0) Then
    249. '
    250. 'Set the preview scale
    251. '
    252. SendMessage(hHwnd, WM_CAP_SET_SCALE, True, 0)
    253. '
    254. 'Set the preview rate in milliseconds
    255. '
    256. SendMessage(hHwnd, WM_CAP_SET_PREVIEWRATE, 10, 0)
    257. '
    258. 'Start previewing the image from the camera
    259. '
    260. SendMessage(hHwnd, WM_CAP_SET_PREVIEW, True, 0)
    261. '
    262. ' Resize window to fit in picturebox
    263. '
    264. SetWindowPos(hHwnd, HWND_BOTTOM, 0, 0, picCapture.Width, picCapture.Height, _
    265. SWP_NOMOVE Or SWP_NOZORDER)
    266. btnSave.Enabled = True
    267. btnStop.Enabled = True
    268. btnStart.Enabled = False
    269. Else
    270. '
    271. ' Error connecting to device close window
    272. '
    273. DestroyWindow(hHwnd)
    274. btnSave.Enabled = False
    275. End If
    276. End Sub
    277. Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
    278. iDevice = lstDevices.SelectedIndex
    279. OpenPreviewWindow()
    280. End Sub
    281. Private Sub ClosePreviewWindow()
    282. '
    283. ' Disconnect from device
    284. '
    285. SendMessage(hHwnd, WM_CAP_DRIVER_DISCONNECT, iDevice, 0)
    286. '
    287. ' close window
    288. '
    289. DestroyWindow(hHwnd)
    290. End Sub
    291. Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
    292. ClosePreviewWindow()
    293. btnSave.Enabled = False
    294. btnStart.Enabled = True
    295. btnStop.Enabled = False
    296. End Sub
    297. Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
    298. Text1.Text = CStr(i)
    299. Timer1.Interval = 10
    300. Timer1.Enabled = True
    301. End Sub
    302. Private Sub Form_Unload(ByVal Cancel As Integer)
    303. ' Beim Beenden, Timer deaktivieren
    304. Timer1.Enabled = False
    305. End Sub
    306. Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
    307. If btnStop.Enabled Then
    308. ClosePreviewWindow()
    309. End If
    310. End Sub
    311. Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    312. Dim FileName As String
    313. Dim data As IDataObject
    314. Dim bmap As Image
    315. i = i + 1
    316. ' Copy image to clipboard
    317. '
    318. SendMessage(hHwnd, WM_CAP_EDIT_COPY, 0, 0)
    319. '
    320. ' Get image from clipboard and convert it to a bitmap
    321. '
    322. data = Clipboard.GetDataObject()
    323. If data.GetDataPresent(GetType(System.Drawing.Bitmap)) Then
    324. bmap = CType(data.GetData(GetType(System.Drawing.Bitmap)), Image)
    325. picCapture.Image = bmap
    326. 'ClosePreviewWindow()
    327. 'btnSave.Enabled = False
    328. 'btnStop.Enabled = False
    329. 'btnStart.Enabled = True
    330. 'If sfdImage.ShowDialog = DialogResult.OK Then
    331. 'sfdImage.FileName
    332. If i < 10 Then
    333. FileName = "0" + CStr(i)
    334. Else
    335. FileName = CStr(i)
    336. End If
    337. Text1.Text = FileName
    338. bmap.Save("C:/test" + FileName + ".bmp", Imaging.ImageFormat.Bmp)
    339. End If
    340. End Sub
    341. Friend WithEvents Label1 As System.Windows.Forms.Label
    342. Friend WithEvents Label2 As System.Windows.Forms.Label
    343. Friend WithEvents Button1 As System.Windows.Forms.Button
    344. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    345. SendMessage(hHwnd, WM_CAP_DLG_VIDEOSOURCE, 0, 0)
    346. End Sub
    347. Private Sub lstDevices_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstDevices.SelectedIndexChanged
    348. End Sub
    349. End Class


    LG und Danke schonmal :)

    Markus
    Hallo,

    danke schonmal für deine antwort, leider treten aber in diesem programm genau dieselben symptome auf!
    Es die kamera um die es geht, wird übrigens nicht als webcam erkannt, sondern als videogerät.
    ich weiß nicht in wiefern das eine rolle spielt, als dass das die kamera ja prinzipiel erkannt wird!

    LG

    markus
    Ich habe jetzt im Programm "TVideoGrabber" gesehen dass über den Button "device" ein Fenster "Prperties" aufgeht, welches exakt (!!) so aussieht wie das von VLC! Ich könnte mir vorstellen dass es sich da z.B. um einen DirectX-Dialog handeln könnte - den ich ja auch ohne weiteres öffnen können müsste!