基于官方SDK,API手册版本
1.7.6855.20160217
,cs文件版本44.16010.2019.1128
官方SDK下载地址:SDK
本文章不讲解一些细微的东西,具体细节的东西,在官方压缩包里提供有文档,可以自行查看,这里主要介绍如何使用(本例子基于WPF)。
实例化一个相机对象Toupcam toupcam_ = null;
函数 | 说明 |
---|---|
Toupcam.EnumV2() | 调用该函数枚举计算机上当前插上的Toupcam相机,返回Toupcam.DeviceV2[] |
Toupcam.Open(string id) | 传入相机的ID,打开第对应相机,如果传入null,则自动打开第一个相机,返回一个Toupcam 类型的实例,打开失败时返回NULL |
函数调用如下:
获取支持的分辨率个数:uint resnum = toupcam_.ResolutionNumber;
在通过对象方法get_Resolution
可以得到每种分辨率的高度/宽度
for (uint i = 0; i < resnum; ++i)
{
int w = 0, h = 0;
if (toupcam_.get_Resolution(i, out w, out h))
{
cmb_camera_get_resolution.AddItemBindUidAndContent(w.ToString() + "*" + h.ToString(), w.ToString() + "*" + h.ToString());
cmb_camera_resolution.AddItemBindUidAndContent(w.ToString() + "*" + h.ToString(), w.ToString() + "*" + h.ToString());
}
}
获取当前预览情况下的分辨率序号:toupcam_.get_eSize(out eSize)
设置当前预览情况下的分辨率序号:toupcam_.put_eSize(out eSize)
ComboBoxExtendMethod.cs
和StringExtendMethod.cs
是我常用的扩展toupcam.dll
属性如下:<Window x:Class="ToupCamTestWPF.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
mc:Ignorable="d" Loaded="Window_Loaded"
Title="ToupCam" Height="450" Width="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="34"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="100"></RowDefinition>
<RowDefinition Height="34"></RowDefinition>
</Grid.RowDefinitions>
<Grid Grid.Row="0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="90"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Label Content="选择摄像头:" HorizontalContentAlignment="Right" VerticalContentAlignment="Center"></Label>
<ComboBox Name="cmb_camera" Grid.Column="1" Height="26" Margin="0 0 5 0" VerticalContentAlignment="Center"></ComboBox>
</Grid>
<WindowsFormsHost Grid.Row="1" HorizontalAlignment="Stretch" Margin="0,0,0,0" VerticalAlignment="Stretch">
<wf:PictureBox x:Name="toupcam_vce"></wf:PictureBox>
</WindowsFormsHost>
<Button x:Name="btn_save" Grid.Row="3" Height="26" Width="60" Content="拍 摄" Margin="0 0 0 0" Click="btn_save_Click"></Button>
</Grid>
</Window>
using System;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interop;
using PixelFormat = System.Drawing.Imaging.PixelFormat;
using ToupCamTestWPF.ExtendMethod;
namespace ToupCamTestWPF
{
///
/// MainWindow.xaml 的交互逻辑
///
public partial class MainWindow : Window
{
public Toupcam toupcam_ = null;
public Bitmap bmp_ = null;
public uint MSG_CAMEVENT = 0x8001;
public Toupcam.DeviceV2[] ToupcamArr = null;
public MainWindow()
{
InitializeComponent();
}
private void btn_save_Click(object sender, RoutedEventArgs e)
{
if (toupcam_ != null)
{
btn_save.IsEnabled = false;
toupcam_.Snap(0);
}
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
ToupcamArr = Toupcam.EnumV2();
if (ToupcamArr.Length <= 0)
{
MessageBox.Show("No device!");
}
foreach (var cam_item in ToupcamArr)
{
cmb_camera.AddItemBindUidAndContent(cam_item.id, cam_item.displayname);
}
cmb_camera.SelectionChanged += Cmb_camera_SelectionChanged;
}
private void Cmb_camera_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
OpenToupCam();
}
private void OpenToupCam()
{
if (toupcam_ != null)
{
CloseToupCam();
}
toupcam_ = Toupcam.Open(cmb_camera.GetComboBoxSelectedItemUidAsString());
if (toupcam_ == null)
{
throw new Exception("摄像头打开失败!");
}
int width, height;
if (toupcam_.get_Size(out width, out height))
{
bmp_ = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
IntPtr hwnd = new WindowInteropHelper(this).Handle;
if (!toupcam_.StartPullModeWithWndMsg(hwnd, MSG_CAMEVENT))
{
MessageBox.Show("Window_Loaded:Failed to start device!", "Error");
}
}
}
private void CloseToupCam()
{
if (toupcam_ != null)
{
toupcam_.Close();
}
}
#region Toupcam相关方法
protected override void OnSourceInitialized(EventArgs e)
{
base.OnSourceInitialized(e);
HwndSource source = PresentationSource.FromVisual(this) as HwndSource;
source.AddHook(WndProc);
}
private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
if (MSG_CAMEVENT == msg)
{
switch ((Toupcam.eEVENT)wParam)
{
case Toupcam.eEVENT.EVENT_ERROR:
OnEventError();
break;
case Toupcam.eEVENT.EVENT_DISCONNECTED:
OnEventDisconnected();
break;
case Toupcam.eEVENT.EVENT_EXPOSURE:
OnEventExposure();
break;
case Toupcam.eEVENT.EVENT_IMAGE:
OnEventImage();
break;
case Toupcam.eEVENT.EVENT_STILLIMAGE:
OnEventStillImage();
break;
case Toupcam.eEVENT.EVENT_TEMPTINT:
OnEventTempTint();
break;
}
}
return IntPtr.Zero;
}
private void OnEventError()
{
if (toupcam_ != null)
{
toupcam_.Close();
toupcam_ = null;
}
MessageBox.Show("OnEventError:The camera open error.", "Error");
}
private void OnEventDisconnected()
{
if (toupcam_ != null)
{
toupcam_.Close();
toupcam_ = null;
}
MessageBox.Show("OnEventDisconnected:The camera is disconnected, maybe has been pulled out.", "Error");
}
private void OnEventExposure()
{
if (toupcam_ != null)
{
uint nTime = 0;
if (toupcam_.get_ExpoTime(out nTime))
{
}
}
}
private void OnEventImage()
{
if (bmp_ != null)
{
BitmapData bmpdata = bmp_.LockBits(new System.Drawing.Rectangle(0, 0, bmp_.Width, bmp_.Height), ImageLockMode.WriteOnly, bmp_.PixelFormat);
uint nWidth = 0, nHeight = 0;
toupcam_.PullImage(bmpdata.Scan0, 24, out nWidth, out nHeight);
bmp_.UnlockBits(bmpdata);
toupcam_vce.Image = bmp_;
}
}
private void OnEventStillImage()
{
Toupcam.FrameInfoV2 info = new Toupcam.FrameInfoV2();
if (toupcam_.PullStillImageV2(IntPtr.Zero, 24, out info)) /* peek the width and height */
{
Bitmap sbmp = new Bitmap((int)info.width, (int)info.height, PixelFormat.Format24bppRgb);
BitmapData bmpdata = sbmp.LockBits(new System.Drawing.Rectangle(0, 0, sbmp.Width, sbmp.Height), ImageLockMode.WriteOnly, sbmp.PixelFormat);
toupcam_.PullStillImageV2(bmpdata.Scan0, 24, out info);
sbmp.UnlockBits(bmpdata);
FileStream fs = new FileStream("1.png", FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
btn_save.IsEnabled = true;
sbmp.Dispose();
fs.Dispose();
sbmp = null;
fs = null;
GC.Collect();
}
}
public void OnEventTempTint()
{
if (toupcam_ != null)
{
int nTemp = 0, nTint = 0;
if (toupcam_.get_TempTint(out nTemp, out nTint))
{
}
}
}
#endregion
}
}