private void LoadTitleCenterData() { string titleMsg ="二维码识别主界面"; Graphics g = this.CreateGraphics(); Double startingPoint = (this.Width / 2) - (g.MeasureString(titleMsg, this.Font).Width / 2); Double widthOfASpace = g.MeasureString(" ", this.Font).Width; String tmp = " "; Double tmpWidth = 0; while ((tmpWidth + widthOfASpace) < startingPoint) { tmp += " "; tmpWidth += widthOfASpace; } this.Text = tmp + titleMsg; }最大最小化禁用
public Form1() { this.MinimizeBox = false; this.MaximizeBox = false; InitializeComponent(); LoadTitleCenterData(); }Form1中添加一个TableLayoutPanel,三行三列,比例按照百分比:10%,80%,10%。在TableLayoutPanel的80%中再添加一个TableLayoutPanel,还是行比例:20%,80%这样(二八定律)。在TableLayoutPanel中添加Panel,在其中手动在添加几个按钮和label。
private AForge.Video.DirectShow.FilterInfoCollection _videoDevices;//摄像设备 System.Timers.Timer timer;//定时器 CameraHelper _cameraHelper = new CameraHelper();//视屏设备操作类窗体Load事件中获取拍照设备列表,并将第一个设备作为摄像设备(如有前后两个或多个摄像头,自己去改一下代码,设置成可以选择的,在CameraHelper中的CreateFilterInfoCollection()中):
private void CameraQR_Load(object sender, EventArgs e) { // 获取视频输入设备 _videoDevices = _cameraHelper.CreateFilterInfoCollection();//获取拍照设备列表 if (_videoDevices.Count == 0) { MessageBox.Show("无设备"); this.Dispose(); this.Close(); return; } resultStr = "";//二维码识别字符串清空 _cameraHelper.ConnectDevice(videoSourcePlayer1);//连接打开设备 }组件初始化完成之后,添加一个定时任务,用来阶段性识别摄像设备中的图片资源,我写的是每200毫秒去识别一次,如果图片中有二维码,就识别二维码;识别成功之后,关闭窗体,将识别结果返回给上一个界面,此处需要一个有识别二维码程序包。
public CameraQR() { this.MinimizeBox = false; this.MaximizeBox = false; InitializeComponent(); LoadTitleCenterData(); CheckForIllegalCrossThreadCalls = false;//多线程中访问窗体控件资源不会异常 AddTimer();//定时识别图片 } private void AddTimer() { timer = new System.Timers.Timer(); timer.Enabled = true; timer.Interval = 200; timer.Start(); timer.Elapsed += new ElapsedEventHandler(PicToQRCode); } private void PicToQRCode(object sender, ElapsedEventArgs e) { if (_cameraHelper.img == null) return; BinaryBitmap bitmap = null; try { MemoryStream ms = new MemoryStream(); _cameraHelper.img.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp); byte[] bt = ms.GetBuffer(); ms.Close(); LuminanceSource source = new RGBLuminanceSource(bt, _cameraHelper.img.Width, _cameraHelper.img.Height); bitmap = new BinaryBitmap(new ZXing.Common.HybridBinarizer(source)); } catch (Exception ex) { return; } Result result=null; try { //开始解码 result = new MultiFormatReader().decode(bitmap); } catch (ReaderException ex) { resultStr = ex.ToString(); } if (result != null) { resultStr = result.Text; this.DialogResult = DialogResult.OK; this.Close(); } }窗体关闭时,记得释放定时器 关闭摄像头(不然异常满天飞):
private void CameraQR_FormClosing(object sender, FormClosingEventArgs e) { if (timer != null) { timer.Dispose(); } _cameraHelper.CloseDevice(); }CameraHelper类:
public class CameraHelper { public FilterInfoCollection _videoDevices;//本机摄像硬件设备列表 public VideoSourcePlayer _videoSourcePlayer;//视频画布 public Bitmap img = null;//全局变量,保存每一次捕获的图像 public System.Drawing.Image CaptureImage(VideoSourcePlayer sourcePlayer = null) { if (sourcePlayer == null || sourcePlayer.VideoSource == null) { if (_videoSourcePlayer == null) return null; else { sourcePlayer = _videoSourcePlayer; } } try { if (sourcePlayer.IsRunning) { System.Drawing.Image bitmap = sourcePlayer.GetCurrentVideoFrame(); return bitmap; } return null; } catch (Exception ex) { return null; } } public FilterInfoCollection CreateFilterInfoCollection() { if (_videoDevices != null) return _videoDevices; _videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice); return _videoDevices; } public VideoCaptureDevice ConnectDevice(VideoSourcePlayer videoSourcePlayer, FilterInfo filterInfo = null) { VideoCaptureDevice videoSource = new VideoCaptureDevice(); if (filterInfo == null) { videoSource = new VideoCaptureDevice(_videoDevices[_videoDevices.Count - 1].MonikerString); } else { videoSource = new VideoCaptureDevice(filterInfo.MonikerString); } videoSource.NewFrame += new NewFrameEventHandler(video_NewFrame); videoSourcePlayer.VideoSource = videoSource; videoSourcePlayer.Start(); _videoSourcePlayer = videoSourcePlayer; return videoSource; } private void video_NewFrame(object sender, NewFrameEventArgs eventArgs) { img = (Bitmap)eventArgs.Frame.Clone(); } public void CloseDevice(VideoSourcePlayer videoSourcePlayer = null) { if (videoSourcePlayer == null) { if (_videoSourcePlayer == null) return; _videoSourcePlayer.SignalToStop(); } else { videoSourcePlayer.SignalToStop(); } } }我用的测试二维码是:
最终的别结果为: