ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 效率神器,一键搞定繁琐工作
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
让更多数据处理,一键完成 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 21639|回复: 33

[求助] 做了一个摄像头采集照片的程序,采用VFW和GDI

  [复制链接]

TA的精华主题

TA的得分主题

发表于 2013-3-23 03:21 | 显示全部楼层 |阅读模式
本帖已被收录到知识树中,索引项:图像处理和GDI
本帖最后由 liucqa 于 2014-3-1 23:41 编辑

捕获.JPG
左中右三个窗口分别是:摄像头捕获预览窗口(子窗体)、裁切之后的图片显示窗口(Image控件)、正常捕获图片(Image控件)+裁切预览框


特点:
1、采用VFW技术,不使用第三方控件。
    VFW(Video for Windows)是Microsoft推出的关于数字视频的一个开发包。VFW推出了一整套完整的视频采集、压缩、解压缩、回放和编辑的应用程序接口(API)。
2、使用GDI操作Image控件图片的裁切,可以保存成jpg格式。
3、支持裁切框预览(用GDI绘制输出)

下面是建立捕获预览窗口以及连接摄像头的函数
  1. Public Function CreateCaptureWindow( _
  2.        hWndParent As Long, _
  3.        Optional x As Long = 0, _
  4.        Optional y As Long = 0, _
  5.        Optional nWidth As Long = 320, _
  6.        Optional nHeight As Long = 240, _
  7.        Optional nCameraID As Long = 0) As Long
  8.     Dim bRet As Long, n As Long, strAllDeviceName As String
  9.     n = 0
  10.     strAllDeviceName = ""
  11.     Do
  12.         bRet = capGetDriverDescriptionA(n, lpszName, 100, lpszVer, 100)
  13.         n = n + 1
  14.         If bRet Then strAllDeviceName = strAllDeviceName + TrimNull(lpszName) + vbCrLf
  15.     Loop While bRet
  16.     'MsgBox "设备数量:" & N - 1 & vbCrLf & "设备名称:" & vbCrLf & strAllDeviceName     '输出设备名称

  17.     '建立捕获窗口,以子窗口形式附在CapPicform窗体上
  18.     Preview_Handle = capCreateCaptureWindowA(TrimNull(lpszName), _
  19.                                              WS_CHILD + WS_VISIBLE, x, y, _
  20.                                              nWidth, nHeight, hWndParent, 1)

  21.     If capDriverConnect(Preview_Handle, nCameraID) > 0 Then       '成功返回1
  22.         Dim Caps As CAPDRIVERCAPS
  23.         capDriverGetCaps Preview_Handle, VarPtr(Caps), Len(Caps)
  24.         If Caps.fHasDlgVideoSource = 0 Then MsgBox ("不支持VideoSource设置")
  25.         'If Caps.fHasDlgVideoDisplay = 0 Then MsgBox ("不支持VideoDisplay设置")
  26.         If Caps.fHasDlgVideoFormat = 0 Then MsgBox ("不支持VideoFormat设置")

  27.         capPreviewScale Preview_Handle, True       '是否允许图像放缩
  28.         capPreviewRate Preview_Handle, 66          '采集间隔(毫秒为单位的变量,用来标识多长时间采集、显示一次。66是15帧/秒)
  29.         capPreview Preview_Handle, True            '预览
  30.         capPreview Preview_Handle, True            '预览模式,应用时overlay模式自动被禁止,preview模式占用cpu资源,
  31.         capOverlay Preview_Handle, False           '覆盖模式,应用后preview模式自动被禁止。
  32.     Else
  33.         MsgBox ("摄像头被占用")
  34.     End If
  35.     CreateCaptureWindow = Preview_Handle

  36. End Function
复制代码

测试VFW功能下摄像头是否工作正常的演示
测试摄像头v0.22.rar (46.8 KB, 下载次数: 1006)

***********************************************************************************************************

vfw技术由于比较古老,对摄像头的支持并不好,存在一些没法解决的问题,简单操作摄像头没什么问题,但要是对摄像头的操作有更高要求(例如多个摄像头同时采集、人工或者动态切换摄像头等),建议采用DirectShow技术。C#可以使用Aforge库来实现对摄像头的进一步操作。具体可以看下面链接中的例子。

对摄像头的操作示例,采用Aforge库






评分

3

查看全部评分

TA的精华主题

TA的得分主题

 楼主| 发表于 2013-3-23 03:34 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
本帖最后由 liucqa 于 2014-2-9 23:09 编辑

总结一下:
1、采用VFW技术对摄像头的兼容性比较好,不必采用第三方控件,代码部署方便。
2、VFW控制摄像头对驱动有要求,建议采用官方驱动,不要使用中星微芯片的通用驱动(可能会造成VFW工作不正常)
3、VFW暂时无法提前判断摄像头被占用情况,导致摄像头被占用时,会弹出视频源选择的窗口,目前只能用定时器解决。
4、VFW的capGetDriverDescription函数不能获取摄像头的友好名称,只能得到驱动的名称和版本,不太方便。

谁有这方面的解决办法,欢迎交流。


采用DirectX Show控制摄像头兼容性远远好于VFW,推荐采用C#开发。

TA的精华主题

TA的得分主题

 楼主| 发表于 2013-3-23 03:39 | 显示全部楼层
