Delphi利用avicap32.dll編程控制攝像頭實現拍照、錄制視頻
項目需求:平板電腦(Windows系統)一維/二維碼掃描功能;
需求分析:
需要掃描一維/二維碼時,分兩步實現。
第一步,avicap32.dll或者dspack技術實現靜默打開攝像頭拍照、保存BMP圖片。
第二步,ZXing技術實現對一維/二維碼的解析,最終獲取到碼值。
本篇是利用avicap32.dll技術實現拍照/錄制視頻的測試Demo。
請看代碼:
1 unit uMain; 2 3 interface 4 5 uses 6 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 7 Dialogs, ExtCtrls, StdCtrls, RzPanel, RzButton; 8 9 type 10 TFrmMain = class(TForm) 11 gbScreen: TRzGroupBox; 12 gbOperation: TRzGroupBox; 13 btnStart: TRzBitBtn; 14 btnStop: TRzBitBtn; 15 btnSaveBMP: TRzBitBtn; 16 btnStartR: TRzBitBtn; 17 btnSaveAVI: TRzBitBtn; 18 pMain: TPanel; 19 procedure btnStartClick(Sender: TObject); 20 procedure FormClose(Sender: TObject; var Action: TCloseAction); 21 procedure btnStopClick(Sender: TObject); 22 procedure btnSaveBMPClick(Sender: TObject); 23 procedure btnStartRClick(Sender: TObject); 24 procedure btnSaveAVIClick(Sender: TObject); 25 private 26 { Private declarations } 27 hWndC: THandle; 28 public 29 { Public declarations } 30 end; 31 32 var 33 FrmMain: TFrmMain; 34 const WM_CAP_START = WM_USER; 35 const WM_CAP_STOP = WM_CAP_START + 68; 36 const WM_CAP_DRIVER_CONNECT = WM_CAP_START + 10; 37 const WM_CAP_DRIVER_DISCONNECT = WM_CAP_START + 11; 38 const WM_CAP_SAVEDIB = WM_CAP_START + 25; 39 const WM_CAP_GRAB_FRAME = WM_CAP_START + 60; 40 const WM_CAP_SEQUENCE = WM_CAP_START + 62; 41 const WM_CAP_FILE_SET_CAPTURE_FILEA = WM_CAP_START + 20; 42 const WM_CAP_SEQUENCE_NOFILE = WM_CAP_START + 63; 43 const WM_CAP_SET_OVERLAY = WM_CAP_START + 51; 44 const WM_CAP_SET_PREVIEW = WM_CAP_START + 50; 45 const WM_CAP_SET_CALLBACK_VIDEOSTREAM = WM_CAP_START + 6; 46 const WM_CAP_SET_CALLBACK_ERROR = WM_CAP_START + 2; 47 const WM_CAP_SET_CALLBACK_STATUSA = WM_CAP_START + 3; 48 const WM_CAP_SET_CALLBACK_FRAME = WM_CAP_START + 5; 49 const WM_CAP_SET_SCALE = WM_CAP_START + 53; 50 const WM_CAP_SET_PREVIEWRATE = WM_CAP_START + 52; 51 52 function capCreateCaptureWindowA(lpszWindowName: PCHAR; 53 dwStyle: longint; x: integer; y: integer; nWidth: integer; 54 nHeight: integer; ParentWin: HWND; nId: integer): HWND; 55 STDCALL EXTERNAL 'AVICAP32.DLL'; 56 57 implementation 58 59 {$R *.dfm} 60 61 procedure TFrmMain.btnStartClick(Sender: TObject); 62 begin 63 //開始拍攝 64 hWndC := capCreateCaptureWindowA('My Own Capture Window', WS_CHILD or WS_VISIBLE, pMain.Left, pMain.Top, pMain.Width, pMain.Height, FrmMain.Handle, 0); 65 if hWndC <> 0 then 66 begin 67 SendMessage(hWndC, WM_CAP_SET_CALLBACK_VIDEOSTREAM, 0, 0); 68 SendMessage(hWndC, WM_CAP_SET_CALLBACK_ERROR, 0, 0); 69 SendMessage(hWndC, WM_CAP_SET_CALLBACK_STATUSA, 0, 0); 70 SendMessage(hWndC, WM_CAP_DRIVER_CONNECT, 0, 0); 71 SendMessage(hWndC, WM_CAP_SET_SCALE, 1, 0); 72 SendMessage(hWndC, WM_CAP_SET_PREVIEWRATE, 66, 0); 73 SendMessage(hWndC, WM_CAP_SET_OVERLAY, 1, 0); 74 SendMessage(hWndC, WM_CAP_SET_PREVIEW, 1, 0); 75 end; 76 end; 77 78 procedure TFrmMain.FormClose(Sender: TObject; var Action: TCloseAction); 79 begin 80 //程序退出時,自動關閉攝像頭 81 if hWndC <> 0 then 82 begin 83 SendMessage(hWndC, WM_CAP_DRIVER_DISCONNECT, 0, 0); 84 end; 85 end; 86 87 procedure TFrmMain.btnStopClick(Sender: TObject); 88 begin 89 //關閉攝像頭 90 if hWndC <> 0 then 91 begin 92 SendMessage(hWndC, WM_CAP_DRIVER_DISCONNECT, 0, 0); 93 hWndC := 0; 94 end; 95 end; 96 97 procedure TFrmMain.btnSaveBMPClick(Sender: TObject); 98 begin 99 //保存BMP 100 if hWndC <> 0 then 101 begin 102 SendMessage(hWndC, WM_CAP_SAVEDIB, 0, longint(pchar('D:/1.bmp'))); 103 end; 104 end; 105 106 procedure TFrmMain.btnStartRClick(Sender: TObject); 107 begin 108 //開始錄制AVI視頻 109 if hWndC <> 0 then 110 begin 111 SendMessage(hWndC, WM_CAP_FILE_SET_CAPTURE_FILEA, 0, Longint(pchar('c:/test.avi'))); 112 SendMessage(hWndC, WM_CAP_SEQUENCE, 0, 0); 113 end; 114 end; 115 116 procedure TFrmMain.btnSaveAVIClick(Sender: TObject); 117 begin 118 //停止錄制並保存AVI視頻 119 if hWndC <> 0 then 120 begin 121 SendMessage(hWndC, WM_CAP_STOP, 0, 0); 122 end; 123 end; 124 125 end.
運行效果:
作者:Jeremy.Wu
出處:https://www.cnblogs.com/jeremywucnblog/
本文版權歸作者和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責任的權利。