码迷,mamicode.com
首页 > Windows程序 > 详细

winform用AForge拍照功能

时间:2015-08-25 18:32:43      阅读:1062      评论:0      收藏:0      [点我收藏+]

标签:

 

记得先引用DLL

private FilterInfoCollection videoDevices;
private VideoCaptureDevice videoSource;

BLL.AWBListBLL alb = new AWBListBLL();
public FormPhoto()
{
InitializeComponent();

//try
//{
// // 枚举所有视频输入设备
// videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);

// if (videoDevices.Count == 0)
// throw new ApplicationException();

// foreach (FilterInfo device in videoDevices)
// {
// tscbxCameras.Items.Add(device.Name);
// }

// tscbxCameras.SelectedIndex = 0;

//}
//catch (ApplicationException)
//{
// tscbxCameras.Items.Add("No local capture devices");
// videoDevices = null;
//}
//CameraConn();


}
public FormPhoto(string ShipCode,bool PrintView,int PhotoFlag)
{
InitializeComponent();
//try
//{
// // 枚举所有视频输入设备
// videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);

// if (videoDevices.Count == 0)
// throw new ApplicationException();

// foreach (FilterInfo device in videoDevices)
// {
// tscbxCameras.Items.Add(device.Name);
// }

// tscbxCameras.SelectedIndex = 0;

//}
//catch (ApplicationException)
//{
// tscbxCameras.Items.Add("No local capture devices");
// videoDevices = null;
//}

//shipcode = ShipCode;
//printpreview = PrintView;
//photoFlag = PhotoFlag;
//this.btnPhoto.Focus();

}

//连接摄像头
private void CameraConn()
{
VideoCaptureDevice videoSource = new VideoCaptureDevice(videoDevices[tscbxCameras.SelectedIndex].MonikerString);
videoSource.DesiredFrameSize = new System.Drawing.Size(919, 718);
videoSource.DesiredFrameRate = 1;

videoSourcePlayer.VideoSource = videoSource;
videoSourcePlayer.Start();
}


public FormPhoto(string sgcode,int photoflag)
{
InitializeComponent();

try
{
// 枚举所有视频输入设备
videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);

if (videoDevices.Count == 0)
throw new ApplicationException();

foreach (FilterInfo device in videoDevices)
{
tscbxCameras.Items.Add(device.Name);
}

tscbxCameras.SelectedIndex = 0;

}
catch (ApplicationException)
{
tscbxCameras.Items.Add("No local capture devices");
videoDevices = null;
}
CameraConn();
photoFlag = photoflag;
shipcode = sgcode;
this.btnPhoto.Focus();
}

//public void startVideo()
//{
// video.StartVideo();
// previewPictureBox.Visible = true;
// showPictureBox.Visible = false;
//}