本帖最后由 liucqa 于 2013-3-23 03:45 编辑
  1. Option Explicit

  2. Public Const WM_USER = &H400
  3. Public Const WM_CLOSE = &H10              '关闭视频窗口

  4. Type POINTAPI
  5.         x As Long
  6.         y As Long
  7. End Type
  8. Declare Function SendMessage Lib "User32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Integer, ByVal lParam As Long) As Long
  9. Declare Function SendMessageS Lib "User32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Integer, ByVal lParam As String) As Long

  10. '// ------------------------------------------------------------------
  11. '//  Window Messages  WM_CAP... which can be sent to an AVICAP window
  12. '// ------------------------------------------------------------------

  13. '// Defines start of the message range
  14. Public Const WM_CAP_START = WM_USER

  15. Public Const WM_CAP_GET_CAPSTREAMPTR = WM_CAP_START + 1

  16. Public Const WM_CAP_SET_CALLBACK_ERROR = WM_CAP_START + 2
  17. Public Const WM_CAP_SET_CALLBACK_STATUS = WM_CAP_START + 3
  18. Public Const WM_CAP_SET_CALLBACK_YIELD = WM_CAP_START + 4
  19. Public Const WM_CAP_SET_CALLBACK_FRAME = WM_CAP_START + 5
  20. Public Const WM_CAP_SET_CALLBACK_VIDEOSTREAM = WM_CAP_START + 6
  21. Public Const WM_CAP_SET_CALLBACK_WAVESTREAM = WM_CAP_START + 7
  22. Public Const WM_CAP_GET_USER_DATA = WM_CAP_START + 8
  23. Public Const WM_CAP_SET_USER_DATA = WM_CAP_START + 9
  24.    
  25. Public Const WM_CAP_DRIVER_CONNECT = WM_CAP_START + 10
  26. Public Const WM_CAP_DRIVER_DISCONNECT = WM_CAP_START + 11
  27. Public Const WM_CAP_DRIVER_GET_NAME = WM_CAP_START + 12
  28. Public Const WM_CAP_DRIVER_GET_VERSION = WM_CAP_START + 13
  29. Public Const WM_CAP_DRIVER_GET_CAPS = WM_CAP_START + 14

  30. Public Const WM_CAP_FILE_SET_CAPTURE_FILE = WM_CAP_START + 20
  31. Public Const WM_CAP_FILE_GET_CAPTURE_FILE = WM_CAP_START + 21
  32. Public Const WM_CAP_FILE_ALLOCATE = WM_CAP_START + 22
  33. Public Const WM_CAP_FILE_SAVEAS = WM_CAP_START + 23
  34. Public Const WM_CAP_FILE_SET_INFOCHUNK = WM_CAP_START + 24
  35. Public Const WM_CAP_FILE_SAVEDIB = WM_CAP_START + 25

  36. Public Const WM_CAP_EDIT_COPY = WM_CAP_START + 30

  37. Public Const WM_CAP_SET_AUDIOFORMAT = WM_CAP_START + 35
  38. Public Const WM_CAP_GET_AUDIOFORMAT = WM_CAP_START + 36

  39. Public Const WM_CAP_DLG_VIDEOFORMAT = WM_CAP_START + 41
  40. Public Const WM_CAP_DLG_VIDEOSOURCE = WM_CAP_START + 42
  41. Public Const WM_CAP_DLG_VIDEODISPLAY = WM_CAP_START + 43
  42. Public Const WM_CAP_GET_VIDEOFORMAT = WM_CAP_START + 44
  43. Public Const WM_CAP_SET_VIDEOFORMAT = WM_CAP_START + 45
  44. Public Const WM_CAP_DLG_VIDEOCOMPRESSION = WM_CAP_START + 46

  45. Public Const WM_CAP_SET_PREVIEW = WM_CAP_START + 50
  46. Public Const WM_CAP_SET_OVERLAY = WM_CAP_START + 51
  47. Public Const WM_CAP_SET_PREVIEWRATE = WM_CAP_START + 52
  48. Public Const WM_CAP_SET_SCALE = WM_CAP_START + 53
  49. Public Const WM_CAP_GET_STATUS = WM_CAP_START + 54
  50. Public Const WM_CAP_SET_SCROLL = WM_CAP_START + 55

  51. Public Const WM_CAP_GRAB_FRAME = WM_CAP_START + 60
  52. Public Const WM_CAP_GRAB_FRAME_NOSTOP = WM_CAP_START + 61

  53. Public Const WM_CAP_SEQUENCE = WM_CAP_START + 62
  54. Public Const WM_CAP_SEQUENCE_NOFILE = WM_CAP_START + 63
  55. Public Const WM_CAP_SET_SEQUENCE_SETUP = WM_CAP_START + 64
  56. Public Const WM_CAP_GET_SEQUENCE_SETUP = WM_CAP_START + 65
  57. Public Const WM_CAP_SET_MCI_DEVICE = WM_CAP_START + 66
  58. Public Const WM_CAP_GET_MCI_DEVICE = WM_CAP_START + 67
  59. Public Const WM_CAP_STOP = WM_CAP_START + 68
  60. Public Const WM_CAP_ABORT = WM_CAP_START + 69

  61. Public Const WM_CAP_SINGLE_FRAME_OPEN = WM_CAP_START + 70
  62. Public Const WM_CAP_SINGLE_FRAME_CLOSE = WM_CAP_START + 71
  63. Public Const WM_CAP_SINGLE_FRAME = WM_CAP_START + 72

  64. Public Const WM_CAP_PAL_OPEN = WM_CAP_START + 80
  65. Public Const WM_CAP_PAL_SAVE = WM_CAP_START + 81
  66. Public Const WM_CAP_PAL_PASTE = WM_CAP_START + 82
  67. Public Const WM_CAP_PAL_AUTOCREATE = WM_CAP_START + 83
  68. Public Const WM_CAP_PAL_MANUALCREATE = WM_CAP_START + 84

  69. '// Following added post VFW 1.1
  70. Public Const WM_CAP_SET_CALLBACK_CAPCONTROL = WM_CAP_START + 85

  71. '// Defines end of the message range
  72. Public Const WM_CAP_END = WM_CAP_SET_CALLBACK_CAPCONTROL

  73. '// ------------------------------------------------------------------
  74. '//  Structures
  75. '// ------------------------------------------------------------------
  76. Type CAPDRIVERCAPS
  77.     wDeviceIndex As Long '               // Driver index in system.ini
  78.     fHasOverlay As Long '                // Can device overlay?
  79.     fHasDlgVideoSource As Long '         // Has Video source dlg?
  80.     fHasDlgVideoFormat As Long '         // Has Format dlg?
  81.     fHasDlgVideoDisplay As Long '        // Has External out dlg?
  82.     fCaptureInitialized As Long '        // Driver ready to capture?
  83.     fDriverSuppliesPalettes As Long '    // Can driver make palettes?
  84.     hVideoIn As Long '                   // Driver In channel
  85.     hVideoOut As Long '                  // Driver Out channel
  86.     hVideoExtIn As Long '                // Driver Ext In channel
  87.     hVideoExtOut As Long '               // Driver Ext Out channel
  88. End Type

  89. Type CAPSTATUS
  90.     uiImageWidth As Long                    '// Width of the image
  91.     uiImageHeight As Long                   '// Height of the image
  92.     fLiveWindow As Long                     '// Now Previewing video?
  93.     fOverlayWindow As Long                  '// Now Overlaying video?
  94.     fScale As Long                          '// Scale image to client?
  95.     ptScroll As POINTAPI                    '// Scroll position
  96.     fUsingDefaultPalette As Long            '// Using default driver palette?
  97.     fAudioHardware As Long                  '// Audio hardware present?
  98.     fCapFileExists As Long                  '// Does capture file exist?
  99.     dwCurrentVideoFrame As Long             '// # of video frames cap'td
  100.     dwCurrentVideoFramesDropped As Long     '// # of video frames dropped
  101.     dwCurrentWaveSamples As Long            '// # of wave samples cap'td
  102.     dwCurrentTimeElapsedMS As Long          '// Elapsed capture duration
  103.     hPalCurrent As Long                     '// Current palette in use
  104.     fCapturingNow As Long                   '// Capture in progress?
  105.     dwReturn As Long                        '// Error value after any operation
  106.     wNumVideoAllocated As Long              '// Actual number of video buffers
  107.     wNumAudioAllocated As Long              '// Actual number of audio buffers
  108. End Type

  109. Type CAPTUREPARMS
  110.     dwRequestMicroSecPerFrame As Long       '// Requested capture rate
  111.     fMakeUserHitOKToCapture As Long         '// Show "Hit OK to cap" dlg?
  112.     wPercentDropForError As Long            '// Give error msg if > (10%)
  113.     fYield As Long                          '// Capture via background task?
  114.     dwIndexSize As Long                     '// Max index size in frames (32K)
  115.     wChunkGranularity As Long               '// Junk chunk granularity (2K)
  116.     fUsingDOSMemory As Long                 '// Use DOS buffers?
  117.     wNumVideoRequested As Long              '// # video buffers, If 0, autocalc
  118.     fCaptureAudio As Long                   '// Capture audio?
  119.     wNumAudioRequested As Long              '// # audio buffers, If 0, autocalc
  120.     vKeyAbort As Long                       '// Virtual key causing abort
  121.     fAbortLeftMouse As Long                 '// Abort on left mouse?
  122.     fAbortRightMouse As Long                '// Abort on right mouse?
  123.     fLimitEnabled As Long                   '// Use wTimeLimit?
  124.     wTimeLimit As Long                      '// Seconds to capture
  125.     fMCIControl As Long                     '// Use MCI video source?
  126.     fStepMCIDevice As Long                  '// Step MCI device?
  127.     dwMCIStartTime As Long                  '// Time to start in MS
  128.     dwMCIStopTime As Long                   '// Time to stop in MS
  129.     fStepCaptureAt2x As Long                '// Perform spatial averaging 2x
  130.     wStepCaptureAverageFrames As Long       '// Temporal average n Frames
  131.     dwAudioBufferSize As Long               '// Size of audio bufs (0 = default)
  132.     fDisableWriteCache As Long              '// Attempt to disable write cache
  133. End Type

  134. Type CAPINFOCHUNK
  135.     fccInfoID As Long                       '// Chunk ID, "ICOP" for copyright
  136.     lpData As Long                          '// pointer to data
  137.     cbData As Long                          '// size of lpData
  138. End Type

  139. Type VIDEOHDR
  140.     lpData As Long '// address of video buffer
  141.     dwBufferLength As Long '// size, in bytes, of the Data buffer
  142.     dwBytesUsed As Long '// see below
  143.     dwTimeCaptured As Long '// see below
  144.     dwUser As Long '// user-specific data
  145.     dwFlags As Long '// see below
  146.     dwReserved(3) As Long '// reserved; do not use}
  147. End Type

  148. Public Const WS_CHILD = &H40000000   '使用子窗口
  149. Public Const WS_VISIBLE = &H10000000 '创建一个初始不可用的窗体
  150. Public Const WS_CAPTION = &HC00000   '创建一个有标题栏的窗体(隐含了WS_BORDER)
  151. Public Const WS_BORDER = &H800000    '创建一个有边框的窗体
  152. Public Const WS_OVERLAPPED = &H0&    '创建一个重叠窗体?一个重叠窗体一般有标题和边框
  153. Public Const WS_POPUP = &H80000000   '创建一个弹出式窗体,不能和WS_CHILD一起使用



  154. '// The two functions exported by AVICap
  155. Declare Function capCreateCaptureWindowA Lib "avicap32.dll" ( _
  156.     ByVal lpszWindowName As String, _
  157.     ByVal dwStyle As Long, _
  158.     ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Integer, _
  159.     ByVal hWndParent As Long, ByVal nID As Long) As Long
  160. Declare Function capGetDriverDescriptionA Lib "avicap32.dll" ( _
  161.     ByVal wDriver As Integer, _
  162.     ByVal lpszName As String, _
  163.     ByVal cbName As Long, _
  164.     ByVal lpszVer As String, _
  165.     ByVal cbVer As Long) As Boolean

  166. '// ------------------------------------------------------------------
  167. '// String IDs from status and error callbacks
  168. '// ------------------------------------------------------------------

  169. Public Const IDS_CAP_BEGIN = 300              '/* "Capture Start" */
  170. Public Const IDS_CAP_END = 301                '/* "Capture End" */

  171. Public Const IDS_CAP_INFO = 401               '/* "%s" */
  172. Public Const IDS_CAP_OUTOFMEM = 402           '/* "Out of memory" */
  173. Public Const IDS_CAP_FILEEXISTS = 403         '/* "File '%s' exists -- overwrite it?" */
  174. Public Const IDS_CAP_ERRORPALOPEN = 404       '/* "Error opening palette '%s'" */
  175. Public Const IDS_CAP_ERRORPALSAVE = 405       '/* "Error saving palette '%s'" */
  176. Public Const IDS_CAP_ERRORDIBSAVE = 406       '/* "Error saving frame '%s'" */
  177. Public Const IDS_CAP_DEFAVIEXT = 407          '/* "avi" */
  178. Public Const IDS_CAP_DEFPALEXT = 408          '/* "pal" */
  179. Public Const IDS_CAP_CANTOPEN = 409           '/* "Cannot open '%s'" */
  180. Public Const IDS_CAP_SEQ_MSGSTART = 410       '/* "Select OK to start capture\nof video sequence\nto %s." */
  181. Public Const IDS_CAP_SEQ_MSGSTOP = 411        '/* "Hit ESCAPE or click to end capture" */
  182.                
  183. Public Const IDS_CAP_VIDEDITERR = 412         '/* "An error occurred while trying to run VidEdit." */
  184. Public Const IDS_CAP_READONLYFILE = 413       '/* "The file '%s' is a read-only file." */
  185. Public Const IDS_CAP_WRITEERROR = 414         '/* "Unable to write to file '%s'.\nDisk may be full." */
  186. Public Const IDS_CAP_NODISKSPACE = 415        '/* "There is no space to create a capture file on the specified device." */
  187. Public Const IDS_CAP_SETFILESIZE = 416        '/* "Set File Size" */
  188. Public Const IDS_CAP_SAVEASPERCENT = 417      '/* "SaveAs: %2ld%%  Hit Escape to abort." */
  189.                
  190. Public Const IDS_CAP_DRIVER_ERROR = 418       '/* Driver specific error message */

  191. Public Const IDS_CAP_WAVE_OPEN_ERROR = 419    '/* "Error: Cannot open the wave input device.\nCheck sample size, frequency, and channels." */
  192. Public Const IDS_CAP_WAVE_ALLOC_ERROR = 420   '/* "Error: Out of memory for wave buffers." */
  193. Public Const IDS_CAP_WAVE_PREPARE_ERROR = 421 '/* "Error: Cannot prepare wave buffers." */
  194. Public Const IDS_CAP_WAVE_ADD_ERROR = 422     '/* "Error: Cannot add wave buffers." */
  195. Public Const IDS_CAP_WAVE_SIZE_ERROR = 423    '/* "Error: Bad wave size." */
  196.                
  197. Public Const IDS_CAP_VIDEO_OPEN_ERROR = 424   '/* "Error: Cannot open the video input device." */
  198. Public Const IDS_CAP_VIDEO_ALLOC_ERROR = 425  '/* "Error: Out of memory for video buffers." */
  199. Public Const IDS_CAP_VIDEO_PREPARE_ERROR = 426 '/* "Error: Cannot prepare video buffers." */
  200. Public Const IDS_CAP_VIDEO_ADD_ERROR = 427    '/* "Error: Cannot add video buffers." */
  201. Public Const IDS_CAP_VIDEO_SIZE_ERROR = 428   '/* "Error: Bad video size." */
  202.                
  203. Public Const IDS_CAP_FILE_OPEN_ERROR = 429    '/* "Error: Cannot open capture file." */
  204. Public Const IDS_CAP_FILE_WRITE_ERROR = 430   '/* "Error: Cannot write to capture file.  Disk may be full." */
  205. Public Const IDS_CAP_RECORDING_ERROR = 431    '/* "Error: Cannot write to capture file.  Data rate too high or disk full." */
  206. Public Const IDS_CAP_RECORDING_ERROR2 = 432   '/* "Error while recording" */
  207. Public Const IDS_CAP_AVI_INIT_ERROR = 433     '/* "Error: Unable to initialize for capture." */
  208. Public Const IDS_CAP_NO_FRAME_CAP_ERROR = 434 '/* "Warning: No frames captured.\nConfirm that vertical sync interrupts\nare configured and enabled." */
  209. Public Const IDS_CAP_NO_PALETTE_WARN = 435    '/* "Warning: Using default palette." */
  210. Public Const IDS_CAP_MCI_CONTROL_ERROR = 436  '/* "Error: Unable to access MCI device." */
  211. Public Const IDS_CAP_MCI_CANT_STEP_ERROR = 437 '/* "Error: Unable to step MCI device." */
  212. Public Const IDS_CAP_NO_AUDIO_CAP_ERROR = 438 '/* "Error: No audio data captured.\nCheck audio card settings." */
  213. Public Const IDS_CAP_AVI_DRAWDIB_ERROR = 439  '/* "Error: Unable to draw this data format." */
  214. Public Const IDS_CAP_COMPRESSOR_ERROR = 440   '/* "Error: Unable to initialize compressor." */
  215. Public Const IDS_CAP_AUDIO_DROP_ERROR = 441   '/* "Error: Audio data was lost during capture, reduce capture rate." */
  216.                
  217. '/* status string IDs */
  218. Public Const IDS_CAP_STAT_LIVE_MODE = 500      '/* "Live window" */
  219. Public Const IDS_CAP_STAT_OVERLAY_MODE = 501   '/* "Overlay window" */
  220. Public Const IDS_CAP_STAT_CAP_INIT = 502       '/* "Setting up for capture - Please wait" */
  221. Public Const IDS_CAP_STAT_CAP_FINI = 503       '/* "Finished capture, now writing frame %ld" */
  222. Public Const IDS_CAP_STAT_PALETTE_BUILD = 504  '/* "Building palette map" */
  223. Public Const IDS_CAP_STAT_OPTPAL_BUILD = 505   '/* "Computing optimal palette" */
  224. Public Const IDS_CAP_STAT_I_FRAMES = 506       '/* "%d frames" */
  225. Public Const IDS_CAP_STAT_L_FRAMES = 507       '/* "%ld frames" */
  226. Public Const IDS_CAP_STAT_CAP_L_FRAMES = 508   '/* "Captured %ld frames" */
  227. Public Const IDS_CAP_STAT_CAP_AUDIO = 509      '/* "Capturing audio" */
  228. Public Const IDS_CAP_STAT_VIDEOCURRENT = 510   '/* "Captured %ld frames (%ld dropped) %d.%03d sec." */
  229. Public Const IDS_CAP_STAT_VIDEOAUDIO = 511     '/* "Captured %d.%03d sec.  %ld frames (%ld dropped) (%d.%03d fps).  %ld audio bytes (%d,%03d sps)" */
  230. Public Const IDS_CAP_STAT_VIDEOONLY = 512      '/* "Captured %d.%03d sec.  %ld frames (%ld dropped) (%d.%03d fps)" */




