using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Drawing.Imaging;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Threading;
using HalconDotNet;
///
/// 摄像头类
///
public class ImageGraber
{
[DllImport("Kernel32.dll")]
internal static extern void CopyMemory(int dest, int source, int size);
private HObject ho_Image = null; //Halcon中采集的图像对象
private HTuple hv_AcqHandle = null; //Halcon中摄像头操作句柄
private PictureBox picCamera = null; //采集图像显示区
private Thread imageGrabeThread = null; //图像采集异步线程
///
/// 摄像头初始化成功标志
///
public bool IsInitSuccess { get; set; }
///
/// 摄像头处于采集中标志
///
public bool IsStart { get; set; }
///
/// 摄像头增益参数
///
private int gain;
public int Gain
{
get
{
return gain;
}
set
{
try
{
HOperatorSet.SetFramegrabberParam(hv_AcqHandle, "gain", value);
gain = value;
}
catch (Exception) { }
}
}
///
/// 摄像头快门参数
///
private int shutter;
public int Shutter
{
get
{
return shutter;
}
set
{
try
{
HOperatorSet.SetFramegrabberParam(hv_AcqHandle, "shutter", value);
shutter = value;
}
catch (Exception) { }
}
}
//其他摄像头参数如法炮制...
///
/// 构造函数
///
public ImageGraber(PictureBox picCamera)
{
try
{
this.picCamera = picCamera;
IsInitSuccess = false;
IsStart = false;
HOperatorSet.GenEmptyObj(out ho_Image);
HOperatorSet.OpenFramegrabber("DahengCAM", 1, 1, 0, 0, 0, 0, "interlaced", 8,
"gray", -1, "false", "HV-xx51", "1", 1, -1, out hv_AcqHandle);
HOperatorSet.GrabImageStart(hv_AcqHandle, -1);
IsInitSuccess = true;
}
catch (Exception) { IsInitSuccess = false; }
}
///
/// 采集一帧图像
///
///
public Bitmap GrabSingleFrame()
{
try
{
Bitmap GrabBitmap = null;
ho_Image.Dispose();
HOperatorSet.GrabImageAsync(out ho_Image, hv_AcqHandle, -1);
ConvertHalconGrayByteImageToBitmap(ho_Image, out GrabBitmap);
return GrabBitmap;
}
catch (Exception) { return null; }
}
///
/// 关闭摄像头
///
///
public bool CloseCamera()
{
try
{
HOperatorSet.CloseFramegrabber(hv_AcqHandle);
ho_Image.Dispose();
return true;
}
catch (Exception) { return false; }
}
///
/// 图像采集线程函数
///
private void ImageGrabThread()
{
while (true)
{
this.picCamera.Image = GrabSingleFrame();
}
}
///
/// 开始采集
///
public void Start()
{
if (!IsStart && imageGrabeThread == null)
{
imageGrabeThread = new Thread(new ThreadStart(ImageGrabThread));
imageGrabeThread.Start();
IsStart = true;
}
}
///
/// 停止采集
///
public void Stop()
{
if (IsStart && imageGrabeThread != null)
{
imageGrabeThread.Abort();
imageGrabeThread = null;
IsStart = false;
}
}
///
/// 将Halcon中8位灰度图转换为Bitmap图像
///
///
Halcon中8位灰度图
///
.net中Bitmap图像
private void ConvertHalconGrayByteImageToBitmap(HObject image, out Bitmap res)
{
HTuple hpoint, type, width, height;
const int Alpha = 255;
int[] ptr = new int[2];
HOperatorSet.GetImagePointer1(image, out hpoint, out type, out width, out height);
res = new Bitmap(width, height, PixelFormat.Format8bppIndexed);
ColorPalette pal = res.Palette;
for (int i = 0; i <= 255; i++)
{
pal.Entries[i] = Color.FromArgb(Alpha, i, i, i);
}
res.Palette = pal;
Rectangle rect = new Rectangle(0, 0, width, height);
BitmapData bitmapData = res.LockBits(rect, ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);
int PixelSize = Bitmap.GetPixelFormatSize(bitmapData.PixelFormat) / 8;
ptr[0] = bitmapData.Scan0.ToInt32();
ptr[1] = hpoint.I;
if (width % 4 == 0)
CopyMemory(ptr[0], ptr[1], width * height * PixelSize);
else
{
for (int i = 0; i < height - 1; i++)
{
ptr[1] += width;
CopyMemory(ptr[0], ptr[1], width * PixelSize);
ptr[0] += bitmapData.Stride;
}
}
res.UnlockBits(bitmapData);
}
///
/// 将Halcon中RGB图像转换为Bitmap图像
///
///
Halcon中RGB图像
///
.net中Bitmap图像
private void GenertateRGBBitmap(HObject image, out Bitmap res)
{
HTuple hred, hgreen, hblue, type, width, height;
HOperatorSet.GetImagePointer3(image, out hred, out hgreen, out hblue, out type, out width, out height);
res = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
Rectangle rect = new Rectangle(0, 0, width, height);
BitmapData bitmapData = res.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format32bppRgb);
unsafe
{
byte* bptr = (byte*)bitmapData.Scan0;
byte* r = ((byte*)hred.I);
byte* g = ((byte*)hgreen.I);
byte* b = ((byte*)hblue.I);
for (int i = 0; i < width * height; i++)
{
bptr[i * 4] = (b)[i];
bptr[i * 4 + 1] = (g)[i];
bptr[i * 4 + 2] = (r)[i];
bptr[i * 4 + 3] = 255;
}
}
res.UnlockBits(bitmapData);
}
}