AForge实现拍照
记得先引用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();
// }
// }
// }
//}
AForge实现拍照的更多相关文章
- AForge.net 录像拍照功能实现 转
AForge.net 使用之录像拍照功能实现 最近使用aforge.NET拍照录像功能实现 记录一下以便以后好学习,哈哈,直接上代码 连接摄像头设备,这里需要引入 AForge.Video; AFor ...
- C# 使用AForge调用笔记本摄像头拍照
vs2012 winform 连接摄像头设备,这里需要引入 代码: using AForge; using AForge.Controls; using AForge.Imaging; using ...
- 在WPF中使用AForge.net控制摄像头拍照
原文:在WPF中使用AForge.net控制摄像头拍照 利用AForge.net控制摄像头拍照最方便的方法就是利用PictureBox显示摄像头画面,但在WPF中不能直接使用PictureBox.必须 ...
- AForge.net 使用之录像拍照功能实现
连接摄像头设备,这里需要引入 AForge.Video; AForge.Video.DirectShow; AForge.Video.FFMPEG; 还需要添加引用,aforge.dll,aforge ...
- 利用AForge.NET 调用电脑摄像头进行拍照
当然了,你需要去官网下载类库,http://www.aforgenet.com/ 调用本机摄像头常用的组件: AForge AForge.Controls AForge.Imaging AForge. ...
- C#使用Aforge调用摄像头拍照
一. 新建一个Winform项目 二.使用Nuget添加引用 安装下图中红色框住的两个程序包 安装完后发现安装了如下图的程序包,这是因为上述两个程序包存在对其它程序包的依赖. 三.编写程序 1. 窗体 ...
- C# - VS2019调用AForge库实现调用摄像头拍照功能
前言 作为一名资深Delphi7程序员,想要实现摄像头扫描一维码/二维码功能,发现所有免费的第三方库都没有简便的实现办法,通用的OpenCV或者ZXing库基本上只支持XE以上的版本,而且一维码的识别 ...
- WinForm利用AForge.NET调用电脑摄像头进行拍照和视频
当然了,你需要去官网下载类库,http://www.aforgenet.com/ 调用本机摄像头常用的组件: AForge AForge.Controls AForge.Imaging AForge. ...
- AForge调用摄像头拍照时设置分辨率
简单记录下AForge2.2.5.0版本调用摄像头拍照时设置分辨率的方法. FilterInfo info = _videoDevices[0];//获取第一个摄像头 _cameraDevice = ...
随机推荐
- 深复制VS浅复制(MemberwiseClone方法介绍)
MemberwiseClone方法,属于命名空间System,存在于程序集 mscorlib.dll中.返回值是System.Object.其含义是:创建一个当前object对象的浅表副本. MSDN ...
- 从单页应用(SPA)到服务器渲染(SSR)
从单页应用(SPA)到服务器渲染(SSR) 情景回顾 在学习Vue开发一个电商网站的管理后台时,使用到了一个组件 vue-quill-editor 主要是一个快捷的一个富文本编辑器 在使用这个组件的组 ...
- js实现购物车左滑删除
使用 js 和jquery动画实现购物车左滑,请引入jquery库,不然会报错哦! 首页编写页面,注意布局,滑动分成两部分,商品图片和信息放在一个布局,删除和移入收藏放在一起. 其中js用到了 tou ...
- 2. Linear Regression with One Variable
Speaker:Andrew Ng 这一次主要讲解的是单变量的线性回归问题. 1.Model Representation 先来一个现实生活中的例子,这里的例子是房子尺寸和房价的模型关系表达. 通过学 ...
- 2019HDU多校 Round9
Solved:3 02 Rikka with Cake (树状数组) #include <bits/stdc++.h> using namespace std; typedef long ...
- 1569: Wet Tiles
Description Alice owns a construction company in the town of Norainia, famous for its unusually dry ...
- js实现一棵树的生长
参考链接:https://blog.csdn.net/u010298576/article/details/76609244 HTML网页源码: 1 <!DOCTYPE html> 2 & ...
- Codeforces Round #544 (Div. 3) E. K Balanced Teams (DP)
题意:有\(n\)个人,每个人的能力值是\(a_i\),现在你想将这些人分成\(k\)组(没必要全选),但是每组中最高水平和最低水平的人的能力差值必须\(\le 5\),问最多能选多少人. 题解:想了 ...
- Codeforces Global Round 9 C. Element Extermination (思维,栈)
题意:有一个长度\(n\)的序列,如果\(a_{i}<a_{i+1}\),那么可以选择删除\(a_{i}\)或者\(a_{i+1}\),再继续操作,问是否能够将序列删到只剩一个元素. 题解:感觉 ...
- python程序配置守护进程
参考博客 python Supervisor 使用与配置_a35155的博客-CSDN博客 Ubuntu系统下:apt-get install supervisor,通过这种方式安装后,自动设置为开机 ...