复制代码

对摄像头进行操作的模块代码(就是封装了SendMessage的调用,方便使用,代码来自网上。),供大家参考,有问题请百度。



TA的精华主题

TA的得分主题

 楼主| 发表于 2013-3-23 03:41 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
本帖最后由 liucqa 于 2013-3-23 03:46 编辑

续上楼

  1. Function capSetCallbackOnError(ByVal lwnd As Long, ByVal lpProc As Long) As Boolean
  2.    capSetCallbackOnError = SendMessage(lwnd, WM_CAP_SET_CALLBACK_ERROR, 0, lpProc)
  3. End Function
  4. Function capSetCallbackOnStatus(ByVal lwnd As Long, ByVal lpProc As Long) As Boolean
  5.    capSetCallbackOnStatus = SendMessage(lwnd, WM_CAP_SET_CALLBACK_STATUS, 0, lpProc)
  6. End Function
  7. Function capSetCallbackOnYield(ByVal lwnd As Long, ByVal lpProc As Long) As Boolean
  8.    capSetCallbackOnYield = SendMessage(lwnd, WM_CAP_SET_CALLBACK_YIELD, 0, lpProc)
  9. End Function
  10. Function capSetCallbackOnFrame(ByVal lwnd As Long, ByVal lpProc As Long) As Boolean
  11.    capSetCallbackOnFrame = SendMessage(lwnd, WM_CAP_SET_CALLBACK_FRAME, 0, lpProc)
  12. End Function
  13. Function capSetCallbackOnVideoStream(ByVal lwnd As Long, ByVal lpProc As Long) As Boolean
  14.    capSetCallbackOnVideoStream = SendMessage(lwnd, WM_CAP_SET_CALLBACK_VIDEOSTREAM, 0, lpProc)
  15. End Function
  16. Function capSetCallbackOnWaveStream(ByVal lwnd As Long, ByVal lpProc As Long) As Boolean
  17.    capSetCallbackOnWaveStream = SendMessage(lwnd, WM_CAP_SET_CALLBACK_WAVESTREAM, 0, lpProc)
  18. End Function
  19. Function capSetCallbackOnCapControl(ByVal lwnd As Long, ByVal lpProc As Long) As Boolean
  20.    capSetCallbackOnCapControl = SendMessage(lwnd, WM_CAP_SET_CALLBACK_CAPCONTROL, 0, lpProc)
  21. End Function
  22. Function capSetUserData(ByVal lwnd As Long, ByVal lUser As Long) As Boolean
  23.    capSetUserData = SendMessage(lwnd, WM_CAP_SET_USER_DATA, 0, lUser)
  24. End Function
  25. Function capGetUserData(ByVal lwnd As Long) As Long
  26.    capGetUserData = SendMessage(lwnd, WM_CAP_GET_USER_DATA, 0, 0)
  27. End Function
  28. Function capDriverConnect(ByVal lwnd As Long, ByVal I As Integer) As Long
  29.    capDriverConnect = SendMessage(lwnd, WM_CAP_DRIVER_CONNECT, I, 0)
  30. End Function
  31. Function capDriverDisconnect(ByVal lwnd As Long) As Boolean
  32.    capDriverDisconnect = SendMessage(lwnd, WM_CAP_DRIVER_DISCONNECT, 0, 0)
  33. End Function
  34. Function capDriverGetName(ByVal lwnd As Long, ByVal szName As Long, ByVal wSize As Integer) As Boolean
  35.    capDriverGetName = SendMessage(lwnd, YOURCONSTANTMESSAGE, wSize, szName)
  36. End Function
  37. Function capDriverGetVersion(ByVal lwnd As Long, ByVal szVer As Long, ByVal wSize As Integer) As Boolean
  38.    capDriverGetVersion = SendMessage(lwnd, WM_CAP_DRIVER_GET_VERSION, wSize, szVer)
  39. End Function
  40. Function capDriverGetCaps(ByVal lwnd As Long, ByVal s As Long, ByVal wSize As Integer) As Boolean
  41.    capDriverGetCaps = SendMessage(lwnd, WM_CAP_DRIVER_GET_CAPS, wSize, s)
  42. End Function
  43. Function capFileSetCaptureFile(ByVal lwnd As Long, szName As String) As Boolean
  44.    capFileSetCaptureFile = SendMessageS(lwnd, WM_CAP_FILE_SET_CAPTURE_FILE, 0, szName)
  45. End Function
  46. Function capFileGetCaptureFile(ByVal lwnd As Long, ByVal szName As Long, wSize As String) As Boolean
  47.    capFileGetCaptureFile = SendMessageS(lwnd, WM_CAP_FILE_SET_CAPTURE_FILE, wSize, szName)
  48. End Function
  49. Function capFileAlloc(ByVal lwnd As Long, ByVal dwSize As Long) As Boolean
  50.    capFileAlloc = SendMessage(lwnd, WM_CAP_FILE_ALLOCATE, 0, dwSize)
  51. End Function
  52. Function capFileSaveAs(ByVal lwnd As Long, szName As String) As Boolean
  53.    capFileSaveAs = SendMessageS(lwnd, WM_CAP_FILE_SAVEAS, 0, szName)
  54. End Function
  55. Function capFileSetInfoChunk(ByVal lwnd As Long, ByVal lpInfoChunk As Long) As Boolean
  56.    capFileSetInfoChunk = SendMessage(lwnd, WM_CAP_FILE_SET_INFOCHUNK, 0, lpInfoChunk)
  57. End Function
  58. Function capFileSaveDIB(ByVal lwnd As Long, ByVal szName As Long) As Boolean
  59.    capFileSaveDIB = SendMessage(lwnd, WM_CAP_FILE_SAVEDIB, 0, szName)
  60. End Function
  61. Function capEditCopy(ByVal lwnd As Long) As Boolean
  62.    capEditCopy = SendMessage(lwnd, WM_CAP_EDIT_COPY, 0, 0)
  63. End Function
  64. Function capSetAudioFormat(ByVal lwnd As Long, ByVal s As Long, ByVal wSize As Integer) As Boolean
  65.    capSetAudioFormat = SendMessage(lwnd, WM_CAP_SET_AUDIOFORMAT, wSize, s)
  66. End Function
  67. Function capGetAudioFormat(ByVal lwnd As Long, ByVal s As Long, ByVal wSize As Integer) As Long
  68.    capGetAudioFormat = SendMessage(lwnd, WM_CAP_GET_AUDIOFORMAT, wSize, s)
  69. End Function
  70. Function capGetAudioFormatSize(ByVal lwnd As Long) As Long
  71.    capGetAudioFormatSize = SendMessage(lwnd, WM_CAP_GET_AUDIOFORMAT, 0, 0)
  72. End Function
  73. Function capDlgVideoFormat(ByVal lwnd As Long) As Boolean
  74.    capDlgVideoFormat = SendMessage(lwnd, WM_CAP_DLG_VIDEOFORMAT, 0, 0)
  75. End Function
  76. Function capDlgVideoSource(ByVal lwnd As Long) As Boolean
  77.    capDlgVideoSource = SendMessage(lwnd, WM_CAP_DLG_VIDEOSOURCE, 0, 0)
  78. End Function
  79. Function capDlgVideoDisplay(ByVal lwnd As Long) As Boolean
  80.    capDlgVideoDisplay = SendMessage(lwnd, WM_CAP_DLG_VIDEODISPLAY, 0, 0)
  81. End Function
  82. Function capDlgVideoCompression(ByVal lwnd As Long) As Boolean
  83.    capDlgVideoCompression = SendMessage(lwnd, WM_CAP_DLG_VIDEOCOMPRESSION, 0, 0)
  84. End Function
  85. Function capGetVideoFormat(ByVal lwnd As Long, ByVal s As Long, ByVal wSize As Integer) As Long
  86.    capGetVideoFormat = SendMessage(lwnd, WM_CAP_GET_VIDEOFORMAT, wSize, s)
  87. End Function
  88. Function capGetVideoFormatSize(ByVal lwnd As Long) As Long
  89.    capGetVideoFormatSize = SendMessage(lwnd, WM_CAP_GET_VIDEOFORMAT, 0, 0)
  90. End Function
  91. Function capSetVideoFormat(ByVal lwnd As Long, ByVal s As Long, ByVal wSize As Integer) As Boolean
  92.    capSetVideoFormat = SendMessage(lwnd, WM_CAP_SET_VIDEOFORMAT, wSize, s)
  93. End Function
  94. Function capPreview(ByVal lwnd As Long, ByVal f As Boolean) As Boolean
  95.    capPreview = SendMessage(lwnd, WM_CAP_SET_PREVIEW, f, 0)
  96. End Function
  97. Function capPreviewRate(ByVal lwnd As Long, ByVal wMS As Integer) As Boolean
  98.    capPreviewRate = SendMessage(lwnd, WM_CAP_SET_PREVIEWRATE, wMS, 0)
  99. End Function
  100. Function capOverlay(ByVal lwnd As Long, ByVal f As Boolean) As Boolean
  101.    capOverlay = SendMessage(lwnd, WM_CAP_SET_OVERLAY, f, 0)
  102. End Function
  103. Function capPreviewScale(ByVal lwnd As Long, ByVal f As Boolean) As Boolean
  104.    capPreviewScale = SendMessage(lwnd, WM_CAP_SET_SCALE, f, 0)
  105. End Function
  106. Function capGetStatus(ByVal lwnd As Long, ByVal s As Long, ByVal wSize As Integer) As Boolean
  107.    capGetStatus = SendMessage(lwnd, WM_CAP_GET_STATUS, wSize, s)
  108. End Function
  109. Function capSetScrollPos(ByVal lwnd As Long, ByVal lpP As Long) As Boolean
  110.    capSetScrollPos = SendMessage(lwnd, WM_CAP_SET_SCROLL, 0, lpP)
  111. End Function
  112. Function capGrabFrame(ByVal lwnd As Long) As Boolean
  113.    capGrabFrame = SendMessage(lwnd, WM_CAP_GRAB_FRAME, 0, 0)
  114. End Function
  115. Function capGrabFrameNoStop(ByVal lwnd As Long) As Boolean
  116.    capGrabFrameNoStop = SendMessage(lwnd, WM_CAP_GRAB_FRAME_NOSTOP, 0, 0)
  117. End Function
  118. Function capCaptureSequence(ByVal lwnd As Long) As Boolean
  119.    capCaptureSequence = SendMessage(lwnd, WM_CAP_SEQUENCE, 0, 0)
  120. End Function
  121. Function capCaptureSequenceNoFile(ByVal lwnd As Long) As Boolean
  122.    capCaptureSequenceNoFile = SendMessage(lwnd, WM_CAP_SEQUENCE_NOFILE, 0, 0)
  123. End Function
  124. Function capCaptureStop(ByVal lwnd As Long) As Boolean
  125.    capCaptureStop = SendMessage(lwnd, WM_CAP_STOP, 0, 0)
  126. End Function
  127. Function capCaptureAbort(ByVal lwnd As Long) As Boolean
  128.    capCaptureAbort = SendMessage(lwnd, WM_CAP_ABORT, 0, 0)
  129. End Function
  130. Function capCaptureSingleFrameOpen(ByVal lwnd As Long) As Boolean
  131.    capCaptureSingleFrameOpen = SendMessage(lwnd, WM_CAP_SINGLE_FRAME_OPEN, 0, 0)
  132. End Function
  133. Function capCaptureSingleFrameClose(ByVal lwnd As Long) As Boolean
  134.    capCaptureSingleFrameClose = SendMessage(lwnd, WM_CAP_SINGLE_FRAME_CLOSE, 0, 0)
  135. End Function
  136. Function capCaptureSingleFrame(ByVal lwnd As Long) As Boolean
  137.    capCaptureSingleFrame = SendMessage(lwnd, WM_CAP_SINGLE_FRAME, 0, 0)
  138. End Function
  139. Function capCaptureGetSetup(ByVal lwnd As Long, ByVal s As Long, ByVal wSize As Integer) As Boolean
  140.    capCaptureGetSetup = SendMessage(lwnd, WM_CAP_GET_SEQUENCE_SETUP, wSize, s)
  141. End Function
  142. Function capCaptureSetSetup(ByVal lwnd As Long, ByVal s As Long, ByVal wSize As Integer) As Boolean
  143.    capCaptureSetSetup = SendMessage(lwnd, WM_CAP_SET_SEQUENCE_SETUP, wSize, s)
  144. End Function
  145. Function capSetMCIDeviceName(ByVal lwnd As Long, ByVal szName As Long) As Boolean
  146.    capSetMCIDeviceName = SendMessage(lwnd, WM_CAP_SET_MCI_DEVICE, 0, szName)
  147. End Function
  148. Function capGetMCIDeviceName(ByVal lwnd As Long, ByVal szName As Long, ByVal wSize As Integer) As Boolean
  149.    capGetMCIDeviceName = SendMessage(lwnd, WM_CAP_GET_MCI_DEVICE, wSize, szName)
  150. End Function
  151. Function capPaletteOpen(ByVal lwnd As Long, ByVal szName As Long) As Boolean
  152.    capPaletteOpen = SendMessage(lwnd, WM_CAP_PAL_OPEN, 0, szName)
  153. End Function
  154. Function capPaletteSave(ByVal lwnd As Long, ByVal szName As Long) As Boolean
  155.    capPaletteSave = SendMessage(lwnd, WM_CAP_PAL_SAVE, 0, szName)
  156. End Function
  157. Function capPalettePaste(ByVal lwnd As Long) As Boolean
  158.    capPalettePaste = SendMessage(lwnd, WM_CAP_PAL_PASTE, 0, 0)
  159. End Function
  160. Function capPaletteAuto(ByVal lwnd As Long, ByVal iFrames As Integer, ByVal iColor As Long) As Boolean
  161.    capPaletteAuto = SendMessage(lwnd, WM_CAP_PAL_AUTOCREATE, iFrames, iColors)
  162. End Function
  163. Function capPaletteManual(ByVal lwnd As Long, ByVal fGrab As Boolean, ByVal iColors As Long) As Boolean
  164.    capPaletteManual = SendMessage(lwnd, WM_CAP_PAL_MANUALCREATE, fGrab, iColors)
  165. End Function
