如何设置网络摄像头的分辨率 [英] how to set resolution of web cam

查看:143
本文介绍了如何设置网络摄像头的分辨率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设置网络摄像头的分辨率。

我的代码是

I want to set resolution of web cam.
My code is

[StructLayout(LayoutKind.Sequential)]
public struct BITMAPINFOHEADER
{
public uint biSize;
public int biWidth;
public int biHeight;
public ushort biPlanes;
public ushort biBitCount;
public uint biCompression;
public uint biSizeImage;
public int biXPelsPerMeter;
public int biYPelsPerMeter;
public uint biClrUsed;
public uint biClrImportant;
}
[StructLayout(LayoutKind.Sequential)]
public struct BITMAPINFO
{
public BITMAPINFOHEADER bmiHeader;
public int bmiColors;
}
public class Avicap32
{
[DllImport("avicap32.dll")]
public extern static IntPtr capGetDriverDescription(ushort index, StringBuilder name, int nameCapacity, StringBuilder description, int descriptionCapacity);
[DllImport("avicap32.dll")]
public extern static IntPtr capCreateCaptureWindow(string title, uint style, int x, int y, int width, int height, IntPtr window, int id);
[DllImport("avicap32.dll")]
public extern static bool capDriverGetName(IntPtr deviceHandle, StringBuilder Drivername, byte size);
[DllImport("avicap32.dll")]
public extern static UInt32 capGetVideoFormat(IntPtr deviceHandle,ref  BITMAPINFO m_bitmapinfo, byte size);
[DllImport("avicap32.dll")]
public extern static bool capSetVideoFormat(IntPtr deviceHandle,ref BITMAPINFO m_bitmapinfo, byte size);
[DllImport("avicap32.dll" ,EntryPoint="capCaptureAbort")]
public extern static bool capCaptureAbort(IntPtr deviceHandle);
[DllImport("avicap32.dll" ,EntryPoint="capCaptureStop")]
public extern static bool capCaptureStop(IntPtr deviceHandle);
public class Constants
{
public const uint WM_CAP = 0x400;
public const uint WM_CAP_DRIVER_CONNECT = 0x40a;
public const uint WM_CAP_DRIVER_DISCONNECT = 0x40b;
public const uint WM_CAP_EDIT_COPY = 0x41e;
public const uint WM_CAP_SET_PREVIEW = 0x432;
public const uint WM_CAP_SET_OVERLAY = 0x433;
public const uint WM_CAP_SET_PREVIEWRATE = 0x434;
public const uint WM_CAP_SET_SCALE = 0x435;
public const uint WS_CHILD = 0x40000000;
public const uint WS_VISIBLE = 0x10000000;
}
public   const int WM_CAP_START = 1024; // WM_USER
public const int WM_CAP_SET_CALLBACK_FRAME = WM_CAP_START + 5;
public const int WM_CAP_DRIVER_CONNECT = WM_CAP_START + 10;
public const int WM_CAP_DRIVER_DISCONNECT = WM_CAP_START + 11;
public const int WM_CAP_DLG_VIDEODISPLAY = WM_CAP_START + 42;
public const int WM_CAP_GET_VIDEOFORMAT = WM_CAP_START + 44;
public const int WM_CAP_SET_VIDEOFORMAT = WM_CAP_START + 45;
public const int WM_CAP_SET_PREVIEW = WM_CAP_START + 50;
public const int WM_CAP_SET_PREVIEWRATE = WM_CAP_START + 52;
public const int WM_CAP_GRAB_FRAME = WM_CAP_START + 60;
public const int WM_CAP_GRAB_FRAME_NOSTOP = WM_CAP_START + 61;

[DllImport("user32", EntryPoint = "SendMessage")]
public static extern int SendBitmapMessage(IntPtr hWnd, uint wMsg, int wParam, ref BITMAPINFO lParam);



public void Attach(System.Windows.Forms.Control control)
{
deviceHandle = capCreateCaptureWindow("", Constants.WS_VISIBLE | Constants.WS_CHILD, 0, 0,control.Width,control.Height, control.Handle, 0);
bInfo=new SystemDLL.User32.BITMAPINFO();
bInfo.bmiHeader = new BITMAPINFOHEADER();
if (SendMessage(deviceHandle, Constants.WM_CAP_DRIVER_CONNECT, (IntPtr)deviceNumber, (IntPtr)0).ToInt32() > 0)
{
SendMessage(deviceHandle, Constants.WM_CAP_SET_SCALE, (IntPtr)(-1), (IntPtr)0);
SendMessage(deviceHandle, Constants.WM_CAP_SET_PREVIEWRATE, (IntPtr)0x42, (IntPtr)0);
SendMessage(deviceHandle,Constants.WM_CAP_SET_PREVIEW, (IntPtr)(-1), (IntPtr)0);
bInfo=new BITMAPINFO();
bInfo.bmiHeader = new BITMAPINFOHEADER();
SendBitmapMessage(deviceHandle, WM_CAP_GET_VIDEOFORMAT, Marshal.SizeOf(bInfo), ref bInfo);
bInfo.bmiHeader.biSize = (uint)Marshal.SizeOf(bInfo.bmiHeader);
bInfo.bmiHeader.biWidth = control.Width;
bInfo.bmiHeader.biHeight = control.Height;
SendBitmapMessage(deviceHandle, WM_CAP_SET_VIDEOFORMAT, Marshal.SizeOf(bInfo), ref bInfo);
    
SetWindowPos(deviceHandle, new IntPtr(1), 0, 0,control.Width ,control.Height, 6);
}
else
{
MessageBox.Show("Webcam not found ");
DestroyWindow(deviceHandle);
}
}




SendBitmapMessage(deviceHandle, WM_CAP_GET_VIDEOFORMAT, Marshal.SizeOf(bInfo), ref bInfo);



工作正常,但


is working fine but

SendBitmapMessage(deviceHandle, WM_CAP_SET_VIDEOFORMAT, Marshal.SizeOf(bInfo), ref bInfo);

不起作用。(为什么?)

does not work.(why?)

推荐答案

不仅需要设置 biWidth biHeight 分辨率更改。

除了需要更改的参数(如果您使用之前获得的格式),您应该设置 biSizeImage = biWidth * biHeight * biBitCount / 8

注意:你不能设置你喜欢的分辨率bcs驱动程序可能不支持它,至少我建议你有16对齐和高度4对齐。

您最好尝试使用DirectShow,因为您可以使用det ermine和枚举可用的输出格式。



问候,

Maxim。
It is not only biWidth and biHeight necessary to set for the resolution change.
In additionally to parameters which require to change (this in case you uses the format which you get previously) you should set the biSizeImage = biWidth * biHeight * biBitCount / 8.
Note: you can''t set the resolution you like bcs driver may not support it, at least I suggset you to have with be 16 aligned and height 4 aligned.
You may better to try to use DirectShow as in there you available to determine and enumerate available formats of output.

Regards,
Maxim.


这篇关于如何设置网络摄像头的分辨率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