利用攝像頭拍照的ActiveX控件Cnet源代碼_第1頁
利用攝像頭拍照的ActiveX控件Cnet源代碼_第2頁
利用攝像頭拍照的ActiveX控件Cnet源代碼_第3頁
利用攝像頭拍照的ActiveX控件Cnet源代碼_第4頁
利用攝像頭拍照的ActiveX控件Cnet源代碼_第5頁
已閱讀5頁,還剩7頁未讀 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、功能:使用電腦攝像頭在線拍照控件(ActiveX控件)源代碼,并能將生成的照片轉(zhuǎn)換成Base64字符串以上傳到服務(wù)器。調(diào)試環(huán)境:Microsoft Visual Studio 2005設(shè)計者:渝快數(shù)碼 QQ:日期:2016年1月21日網(wǎng)頁中的調(diào)用方式:/以下是拍照代碼var obj=document.getElementById(takephoto);otxt=obj.make2();首先新建一個類庫,名為:MakePhoto然后修改以下文件:/ -/ 文件名: Properties/AssemblyInfo.cs/ -using System.Refle

2、ction;using System.Runtime.CompilerServices;using System.Runtime.InteropServices;using System.Security;/ 有關(guān)程序集的常規(guī)信息通過下列屬性集/ 控制。更改這些屬性值可修改/ 與程序集關(guān)聯(lián)的信息。assembly: AssemblyTitle(MakePhoto)assembly: AssemblyDescription()assembly: AssemblyConfiguration()assembly: AssemblyCompany()assembly: AssemblyProduct(

3、MakePhoto)assembly: AssemblyCopyright(版權(quán)所有 (C) 2015)assembly: AssemblyTrademark()assembly: AssemblyCulture()/ 將 ComVisible 設(shè)置為 false 使此程序集中的類型/ 對 COM 組件不可見。如果需要從 COM 訪問此程序集中的類型,/ 則將該類型上的 ComVisible 屬性設(shè)置為 true。assembly: ComVisible(true)/ 如果此項目向 COM 公開,則下列 GUID 用于類型庫的 IDassembly: Guid(86d26cf4-ae4e-45

4、99-bbf7-e0ec72ab1b76)/ 程序集的版本信息由下面四個值組成:/ 主版本/ 次版本 / 內(nèi)部版本號/ 修訂號/ 可以指定所有這些值,也可以使用“修訂號”和“內(nèi)部版本號”的默認值,/ 方法是按如下所示使用“*”:assembly: AssemblyVersion(1.0.*)assembly: AssemblyFileVersion()assembly: AllowPartiallyTrustedCallers()/-/文件:InterfaceCapture.cs/-using System;using System.Collections.Generic;usi

5、ng System.Text;using System.Runtime.InteropServices;namespace MakePhoto ComImport, GuidAttribute(CB5BDC81-93C1-11CF-8F20-00805F2CD064) InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown) interface InterfaceCapture PreserveSig int GetInterfaceSafetyOptions(ref Guid riid, MarshalAs(UnmanagedT

6、ype.U4) ref int pdwSupportedOptions, MarshalAs(UnmanagedType.U4) ref int pdwEnabledOptions); PreserveSig() int SetInterfaceSafetyOptions(ref Guid riid, MarshalAs(UnmanagedType.U4) int dwOptionSetMask, MarshalAs(UnmanagedType.U4) int dwEnabledOptions); / -/控件設(shè)計:ykCapture.Designer.cs/ -namespace MakeP