复制代码

原来的代码个别函数返回值不正确,需要修改。

TA的精华主题

TA的得分主题

 楼主| 发表于 2013-3-23 03:54 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
本帖最后由 liucqa 于 2013-3-23 03:56 编辑

capEditCopy函数可以把图片以bmp格式复制到剪贴板,使用gdi可以将剪贴板的bmp图片以jpg格式保存到本地硬盘。
剪贴板保存成jpg的代码参考6楼,把图片句柄换成GetClipboardData(CF_BITMAP)即可。

下面是断开摄像头的代码
  1. Public Sub Disconnects(nCaptureHandle As Long, Optional nCameraID = 0)
  2.     If capDriverDisconnect(nCaptureHandle) = False Then      '断开连接
  3.         MsgBox "断开与摄像头连接失败,请检查是否有应用程序冲突,或更换摄像头驱动!"
  4.     End If
  5.     'SendMessage nCaptureHandle, WM_CAP_DRIVER_DISCONNECT, nCameraID, 0
  6.     SendMessage nCaptureHandle, WM_CLOSE, 0, 0                              '关闭视频窗口
  7.     nCaptureHandle = 0
  8. End Sub
复制代码

TA的精华主题

TA的得分主题

 楼主| 发表于 2013-3-23 03:56 | 显示全部楼层

