调用avicap32.dll第一次成功,以后都无法调用

requiem01119 requiem01119 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
调用avicap32.dll第一次成功,以后都无法调用,picturebox里面剩下的都是黑色的,摄像头灯也没有亮,调用不成功。 还有一个问题是我的摄像头不能定位到中心位置。求大神帮忙。

下面是源码

这个是form1的文件

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Threading;


namespace AutoCam
{
public partial class form1 : Form
{

public form1()
{
InitializeComponent();
}
Camera camera;

#region InitializeComponent

#region
btn_openCamera...
#endregion

private void button1_Click(object sender, EventArgs e)
{
this.btn_openCamera.Enabled = false;
this.btn_closeCam.Enabled = true;
this.btn_cameraPic.Enabled = true;
camera = new Camera(this.pic_camera.Handle, this.pic_camera.Width, this.pic_camera.Height);
camera.StartWebCam();
}
#endregion

#region btn_closeCam_Click
private void btn_closeCam_Click(object sender, EventArgs e)
{
this.btn_openCamera.Enabled = true;
this.btn_closeCam.Enabled = false;
this.btn_cameraPic.Enabled = false;
camera.CloseWebcam();
}
#endregion

#region btn_cameraPic_Click
private void btn_cameraPic_Click(object sender, EventArgs e)
{
camera.GrabImage(this.pic_camera.Handle, "guying.bmp");
}
#endregion

private void quit_Click(object sender, EventArgs e)
{
this.Close();
}
}
}


这个是cameraAPI.cs的文件

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Threading;

namespace AutoCam
{
class CameraAPI
{
[DllImport("avicap32.dll")]
public static extern IntPtr capCreateCaptureWindowA(byte[] lpszWindowName, int dwStyle, int x, int y, int nWidth, int nHeight, IntPtr hWndParent, int nID);
[DllImport("avicap32.dll")]
public static extern bool capGetDriverDescriptionA(short wDriver, byte[] lpszName, int cbName, byte[] lpszVer, int cbVer);
[DllImport("User32.dll")]
public static extern bool SendMessage(IntPtr hWnd, int wMsg, bool wParam, int lParam);
[DllImport("User32.dll")]
public static extern bool SendMessage(IntPtr hWnd, int wMsg, short wParam, int lParam);

public const int WM_USER = 0x400;
public const int WS_CHILD = 0x40000000;
public const int WS_VISIBLE = 0x10000000;
public const int SWP_NOMOVE = 0x2;
public const int SWP_NOZORDER = 0x4;
public const int WM_CAP_DRIVER_CONNECT = WM_USER + 10;
public const int WM_CAP_DRIVER_DISCONNECT = WM_USER +11;
public const int WM_CAP_SET_CALLBACK_FRAME = WM_USER +5;
public const int WM_CAP_SET_PREVIEW = WM_USER+50;
public const int WM_CAP_SET_PREVIEWFORMAT = WM_USER + 45;
public const int WM_CAP_SET_PREVIEWRATE = WM_USER + 52;
public const int WM_CAP_START = WM_USER;
public const int WM_CAP_SAVEDIB = WM_CAP_START + 25;
}
}


这个是camera.cs的class用来生成cameraAPI实例

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Runtime.InteropServices;
using System.IO;
using System.Windows;

namespace AutoCam
{

class Camera
{
private IntPtr lwndC;
private IntPtr mControlPtr;
private int mWidth;
private int mHeight;

public Camera(IntPtr handel, int width, int height)
{
mControlPtr = handel; //handle of video dom
mWidth = width; //video width
mHeight = height; //video height
}

public void StartWebCam()
{
byte[] lpszName = new byte[100];
byte[] lpszVer = new byte[100];
CameraAPI.capGetDriverDescriptionA(0, lpszName,100, lpszVer, 0);
this.lwndC = CameraAPI.capCreateCaptureWindowA(lpszName, CameraAPI.WS_CHILD | CameraAPI.WS_VISIBLE, 0, 0, mWidth, mHeight, mControlPtr, 0);
if (CameraAPI.SendMessage(lwndC, CameraAPI.WM_CAP_DRIVER_CONNECT, 0, 0))
{
CameraAPI.SendMessage(lwndC, CameraAPI.WM_CAP_SET_PREVIEWRATE, 100, 0);
CameraAPI.SendMessage(lwndC, CameraAPI.WM_CAP_SET_PREVIEW, true, 0);
}
}

public void CloseWebcam()
{
CameraAPI.SendMessage(lwndC, CameraAPI.WM_CAP_DRIVER_DISCONNECT, 0, 0);
}

public void GrabImage(IntPtr hWndC, string path)
{
IntPtr hBmp = Marshal.StringToHGlobalAnsi(path);
CameraAPI.SendMessage(lwndC, CameraAPI.WM_CAP_SAVEDIB, 0, hBmp.ToInt32());
}
}
}
...全文

