zoukankan      html  css  js  c++  java
  • C#摄像头实现拍照功能的简单代码示例

    View Code
    1 using System.Drawing;
    2 using System.Drawing.Imaging;
    3 using System;
    4 using System.Runtime.InteropServices;
    View Code
     1 private const int WM_USER = 0x400;
    2 private const int WS_CHILD = 0x40000000;
    3 private const int WS_VISIBLE = 0x10000000;
    4 private const int WM_CAP_START = WM_USER;
    5 private const int WM_CAP_STOP = WM_CAP_START + 68;
    6 private const int WM_CAP_DRIVER_CONNECT = WM_CAP_START + 10;
    7 private const int WM_CAP_DRIVER_DISCONNECT = WM_CAP_START + 11;
    8 private const int WM_CAP_SAVEDIB = WM_CAP_START + 25;
    9 private const int WM_CAP_GRAB_FRAME = WM_CAP_START + 60;
    10 private const int WM_CAP_SEQUENCE = WM_CAP_START + 62;
    11 private const int WM_CAP_FILE_SET_CAPTURE_FILEA = WM_CAP_START + 20;
    12 private const int WM_CAP_SEQUENCE_NOFILE = WM_CAP_START + 63;
    13 private const int WM_CAP_SET_OVERLAY = WM_CAP_START + 51;
    14 private const int WM_CAP_SET_PREVIEW = WM_CAP_START + 50;
    15 private const int WM_CAP_SET_CALLBACK_VIDEOSTREAM = WM_CAP_START + 6;
    16 private const int WM_CAP_SET_CALLBACK_ERROR = WM_CAP_START + 2;
    17 private const int WM_CAP_SET_CALLBACK_STATUSA = WM_CAP_START + 3;
    18 private const int WM_CAP_SET_CALLBACK_FRAME = WM_CAP_START + 5;
    19 private const int WM_CAP_SET_SCALE = WM_CAP_START + 53;
    20 private const int WM_CAP_SET_PREVIEWRATE = WM_CAP_START + 52;
    21 private IntPtr hWndC;
    22 private bool bStat = false;
    23 private IntPtr mControlPtr;
    24 private int mWidth;
    25 private int mHeight;
    26 private int mLeft;
    27 private int mTop;
    View Code
     1 /// <summary>
    2 /// 初始化摄像头
    3 /// </summary>
    4 /// <param name="handle">控件的句柄</param>
    5 /// <param name="left">开始显示的左边距</param>
    6 /// <param name="top">开始显示的上边距</param>
    7 /// <param name="width">要显示的宽度</param>
    8 /// <param name="height">要显示的长度</param>
    9 public VideoClass(IntPtr handle, int left, int top, int width, int height)
    10 {
    11 mControlPtr = handle;
    12 mWidth = width;
    13 mHeight = height;
    14 mLeft = left;
    15 mTop = top;
    16 }
    View Code
     1 [DllImport("avicap32.dll")]
    2 private static extern IntPtr capCreateCaptureWindowA(byte[] lpszWindowName, int dwStyle, int x, int y, int nWidth, int nHeight, IntPtr hWndParent, int nID);
    3
    4 [DllImport("avicap32.dll")]
    5 private static extern int capGetVideoFormat(IntPtr hWnd, IntPtr psVideoFormat, int wSize);
    6
    7 [DllImport("User32.dll")]
    8 private static extern bool SendMessage(IntPtr hWnd, int wMsg, int wParam, long lParam);
    9
    10 [DllImport("avicap32.dll")]
    11 public static extern bool capGetDriverDescriptionA(short wDriver, byte[] lpszName, int cbName, byte[] lpszVer, int cbVer);
    View Code
     1  /// <summary>
    2 /// 开始显示图像
    3 /// </summary>
    4 public void Start()
    5 {
    6 if (bStat)
    7 return;
    8 bStat = true;
    9 byte[] lpszName = new byte[100];
    10
    11 hWndC = capCreateCaptureWindowA(lpszName, WS_CHILD | WS_VISIBLE, mLeft, mTop, mWidth, mHeight, mControlPtr, 0);
    12
    13 if (hWndC.ToInt32() != 0)
    14 {
    15 SendMessage(hWndC, WM_CAP_SET_CALLBACK_VIDEOSTREAM, 0, 0);
    16 SendMessage(hWndC, WM_CAP_SET_CALLBACK_ERROR, 0, 0);
    17 SendMessage(hWndC, WM_CAP_SET_CALLBACK_STATUSA, 0, 0);
    18 SendMessage(hWndC, WM_CAP_DRIVER_CONNECT, 0, 0);
    19 SendMessage(hWndC, WM_CAP_SET_SCALE, 1, 0);
    20 SendMessage(hWndC, WM_CAP_SET_PREVIEWRATE, 66, 0);
    21 SendMessage(hWndC, WM_CAP_SET_OVERLAY, 1, 0);
    22 SendMessage(hWndC, WM_CAP_SET_PREVIEW, 1, 0);
    23 }
    24 return;
    25 }
    View Code
     1  /// <summary>
    2 /// 停止显示
    3 /// </summary>
    4 public void Stop()
    5 {
    6 SendMessage(hWndC, WM_CAP_DRIVER_DISCONNECT, 0, 0);
    7 bStat = false;
    8 }
    9 /// <summary>
    10 /// 抓图
    11 /// </summary>
    12 /// <param name="path">要保存bmp文件的路径</param>
    13 public void GrabImage(string path)
    14 {
    15 IntPtr hBmp = Marshal.StringToHGlobalAnsi(path);
    16 SendMessage(hWndC, WM_CAP_SAVEDIB, 0, hBmp.ToInt64());
    17 }
    18 /// <summary>
    19 /// 录像
    20 /// </summary>
    21 /// <param name="path">要保存avi文件的路径</param>
    22 public void Kinescope(string path)
    23 {
    24 IntPtr hBmp = Marshal.StringToHGlobalAnsi(path);
    25 SendMessage(hWndC, WM_CAP_FILE_SET_CAPTURE_FILEA, 0, hBmp.ToInt64());
    26 SendMessage(hWndC, WM_CAP_SEQUENCE, 0, 0);
    27 }
    28 /// <summary>
    29 /// 停止录像
    30 /// </summary>
    31 public void StopKinescope()
    32 {
    33 SendMessage(hWndC, WM_CAP_STOP, 0, 0);
    34 }







  • 相关阅读:
    halcon 如何把一个region截取出来保存为图像
    Halcon学习(三)赋值与数组操作
    sort_region——对区域进行排序
    Halcon函数【转】
    Halcon算子之shape_trans,用于变换区域的形状
    Halcon学习之八:图像区域叠加与绘制
    Halcon学习之七:改变图像的现实方式和大小
    Halcon学习之六:获取Image图像中Region区域的特征参数
    Halcon学习之五:有关图像的定义域的函数
    Docker Swarm redis 集群搭建
  • 原文地址:https://www.cnblogs.com/siqing99/p/2431018.html
Copyright © 2011-2022 走看看