下面是通过Gdi保存Image控件的图片为jpg格式文件的代码,看不懂的请百度。
  1. Public
  2. Function StdPictureSave2JPG(ByVal picture As StdPicture, ByVal
  3. FileFullName As String, Optional ByVal quality As Byte = 95) As Boolean

  4.     Dim tSI As GdiplusStartupInput
  5.     Dim lRes As Long
  6.     Dim lGDIP As Long
  7.     Dim lBitmap As Long
  8.     Dim hBitmap As Long

  9.     '初始化 GDI+
  10.     tSI.GdiplusVersion = 1
  11.     lRes = GdiplusStartup(lGDIP, tSI, 0)

  12.     If lRes = 0 Then
  13.         lRes = GdipCreateBitmapFromHBITMAP(picture.Handle, 0, lBitmap)
  14.         If lRes = 0 Then
  15.             Dim tJpgEncoder As GUID
  16.             Dim tParams As EncoderParameters
  17.             CLSIDFromString StrPtr("{557CF401-1A04-11D3-9A73-0000F81EF32E}"), tJpgEncoder
  18.             tParams.Count = 1
  19.             With tParams.Parameter      ' Quality
  20.                 CLSIDFromString StrPtr("{1D5BE4B5-FA4A-452D-9CDD-5DB35105E7EB}"), .GUID
  21.                 .NumberOfValues = 1
  22.                 .Type = 4
  23.                 .Value = VarPtr(quality)
  24.             End With
  25.             lRes = GdipSaveImageToFile(lBitmap, StrPtr(FileFullName), tJpgEncoder, tParams)

  26.             GdipDisposeImage lBitmap
  27.         End If
  28.         GdiplusShutdown lGDIP
  29.     End If
  30.     StdPictureSave2JPG = Not lRes
  31. End Function