private void btnPhoto_Click(object sender, EventArgs e)
{
try
{
string pathName = System.Windows.Forms.Application.StartupPath + "\\PhotoImage\\" + shipcode + DateTime.Now.ToString("yyyyMMddHHmmss") + ".jpg";
//保存图片
//video.Images(pathName);
////关闭摄像头
//video.StopVideo();
//showPictureBox.ImageLocation = pathName;
//previewPictureBox.Visible = false;
//showPictureBox.Visible = true;
if (videoSourcePlayer.IsRunning)
{
BitmapSource bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
videoSourcePlayer.GetCurrentVideoFrame().GetHbitmap(),
IntPtr.Zero,
Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions());
PngBitmapEncoder pe = new PngBitmapEncoder();
pe.Frames.Add(BitmapFrame.Create(bitmapSource));
using (Stream stream = File.Create(pathName))
{
pe.Save(stream);
}
videoSourcePlayer.Visible = false;
//拍照完成后关摄像头并刷新同时关窗体
if (videoSourcePlayer != null && videoSourcePlayer.IsRunning)
{
videoSourcePlayer.SignalToStop();
videoSourcePlayer.WaitForStop();
}
pictureBox1.Visible = true;
pictureBox1.ImageLocation = pathName;
DialogResult result = System.Windows.Forms.MessageBox.Show("Whether or not to save?", "The system prompt", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
//将图像读入到字节数组
//System.IO.FileStream fs = new System.IO.FileStream(pathName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
//byte[] buffByte = new byte[fs.Length];
//fs.Read(buffByte, 0, (int)fs.Length);
//fs.Close();
//fs = null;
FileStream fs = new FileStream(pathName, FileMode.Open);
Bitmap bmp = new Bitmap(fs);
fs.Close();
byte[] buffByte = ImageGdi(bmp);

//根据包裹号查运单号
DataSet ds = new DataSet();
ds = alb.SelectfegAWBListBySlShipCode1(shipcode);
if (ds.Tables[0].Rows.Count > 0)
{
imagemd.Sl_ShipNo = ds.Tables[0].Rows[0]["slShipCode"].ToString();
}
imagemd.Sl_Code = shipcode;
imagemd.ImageName = pathName.Substring(pathName.LastIndexOf("\\") + 1);
imagemd.Sl_Image = buffByte;
//将信息保存到数据库
DataSet ds1 = imagebll.SelectfegIamge2(shipcode);
if (ds1.Tables[0].Rows.Count > 0)
{
imagebll.UpdatefegImage(imagemd);
//修改以后 删除本地上张图片....

}
else
{
imagebll.InsertfegImage(imagemd);
}
SaveMessage.Text = "Save success!";
//判断是入库操作或者是打印面单操作 0为入库操作 1为打印面单操作
//if (photoFlag == 1)
//{
// //打印面单保存成功后跳转到打印页面
// UI.FormExpress express = new UI.FormExpress(shipcode, printpreview);
// express.Show();
// this.Close();
//}
//if (photoFlag == 0)
//{
// //入库操作 保存图片后直接关闭
// this.Close();
//}
//想了想还是删除照片 不然占内存大
System.IO.FileInfo file = new System.IO.FileInfo(pathName);
if (file.Exists)
{
file.Delete();
}
this.Close();
//读取二进制图片,待用到时再用。先保存在这里
//SqlCommand cmd = new SqlCommand("select 照片 from fuser where password=‘1b‘", conn);
//SqlDataReader reader = cmd.ExecuteReader();
//reader.Read();
//MemoryStream buf = new MemoryStream((byte[])reader[0]);
//Image image = Image.FromStream(buf, true);
//pictureBox1.Image = image;

}
else
{
//删除照片,重新拍
System.IO.FileInfo file = new System.IO.FileInfo(pathName);
if (file.Exists)
{
file.Delete();
}
pictureBox1.Visible = false;
videoSourcePlayer.Visible = true;
//startVideo();
CameraConn();
this.btnPhoto.Focus();
}
}
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show("摄像头异常:" + ex.Message);
}
}

private void btnConnect_Click(object sender, EventArgs e)
{
CameraConn();
}

private void button1_Click(object sender, EventArgs e)
{
videoSourcePlayer.SignalToStop();
videoSourcePlayer.WaitForStop();
}

///
/// GDI压缩图片
///
/// 传入参数Bitmap
///
public byte[] ImageGdi(Bitmap bmp)
{
Bitmap xbmp = new Bitmap(bmp);
MemoryStream ms = new MemoryStream();
xbmp.Save(ms, ImageFormat.Jpeg);
byte[] buffer;
ms.Flush();
if (ms.Length > 95000)
{
//buffer = ms.GetBuffer();
double new_width = 0;
double new_height = 0;

Image m_src_image = Image.FromStream(ms);
if (m_src_image.Width >= m_src_image.Height)
{
new_width = 1024;
new_height = new_width * m_src_image.Height / (double)m_src_image.Width;
}
else if (m_src_image.Height >= m_src_image.Width)
{
new_height = 768;
new_width = new_height * m_src_image.Width / (double)m_src_image.Height;
}

Bitmap bbmp = new Bitmap((int)new_width, (int)new_height, m_src_image.PixelFormat);
Graphics m_graphics = Graphics.FromImage(bbmp);
m_graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
m_graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
m_graphics.DrawImage(m_src_image, 0, 0, bbmp.Width, bbmp.Height);

ms = new MemoryStream();

bbmp.Save(ms, ImageFormat.Jpeg);
buffer = ms.GetBuffer();
ms.Close();

return buffer;
}
else
{
buffer = ms.GetBuffer();
ms.Close();
return buffer;
}
}

private void FormPhoto_FormClosing(object sender, FormClosingEventArgs e)
{
videoSourcePlayer.SignalToStop();
videoSourcePlayer.WaitForStop();
}

// private byte[] CompressionImage(Stream fileStream, long quality)
//{
// using (System.Drawing.Image img = System.Drawing.Image.FromStream(fileStream))
// {
// using (Bitmap bitmap = new Bitmap(img))
// {
// System.Drawing.Imaging.ImageCodecInfo CodecInfo = GetEncoder(img.RawFormat);
// System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Quality;
// EncoderParameters myEncoderParameters = new EncoderParameters(1);
// EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, quality);
// myEncoderParameters.Param[0] = myEncoderParameter;
// using (MemoryStream ms = new MemoryStream())
// {
// bitmap.Save(ms, CodecInfo, myEncoderParameters);
// myEncoderParameters.Dispose();
// myEncoderParameter.Dispose();
// return ms.ToArray();
// }
// }
// }
//}

winform用AForge拍照功能

标签:

原文地址:http://www.cnblogs.com/panjuan/p/4757632.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!