7、hoto partial class ykCapture / / 必需的設(shè)計器變量。 / private System.ComponentModel.IContainer components = null; / / 清理所有正在使用的資源。 / / 如果應(yīng)釋放托管資源,為 true;否則為 false。 protected override void Dispose(bool disposing) if (disposing & (components != null) components.Dispose(); base.Dispose(disposing); #region 組件設(shè)計器生

8、成的代碼 / / 設(shè)計器支持所需的方法 - 不要 / 使用代碼編輯器修改此方法的內(nèi)容。 / private void InitializeComponent() this.SuspendLayout(); / / ykCapture / this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.Name = ykCapture; this.Size = new System.Drawing.Siz

9、e(320, 240); this.Load += new System.EventHandler(this.ykCapture_Load); this.ResumeLayout(false); #endregion / -/控件主程序:ykCapture.cs/-using System;using System.Collections.Generic;using System.ComponentModel;using System.Drawing;using System.Data;using System.Text;using System.Windows.Forms;using Sys

10、tem.Runtime.InteropServices;using System.Drawing.Imaging;namespace MakePhoto Guid(52A0BE1A-1ABA-4876-B5FC-CF402EBB50A1) public partial class ykCapture : UserControl, InterfaceCapture Pick pick; public ykCapture() InitializeComponent(); private void ykCapture_Load(object sender, EventArgs e) public v

11、oid start() /啟動攝像頭 pick = new Pick(this.Handle, 0, 0, this.Width, this.Height); pick.Start(); public void stop() /停止攝像頭 pick.Stop(); public string make2() return pick.Cap(); public string make3() Bitmap bmp = new Bitmap(this.Width, this.Height); Graphics g = Graphics.FromImage(bmp); g.CopyFromScreen

12、(PointToScreen(new Point(this.Left, this.Top), new Point(0, 0), this.Size); System.IO.MemoryStream ms = new System.IO.MemoryStream(); bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); byte arr = new bytems.Length; ms.Position = 0; ms.Read(arr, 0, (int)ms.Length); ms.Close(); string strbase64 =

13、Convert.ToBase64String(arr); return strbase64; public string GetBase64String(System.Drawing.Image image) System.IO.MemoryStream ms = new System.IO.MemoryStream(); image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); byte buffer = ms.ToArray(); return Convert.ToBase64String(buffer); public Image

14、GetImageFromBase64String(string ImageText) if (ImageText.Length 0) Byte bitmapData = new ByteImageText.Length; /bitmapData = Convert.FromBase64String(FixBase64ForImage(ImageText); bitmapData = Convert.FromBase64String(ImageText); System.IO.MemoryStream streamBitmap = new System.IO.MemoryStream(bitma

15、pData); / Bitmap bitImage = new Bitmap(Bitmap)Image.FromStream(streamBitmap); return Image.FromStream(streamBitmap); else return null; public string make(string path) /拍照 pick.GrabImage(path); return path; #region IObjectSafety 成員 private const string _IID_IDispatch = -0000-0000-C000-6; private cons

16、t string _IID_IDispatchEx = a6ef9860-c720-11d0-9337-00a0c90dcaa9; private const string _IID_IPersistStorage = A-0000-0000-C000-6; private const string _IID_IPersistStream = -0000-0000-C000-6; private const string _IID_IPersistPropertyBag = 37D84F60-42CB-11CE-8135-00AA004BB851; private const int INTE

17、RFACESAFE_FOR_UNTRUSTED_CALLER = 0x; private const int INTERFACESAFE_FOR_UNTRUSTED_DATA = 0x; private const int S_OK = 0; private const int E_FAIL = unchecked(int)0x); private const int E_NOINTERFACE = unchecked(int)0x); private bool _fSafeForScripting = true; private bool _fSafeForInitializing = tr

18、ue; public int GetInterfaceSafetyOptions(ref Guid riid, ref int pdwSupportedOptions, ref int pdwEnabledOptions) int Rslt = E_FAIL; string strGUID = riid.ToString(B); pdwSupportedOptions = INTERFACESAFE_FOR_UNTRUSTED_CALLER | INTERFACESAFE_FOR_UNTRUSTED_DATA; switch (strGUID) case _IID_IDispatch: cas