复制代码



TA的精华主题

TA的得分主题

 楼主| 发表于 2013-3-23 03:59 | 显示全部楼层
本帖最后由 liucqa 于 2013-3-23 13:56 编辑

Image控件的图片保存成jpg的函数

  1. Public Function StdPictureSave2JPG(ByVal picture As StdPicture, ByVal FileFullName As String, Optional ByVal quality As Byte = 95) As Boolean

  2.     Dim tSI As GdiplusStartupInput
  3.     Dim lRes As Long
  4.     Dim lGDIP As Long
  5.     Dim lBitmap As Long
  6.     Dim hBitmap As Long

  7.     '初始化 GDI+
  8.     tSI.GdiplusVersion = 1
  9.     lRes = GdiplusStartup(lGDIP, tSI, 0)

  10.     If lRes = 0 Then
  11.         lRes = GdipCreateBitmapFromHBITMAP(picture.Handle, 0, lBitmap)
  12.         If lRes = 0 Then
  13.             Dim tJpgEncoder As GUID
  14.             Dim tParams As EncoderParameters
  15.             CLSIDFromString StrPtr("{557CF401-1A04-11D3-9A73-0000F81EF32E}"), tJpgEncoder
  16.             tParams.Count = 1
  17.             With tParams.Parameter      ' Quality
  18.                 CLSIDFromString StrPtr("{1D5BE4B5-FA4A-452D-9CDD-5DB35105E7EB}"), .GUID
  19.                 .NumberOfValues = 1
  20.                 .Type = 4
  21.                 .Value = VarPtr(quality)
  22.             End With
  23.             lRes = GdipSaveImageToFile(lBitmap, StrPtr(FileFullName), tJpgEncoder, tParams)

  24.             GdipDisposeImage lBitmap
  25.         End If
  26.         GdiplusShutdown lGDIP
  27.     End If
  28.     StdPictureSave2JPG = Not lRes
  29. End Function
复制代码


**************************************************

关于VFW和GDI+的使用,需要使用者具备一定的API知识,代码没什么难度,会抄就行。关键是肯不肯静下心来慢慢学习。

API不可怕,觉得它可怕的,一般都是没自信,被长长的参数调用给吓跑了。

{:soso_e113:}




TA的精华主题

TA的得分主题

发表于 2013-3-23 15:41 | 显示全部楼层
学习一下,这次没把核心部分做成DLL,非常感谢。

另外,如果用手机摄像头做PC的外置摄像头,是否有这可能?

TA的精华主题

TA的得分主题

 楼主| 发表于 2013-3-23 15:50 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
kangatang 发表于 2013-3-23 15:41
学习一下,这次没把核心部分做成DLL,非常感谢。

另外,如果用手机摄像头做PC的外置摄像头,是否有这可能 ...

如果你能搞到驱动,自然就行{:soso_e151:}

TA的精华主题

TA的得分主题

发表于 2013-3-23 20:34 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
好东西来的,有空试试看。
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-11-21 20:13 , Processed in 0.052095 second(s), 9 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表