




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
第C#獲取攝像頭拍照顯示圖像的方法本文實(shí)例為大家分享了C#獲取攝像頭拍照顯示圖像的具體代碼,供大家參考,具體內(nèi)容如下
之前有個(gè)需求,就是在web界面可以實(shí)現(xiàn)調(diào)用攝像頭,用戶把手機(jī)的個(gè)人二維碼展示給攝像頭,攝像頭進(jìn)行攝像識(shí)別用戶。
其實(shí)本質(zhì)就是保存圖像二維碼,在進(jìn)行二維碼識(shí)別。
下面來看看如何實(shí)現(xiàn)。
主要代碼實(shí)現(xiàn)
1、初始化攝像頭
///summary
///初始化攝像頭
////summary
///paramname="handle"控件的句柄/param
///paramname="left"開始顯示的左邊距/param
///paramname="top"開始顯示的上邊距/param
///paramname="width"要顯示的寬度/param
///paramname="height"要顯示的長(zhǎng)度/param
publicPick(IntPtrhandle,intleft,inttop,intwidth,intheight)
{
mControlPtr=handle;
mWidth=width;
mHeight=height;
mLeft=left;
mTop=top;
}
[DllImport("avicap32.dll")]
privatestaticexternIntPtrcapCreateCaptureWindowA(byte[]lpszWindowName,intdwStyle,intx,inty,intnWidth,intnHeight,IntPtrhWndParent,intnID);
[DllImport("avicap32.dll")]
privatestaticexternintcapGetVideoFormat(IntPtrhWnd,IntPtrpsVideoFormat,intwSize);
[DllImport("User32.dll")]
privatestaticexternboolSendMessage(IntPtrhWnd,intwMsg,intwParam,longlParam);
2、開始顯示圖像
///summary
///開始顯示圖像
////summary
publicvoidStart()
{
if(bStat)
return;
bStat=true;
byte[]lpszName=newbyte[100];
hWndC=capCreateCaptureWindowA(lpszName,WS_CHILD|WS_VISIBLE,mLeft,mTop,mWidth,mHeight,mControlPtr,0);
if(hWndC.ToInt32()!=0)
{
SendMessage(hWndC,WM_CAP_SET_CALLBACK_VIDEOSTREAM,0,0);
SendMessage(hWndC,WM_CAP_SET_CALLBACK_ERROR,0,0);
SendMessage(hWndC,WM_CAP_SET_CALLBACK_STATUSA,0,0);
SendMessage(hWndC,WM_CAP_DRIVER_CONNECT,0,0);
SendMessage(hWndC,WM_CAP_SET_SCALE,1,0);
SendMessage(hWndC,WM_CAP_SET_PREVIEWRATE,66,0);
SendMessage(hWndC,WM_CAP_SET_OVERLAY,1,0);
SendMessage(hWndC,WM_CAP_SET_PREVIEW,1,0);
}
return;
}
3、停止顯示
///summary
///停止顯示
////summary
publicvoidStop()
{
SendMessage(hWndC,WM_CAP_DRIVER_DISCONNECT,0,0);
bStat=false;
}
4、抓圖
///summary
///抓圖
////summary
///paramname="path"要保存bmp文件的路徑/param
publicvoidGrabImage(stringpath)
{
IntPtrhBmp=Marshal.StringToHGlobalAnsi(path);
SendMessage(hWndC,WM_CAP_SAVEDIB,0,hBmp.ToInt64());
}
///summary
///錄像
////summary
///paramname="path"要保存avi文件的路徑/param
publicvoidKinescope(stringpath)
{
IntPtrhBmp=Marshal.StringToHGlobalAnsi(path);
SendMessage(hWndC,WM_CAP_FILE_SET_CAPTURE_FILEA,0,hBmp.ToInt64());
SendMessage(hWndC,WM_CAP_SEQUENCE,0,0);
}
///summary
///停止錄像
////summary
publicvoidStopKinescope()
{
SendMessage(hWndC,WM_CAP_STOP,0,0);
}
usingSystem;
usingSystem.Collections;
usingSystem.Configuration;
usingSystem.Data;
usingSystem.Linq;
usingSystem.Web;
usingSystem.Web.Security;
usingSystem.Web.UI;
usingSystem.Web.UI.HtmlControls;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.WebControls.WebParts;
usingSystem.Xml.Linq;
usingSystem.Windows.Forms;
usingSystem.Runtime.InteropServices;
usingcom.google.zxing.qrcode.decoder;
usingcom.google.zxing.client;
usingmon;
usingSystem.Threading;
publicpartialclassDecode:System.Web.UI.Page
//publicdelegatevoidSaveImg(PickPick1);
///summary
///一個(gè)控制攝像頭的類
////summary
publicclassPick
{
privateconstintWM_USER=0x400;
privateconstintWS_CHILD=0x40000000;
privateconstintWS_VISIBLE=0x10000000;
privateconstintWM_CAP_START=WM_USER;
privateconstintWM_CAP_STOP=WM_CAP_START+68;
privateconstintWM_CAP_DRIVER_CONNECT=WM_CAP_START+10;
privateconstintWM_CAP_DRIVER_DISCONNECT=WM_CAP_START+11;
privateconstintWM_CAP_SAVEDIB=WM_CAP_START+25;
privateconstintWM_CAP_GRAB_FRAME=WM_CAP_START+60;
privateconstintWM_CAP_SEQUENCE=WM_CAP_START+62;
privateconstintWM_CAP_FILE_SET_CAPTURE_FILEA=WM_CAP_START+20;
privateconstintWM_CAP_SEQUENCE_NOFILE=WM_CAP_START+63;
privateconstintWM_CAP_SET_OVERLAY=WM_CAP_START+51;
privateconstintWM_CAP_SET_PREVIEW=WM_CAP_START+50;
privateconstintWM_CAP_SET_CALLBACK_VIDEOSTREAM=WM_CAP_START+6;
privateconstintWM_CAP_SET_CALLBACK_ERROR=WM_CAP_START+2;
privateconstintWM_CAP_SET_CALLBACK_STATUSA=WM_CAP_START+3;
privateconstintWM_CAP_SET_CALLBACK_FRAME=WM_CAP_START+5;
privateconstintWM_CAP_SET_SCALE=WM_CAP_START+53;
privateconstintWM_CAP_SET_PREVIEWRATE=WM_CAP_START+52;
privateIntPtrhWndC;
privateboolbStat=false;
privateIntPtrmControlPtr;
privateintmWidth;
privateintmHeight;
privateintmLeft;
privateintmTop;
///summary
///初始化攝像頭
////summary
///paramname="handle"控件的句柄/param
///paramname="left"開始顯示的左邊距/param
///paramname="top"開始顯示的上邊距/param
///paramname="width"要顯示的寬度/param
///paramname="height"要顯示的長(zhǎng)度/param
publicPick(IntPtrhandle,intleft,inttop,intwidth,intheight)
mControlPtr=handle;
mWidth=width;
mHeight=height;
mLeft=left;
mTop=top;
}
[DllImport("avicap32.dll")]
privatestaticexternIntPtrcapCreateCaptureWindowA(byte[]lpszWindowName,intdwStyle,intx,inty,intnWidth,intnHeight,IntPtrhWndParent,intnID);
[DllImport("avicap32.dll")]
privatestaticexternintcapGetVideoFormat(IntPtrhWnd,IntPtrpsVideoFormat,intwSize);
[DllImport("User32.dll")]
privatestaticexternboolSendMessage(IntPtrhWnd,intwMsg,intwParam,longlParam);
///summary
///開始顯示圖像
////summary
publicvoidStart()
if(bStat)
return;
bStat=true;
byte[]lpszName=newbyte[100];
hWndC=capCreateCaptureWindowA(lpszName,WS_CHILD|WS_VISIBLE,mLeft,mTop,mWidth,mHeight,mControlPtr,0);
if(hWndC.ToInt32()!=0)
{
SendMessage(hWndC,WM_CAP_SET_CALLBACK_VIDEOSTREAM,0,0);
SendMessage(hWndC,WM_CAP_SET_CALLBACK_ERROR,0,0);
SendMessage(hWndC,WM_CAP_SET_CALLBACK_STATUSA,0,0);
SendMessage(hWndC,WM_CAP_DRIVER_CONNECT,0,0);
SendMessage(hWndC,WM_CAP_SET_SCALE,1,0);
SendMessage(hWndC,WM_CAP_SET_PREVIEWRATE,66,0);
SendMessage(hWndC,WM_CAP_SET_OVERLAY,1,0);
SendMessage(hWndC,WM_CAP_SET_PREVIEW,1,0);
}
return;
}
///summary
///停止顯示
////summary
publicvoidStop()
SendMessage(hWndC,WM_CAP_DRIVER_DISCONNECT,0,0);
bStat=false;
}
///summary
///抓圖
////summary
///paramname="path"要保存bmp文件的路徑/param
publicvoidGrabImage(stringpath)
IntPtrhBmp=Marshal.StringToHGlobalAnsi(path);
SendMessage(hWndC,WM_CAP_SAVEDIB,0,hBmp.ToInt64());
}
///summary
///錄像
////summary
///paramname="path"要保存avi文件的路徑/param
publicvoidKinescope(stringpath)
IntPtrhBmp=Marshal.StringToHGlobalAnsi(path);
SendMessage(hWndC,WM_CAP_FILE_SET_CAPTURE_FILEA,0,hBmp.ToInt64());
SendMessage(hWndC,WM_CAP_SEQUENCE,0,0);
}
///summary
///停止錄像
////summary
publicvoidStopKinescope()
SendMessage(hWndC,WM_CAP_STOP,0,0);
}
}
protectedvoidPage_Load(objectsender,EventArgse)
}
//voidDoInit()
//{
//
System.Windows.Forms.Formfrm=newForm();
//
frm.Height=300;
//
frm.Width=300;
//
System.Windows.Forms.PictureBoxPanel=newSystem.Windows.Forms.PictureBox();
//
Panel.Height=300;
//
Panel.Width=300;
//
Panel.Visible=true;
//
Panel.BackgroundImageLayout=ImageLayout.None;
//
frm.Controls.Add(Panel);
//
frm.TopMost=true;
//
Pickp=newPick(Panel.Handle,0,0,300,300);
//
p.Start();
//
frm.Show();
//
p.Kinescope(Server.MapPath("img\\Decode2.avi"));
//
p.GrabImage(Server.MapPath("img\\Decode1.bmp"));
//
p.Stop();
//
frm.Close();
//
frm.Dispose();
//}
privatevoidgetQrcode()
try
{
//ThreadStartworker=newThreadStart(DoInit);
//Threadth=newThread(worker);
//th.IsBackground=true;
//th.Start();
System.Windows.Forms.Formfrm=newForm();
frm.Height=300;
frm.Width=300;
System.Windows.Forms.PictureBoxPanel=newSystem.Windows.Forms.PictureBox();
Panel.Height=300;
Panel.Width=300;
Panel.Visible=true;
Panel.BackgroundImageLayout=ImageLayout.None;
frm.Controls.Add(Panel);
frm.TopMost=true;
Pickp=newPick(Panel.Handle,0,0,300,300);
p.Start();
inti=1;
while(i=1)
{
p.GrabImage(Server.MapPath("img\\Decode.bmp"));
p.Kinescope(Server.MapPath("img\\Video.avi"));
i++;
}
p.Stop();
frm.Close();
frm.Dispose();
try
{
com.google.zxing.qrcode.QRCodeReaderd=newcom
溫馨提示
- 1. 本站所有資源如無(wú)特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 短視頻平臺(tái)賬號(hào)代運(yùn)營(yíng)與數(shù)據(jù)分析協(xié)議
- 智能家居設(shè)施配套房產(chǎn)銷售合同
- 虛擬現(xiàn)實(shí)游戲角色動(dòng)畫特效制作服務(wù)協(xié)議
- 跨國(guó)經(jīng)銷商品牌代理權(quán)合作框架協(xié)議
- 私人直升機(jī)航拍體育賽事影像作品版權(quán)分成及授權(quán)協(xié)議
- 拼多多平臺(tái)店鋪客服團(tuán)隊(duì)構(gòu)建與運(yùn)營(yíng)協(xié)議
- 法拍房稅費(fèi)繳納責(zé)任劃分及支付合同
- 國(guó)際田徑運(yùn)動(dòng)會(huì)票務(wù)總代理服務(wù)補(bǔ)充協(xié)議
- 電力項(xiàng)目風(fēng)險(xiǎn)評(píng)估補(bǔ)充協(xié)議
- 中班綜合活動(dòng):小兔分蘿卜
- NPT5空氣壓縮機(jī)檢修
- 分部工程質(zhì)量驗(yàn)收記錄
- Q∕SY 13123-2017 物資倉(cāng)儲(chǔ)技術(shù)規(guī)范
- 合肥市不動(dòng)產(chǎn)登記申請(qǐng)審批表-版本
- 防洪度汛檢查表
- 手術(shù)通知單模板
- 招商合同范本4篇-合同范本
- 《西方音樂史》課件伯遼茲
- 關(guān)于互聯(lián)網(wǎng)金融對(duì)商業(yè)銀行風(fēng)險(xiǎn)影響的實(shí)證研究會(huì)計(jì)學(xué)專業(yè)
- 十八項(xiàng)電網(wǎng)重大反事故措施
- 液壓缸常見故障類型及維修或排除方法
評(píng)論
0/150
提交評(píng)論