19、e _IID_IDispatchEx: Rslt = S_OK; pdwEnabledOptions = 0; if (_fSafeForScripting = true) pdwEnabledOptions = INTERFACESAFE_FOR_UNTRUSTED_CALLER; break; case _IID_IPersistStorage: case _IID_IPersistStream: case _IID_IPersistPropertyBag: Rslt = S_OK; pdwEnabledOptions = 0; if (_fSafeForInitializing = tr

20、ue) pdwEnabledOptions = INTERFACESAFE_FOR_UNTRUSTED_DATA; break; default: Rslt = E_NOINTERFACE; break; return Rslt; public int SetInterfaceSafetyOptions(ref Guid riid, int dwOptionSetMask, int dwEnabledOptions) int Rslt = E_FAIL; string strGUID = riid.ToString(B); switch (strGUID) case _IID_IDispatc

21、h: case _IID_IDispatchEx: if (dwEnabledOptions & dwOptionSetMask) = INTERFACESAFE_FOR_UNTRUSTED_CALLER) & (_fSafeForScripting = true) Rslt = S_OK; break; case _IID_IPersistStorage: case _IID_IPersistStream: case _IID_IPersistPropertyBag: if (dwEnabledOptions & dwOptionSetMask) = INTERFACESAFE_FOR_UN

22、TRUSTED_DATA) & (_fSafeForInitializing = true) Rslt = S_OK; break; default: Rslt = E_NOINTERFACE; break; return Rslt; #endregion private void this_Click(object sender, EventArgs e) class Pick private const int WM_USER = 0x400; private const int WS_CHILD = 0x; private const int WS_VISIBLE = 0x; priva

23、te const int WM_CAP_START = WM_USER; private const int WM_CAP_STOP = WM_CAP_START + 68; private const int WM_CAP_DRIVER_CONNECT = WM_CAP_START + 10; private const int WM_CAP_DRIVER_DISCONNECT = WM_CAP_START + 11; private const int WM_CAP_SAVEDIB = WM_CAP_START + 25; private const int WM_CAP_GRAB_FRA

24、ME = WM_CAP_START + 60; private const int WM_CAP_SEQUENCE = WM_CAP_START + 62; private const int WM_CAP_FILE_SET_CAPTURE_FILEA = WM_CAP_START + 20; private const int WM_CAP_SEQUENCE_NOFILE = WM_CAP_START + 63; private const int WM_CAP_SET_OVERLAY = WM_CAP_START + 51; private const int WM_CAP_SET_PRE

25、VIEW = WM_CAP_START + 50; private const int WM_CAP_SET_CALLBACK_VIDEOSTREAM = WM_CAP_START + 6; private const int WM_CAP_SET_CALLBACK_ERROR = WM_CAP_START + 2; private const int WM_CAP_SET_CALLBACK_STATUSA = WM_CAP_START + 3; private const int WM_CAP_SET_CALLBACK_FRAME = WM_CAP_START + 5; private co

26、nst int WM_CAP_SET_SCALE = WM_CAP_START + 53; private const int WM_CAP_SET_PREVIEWRATE = WM_CAP_START + 52; private IntPtr hWndC; private bool bStat = false; private IntPtr mControlPtr; private int mWidth; private int mHeight; private int mLeft; private int mTop; / / 初始化攝像頭 / / 控件的句柄 / 開始顯示的左邊距 / 開始

27、顯示的上邊距 / 要顯示的寬度 / 要顯示的長度 public Pick(IntPtr handle, int left, int top, int width, int height) mControlPtr = handle; mWidth = width; mHeight = height; mLeft = left; mTop = top; DllImport(avicap32.dll) private static extern IntPtr capCreateCaptureWindowA(byte lpszWindowName, int dwStyle, int x, int y,

28、 int nWidth, int nHeight, IntPtr hWndParent, int nID); DllImport(avicap32.dll) private static extern int capGetVideoFormat(IntPtr hWnd, IntPtr psVideoFormat, int wSize); DllImport(User32.dll) private static extern bool SendMessage(IntPtr hWnd, int wMsg, int wParam, int lParam); / / 開始顯示圖像 / public v

29、oid Start() if (bStat) return; bStat = true; byte lpszName = new byte100; 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

30、_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; / / 停止顯示 / public void Stop() SendMessage(hWndC, WM_CAP_DRIVER_DISCONNECT, 0, 0); bStat = false; / / 抓圖 / / 要保存bmp文件的路徑 public

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論