评论

C#获取摄像头拍照显示图像

原标题:C#获取摄像头拍照显示图像

概述

之前有个需求,就是在web界面可以实现调用摄像头,用户把手机的个人二维码展示给摄像头,摄像头进行摄像识别用户。

其实本质就是保存图像二维码,在进行二维码识别。

下面来看看如何实现。

主要代码实现

1、初始化摄像头

///<summary> ///初始化摄像头 ///</summary> ///<param name="handle">控件的句柄</param> ///<param name="left">开始显示的左边距</param> ///<param name="top">开始显示的上边距</param> ///<param name="width">要显示的宽度</param> ///<param name="height">要显示的长度</param> publicPick(IntPtr handle, intleft, inttop, intwidth, intheight) { mControlPtr = handle; mWidth = width; mHeight = height; mLeft = left; mTop = top; } [DllImport("avicap32.dll")] privatestaticexternIntPtr capCreateCaptureWindowA(byte[] lpszWindowName, intdwStyle, intx, inty, intnWidth, intnHeight, IntPtr hWndParent, intnID);

[DllImport("avicap32.dll")]privatestaticexternintcapGetVideoFormat(IntPtr hWnd, IntPtr psVideoFormat, intwSize);[DllImport("User32.dll")]privatestaticexternboolSendMessage(IntPtr hWnd, 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>///<param name="path">要保存bmp文件的路径</param>publicvoidGrabImage(stringpath){

IntPtr hBmp = Marshal.StringToHGlobalAnsi(path);SendMessage(hWndC, WM_CAP_SAVEDIB, 0, hBmp.ToInt64);

}

///<summary>///录像///</summary>///<param name="path">要保存avi文件的路径</param>publicvoidKinescope(stringpath){IntPtr hBmp = 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;usingcom.google.zxing.common;

usingSystem.Threading;

publicpartial classDecode:System.Web.UI.Page{// public delegate void SaveImg(Pick Pick1);/// <summary>/// 一个控制摄像头的类/// </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;privateIntPtr hWndC;privateboolbStat = false;privateIntPtr mControlPtr;privateintmWidth;privateintmHeight;privateintmLeft;privateintmTop;

/// <summary>/// 初始化摄像头/// </summary>/// <param name="handle">控件的句柄</param>/// <param name="left">开始显示的左边距</param>/// <param name="top">开始显示的上边距</param>/// <param name="width">要显示的宽度</param>/// <param name="height">要显示的长度</param>publicPick(IntPtr handle, intleft, inttop, intwidth, intheight){mControlPtr = handle;mWidth = width;mHeight = height;mLeft = left;mTop = top;}[DllImport("avicap32.dll")]privatestaticexternIntPtr capCreateCaptureWindowA(byte[] lpszWindowName, intdwStyle, intx, inty, intnWidth, intnHeight, IntPtr hWndParent, intnID);

[DllImport("avicap32.dll")]privatestaticexternintcapGetVideoFormat(IntPtr hWnd, IntPtr psVideoFormat, intwSize);[DllImport("User32.dll")]privatestaticexternboolSendMessage(IntPtr hWnd, 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>/// <param name="path">要保存bmp文件的路径</param>publicvoidGrabImage(stringpath){

IntPtr hBmp = Marshal.StringToHGlobalAnsi(path);SendMessage(hWndC, WM_CAP_SAVEDIB, 0, hBmp.ToInt64);

}

/// <summary>/// 录像/// </summary>/// <param name="path">要保存avi文件的路径</param>publicvoidKinescope(stringpath){IntPtr hBmp = 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(object sender, EventArgs e){}//void DoInit//{// System.Windows.Forms.Form frm = new Form;// frm.Height = 300;// frm.Width = 300;// System.Windows.Forms.PictureBox Panel = new System.Windows.Forms.PictureBox;// Panel.Height = 300;// Panel.Width = 300;// Panel.Visible = true;// Panel.BackgroundImageLayout = ImageLayout.None;// frm.Controls.Add(Panel);// frm.TopMost = true;// Pick p = new Pick(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{//ThreadStart worker = new ThreadStart(DoInit);//Thread th = new Thread(worker);//th.IsBackground = true;//th.Start;System.Windows.Forms.Form frm = newForm;frm.Height = 300;frm.Width = 300;System.Windows.Forms.PictureBox Panel = newSystem.Windows.Forms.PictureBox;Panel.Height = 300;Panel.Width = 300;Panel.Visible = true;Panel.BackgroundImageLayout = ImageLayout.None;frm.Controls.Add(Panel);frm.TopMost = true;Pick p = 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.QRCodeReader d = newcom.google.zxing.qrcode.QRCodeReader;RGBLuminanceSource rg = newRGBLuminanceSource(newSystem.Drawing.Bitmap(Server.MapPath("img\\Decode.bmp")), newSystem.Drawing.Bitmap(Server.MapPath("img\\Decode.bmp")).Width, newSystem.Drawing.Bitmap(Server.MapPath("img\\Decode.bmp")).Height);com.google.zxing.LuminanceSource ls = rg;HybridBinarizer hb = newHybridBinarizer(ls);com.google.zxing.BinaryBitmap bm = newcom.google.zxing.BinaryBitmap(hb);com.google.zxing.Result r = d.decode(bm);TextBox1.Text = r.Text;

}catch(Exception ex){

TextBox1.Text = "";//MessageBox.Show(ex.Message+"111");

thrownewException(ex.Message);}

}catch(Exception ee){ee.ToString;}}protectedvoidTimer1_Tick(object sender, EventArgs e){//getQrcode;}protectedvoidButton1_Click(object sender, EventArgs e){getQrcode;}

}返回搜狐,查看更多

责任编辑:

平台声明:该文观点仅代表作者本人,搜狐号系信息发布平台,搜狐仅提供信息存储空间服务。
阅读 ()
大家都在看
推荐阅读