123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Drawing;
- using System.Data;
- using System.Text;
- using System.Windows.Forms;
- using System.Runtime.InteropServices;
- using System.Drawing.Imaging;
- namespace CameraDemo
- {
- /// <summary>
- /// 申明委托
- /// </summary>
- /// <param name="e"></param>
- /// <returns></returns>
- public delegate void EventPhotographedHandler(EventPhotographed e);
- public partial class Camera : UserControl
- {
- private int hHwnd;
- public string fileName = "*";//照片的文件名,一般为考生的流水号
- public int width = 310*4;//照片的尺寸为宽度:240,高度:320,即:240*320
- public int height = 275*4;
- private bool isOpen = false;
- /// <summary>
- /// 必需的设计器变量。
- /// </summary>
- [DllImport("avicap32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
- public static extern int capCreateCaptureWindowA([MarshalAs(UnmanagedType.VBByRefStr)] ref string lpszWindowName, int dwStyle, int x, int y, int nWidth, short nHeight, int hWndParent, int nID);
- [DllImport("user32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
- public static extern bool DestroyWindow(int hndw);
- [DllImport("user32", EntryPoint = "SendMessageA", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
- public static extern int SendMessage(int hwnd, int wMsg, int wParam, [MarshalAs(UnmanagedType.AsAny)] object lParam);
- [DllImport("user32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
- public static extern int SetWindowPos(int hwnd, int hWndInsertAfter, int x, int y, int cx, int cy, int wFlags);
- public Camera()
- {
- InitializeComponent();
- if (fileName == "*")
- {
- fileName = DateTime.Now.ToString("yyMMddHHmmssfff");
- }
- Clipboard.Clear();
- }
- /// <summary>
- /// 拍照事件
- /// </summary>
- public event EventPhotographedHandler EventPhotographeded;
- int OpenCount = 0;
- private void OpenCapture()
- {
- string msg="未找到可用摄像头,请检查设备是否正常后重试!";
- try
- {
- int intWidth = this.videoWindow.Width;
- int intHeight = this.videoWindow.Height;
- int intDevice = 0;
- string refDevice = intDevice.ToString();
- hHwnd = Camera.capCreateCaptureWindowA(ref refDevice, 1342177280, 0, 0, 640, 480, this.videoWindow.Handle.ToInt32(), 0);
- int cameraCount = Camera.SendMessage(this.hHwnd, 0x40a, intDevice, 0);
- if (cameraCount > 0)
- {
- try
- {
- Camera.SendMessage(this.hHwnd, 0x435, -1, 0);
- Camera.SendMessage(this.hHwnd, 0x434, 0x42, 0);
- Camera.SendMessage(this.hHwnd, 0x432, -1, 0);
- Camera.SetWindowPos(this.hHwnd, 1, 0, 0, intWidth, intHeight, 6);
- isOpen = true;
- }
- catch { LYFZ.MessageBoxCustom.Show(msg); return; }
-
- }
- else
- {
- Camera.DestroyWindow(this.hHwnd);
- }
- if (!isOpen)
- {
- System.Threading.Thread.Sleep(10);
- OpenCount++;
- if (OpenCount < 30)
- {
- OpenCapture();
- }
- else {
- LYFZ.MessageBoxCustom.Show(msg); return;
- }
-
- }
- }
- catch {
- LYFZ.MessageBoxCustom.Show(msg); return;
- }
- }
- /// <summary>
- /// 打开摄像头
- /// </summary>
- /// <returns></returns>
- public bool OpenCamera()
- {
- if (!isOpen)
- this.OpenCapture();
- return isOpen;
- }
- private void btn_OpenCapture_Click(object sender, EventArgs e)//打开摄像头,显示视频
- {
- if (!isOpen)
- this.OpenCapture();
- }
- bool isSaveImageFile = true;
- /// <summary>
- /// 是否直接保存Image文件
- /// </summary>
- public bool IsSaveImageFile
- {
- get { return isSaveImageFile; }
- set { isSaveImageFile = value; }
- }
-
- private void btn_GetCapture_Click(object sender, EventArgs e)//抓取图像
- {
- try
- {
- Camera.SendMessage(this.hHwnd, 0x41e, 0, 0);
- IDataObject obj_camera = Clipboard.GetDataObject();
- if (obj_camera.GetDataPresent(typeof(Bitmap)))
- {
- Image image_camera = ((Image)obj_camera.GetData(typeof(Bitmap))).GetThumbnailImage(width, height, null, IntPtr.Zero);
- //设置图片的尺寸为240*320
- if (IsSaveImageFile)
- {
- SaveFileDialog SaveFileDialog_camera = new SaveFileDialog();
- SaveFileDialog_camera.FileName = fileName + ".jpg";
- SaveFileDialog_camera.Filter = "Image Files(*.JPG)|*.JPG";
- if (SaveFileDialog_camera.ShowDialog() == DialogResult.OK)
- {
- image_camera.Save(SaveFileDialog_camera.FileName, ImageFormat.Jpeg);
- }
- }
- else {
- EventPhotographed eventPhtoto = new EventPhotographed(image_camera);
- eventPhtoto.IsPhotographedSuccess = true;
- this.EventPhotographeded(eventPhtoto);
-
- }
- }
- else
- {
- MessageBox.Show("摄像头没有开启,不能抓图,请开启摄像头!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
- }
- }
- catch
- {
- MessageBox.Show("抓取图像时出现错误!", "错误信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- private void btn_CloseCapture_Click(object sender, EventArgs e)//关闭摄像头
- {
- Camera.SendMessage(this.hHwnd, 0x40b, 0, 0);
- Camera.DestroyWindow(this.hHwnd);
- Clipboard.Clear();//清除剪切板中的内容
- isOpen = false;
- }
- private void btn_set_Click(object sender, EventArgs e)//对摄像头进行设置
- {
- if (isOpen)
- Camera.SendMessage(this.hHwnd, 0x42a, 0, 0);
- else
- MessageBox.Show("摄像头没有开启,不能设置,请开启摄像头!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
- }
- private void btn_kinescope_Click(object sender, EventArgs e)//开始录像,录像过程中鼠标成漏斗形,对着图像显示区域单击鼠标左键或右键就结束了录像。
- {
- if (isOpen)
- {
- SaveFileDialog SaveFileDialog_Video = new SaveFileDialog();
- SaveFileDialog_Video.FileName = fileName + ".avi";
- SaveFileDialog_Video.Filter = "Video Files(*.avi)|*.avi";
- if (SaveFileDialog_Video.ShowDialog() == DialogResult.OK)
- {
- SendMessage(this.hHwnd, 0x414, 0, SaveFileDialog_Video.FileName);
- SendMessage(this.hHwnd, 0x43e, 0, 0);
- }
- }
- else
- MessageBox.Show("摄像头没有开启,不能录像,请开启摄像头!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
- }
- private void btn_stopKinescope_Click(object sender, EventArgs e)//停止录像
- {
- SendMessage(this.hHwnd, 0x444, 0, 0);
- }
- }
- /// <summary>
- /// 拍照事件对象
- /// </summary>
- public class EventPhotographed : EventArgs
- {
- private Image _image;
- /// <summary>
- /// 输出图像
- /// </summary>
- public Image OutputImage
- {
- get { return _image; }
- set { _image = value; }
- }
- public EventPhotographed(Image image)
- {
- this._image = image;
- }
- bool isPhotographedSuccess = false;
- /// <summary>
- /// 是否拍照成功
- /// </summary>
- public bool IsPhotographedSuccess
- {
- get { return isPhotographedSuccess; }
- set { isPhotographedSuccess = value; }
- }
- }
- }
|