相关推荐

VB.NET调用avicap32.dll实现照相功能

VB.NET要实现照相功能,有两种方法:一个是调用windows的API函数avicap32.dll实现照相功能,另一个就是采用第三方程序实现照相功能。

gszfxyfxd

Delphi中avicap32.dll的使用指南

avicap32.dll是Windows操作系统中用于视频捕获和处理的动态链接库(DLL),它允许程序员通过应用程序编程接口(API)访问计算机上的视频输入设备,如摄像头和视频卡。开发者使用avicap32.dll可以较为方便地将视频捕获功能集成到自己的软件产品中,为用户提供实时视频流的处理和显示。为了确保avicap32.dll在视频捕获中的应用顺利,程序员需要了解其详细的函数声明和使用方法。这通常涉及调用一系列的函数,如用于创建视频捕获窗口,获取视频驱动程序信息等。

李姝瑶

avicap32.dll动态链接库详解与应用实战

Windows操作系统自诞生以来,始终致力于为开发者提供一套强大而灵活的多媒体支持体系。在图形、音频和视频处理领域,Windows通过一系列层次分明、职责清晰的API架构实现了对底层硬件资源的抽象化管理,使得应用程序能够在不同设备与驱动之间实现无缝兼容。其中,早期以Video for Windows(VfW)为代表的多媒体框架奠定了视频捕获技术的基础,而后续演进至DirectShow、Media Foundation等现代架构,则进一步提升了系统在高分辨率、低延迟和多格式支持方面的表现能力。

TEDDYYW

C#调用avicap32.dll摄像头录制视频 ffmpeg压缩

基于avicap32.dll调用摄像头录制视频 实现监控功能,ffmpeg进行压缩,完整可运行

洛风123

通过avicap32.dll操作视频的类(C#)

#region 导入API函数 [DllImport("avicap32.dll")]//包含了执行视频捕获的函数,它给AVI文件I/O和视频、音频设备驱动程序提供一个高级接口 public static extern IntPtr capCreateCaptureWindow(string lpszWindowName, int dwStyle, int x, int y, int nWidth, int nHeight, IntPtr hwndParent, int nID); /************参数说明************* * * 函数:capCreateCaptureWindow * * lpszWindowName:标识窗口的名称 * dwStyle:标识窗口风格 * x、y:标识窗口的左上角坐标 * nWidth、nHeight:标识窗口的宽度和高度 * hWnd:标识父窗口句柄 * nID:标识窗口ID * * 返回值:视频捕捉窗口句柄。 * ********************************/ [DllImport("AVICAP32.dll", CharSet = CharSet.Unicode)] public static extern bool capGetDriverDescription(int wDriverIndex, StringBuilder lpszName, int cbName, StringBuilder lpszVer, int cbVer); [DllImport("User32.dll")] public static extern bool SendMessage(IntPtr hWnd, int wMsg, bool wParam, int lParam); [DllImport("User32.dll")] public static extern bool SendMessage(IntPtr hWnd, int wMsg, short wParam, int lParam); [DllImport("User32.dll")] public static extern bool SendMessage(IntPtr hWnd, int wMsg, int wParam, int lParam); [DllImport("User32.dll")] public static extern bool SendMessage(IntPtr hWnd, int wMsg, short wParam, FrameEventHandler lParam); [DllImport("User32.dll")] public static extern bool SendMessage(IntPtr hWnd, int wMsg, int wParam, ref BITMAPINFO lParam); [DllImport("User32.dll")] public static extern bool SendMessage(IntPtr hWnd, int wMsg, int wParam, ref CAPDRIVERCAPS lParam); [DllImport("User32.dll")] public static extern bool SendMessage(IntPtr hWnd, int wMsg, int wParam, ref CAPTUREPARMS lParam); [DllImport("User32.dll")] public static extern bool SendMessage(IntPtr hWnd, int wMsg, int wParam, ref CAPSTATUS lParam); [DllImport("User32.dll")] public static extern int SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int y, int cx, int cy, int wFlags); [DllImport("avicap32.dll")] public static extern int capGetVideoFormat(IntPtr hWnd, IntPtr psVideoFormat, int wSize); #endregion

superww058
评论 4
收藏 已收藏
分享
返回顶部
requiem01119
C# 111,129 成员 642,538 内容