/// <summary> /// 从本地文件夹选择 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void BtnLocal_Click(object sender, RoutedEventArgs e) { OpenFileDialog openFile = new OpenFileDialog { Filter = "图片文件(*.png;*.jpg;*.bmp;*.jpeg)|*.png;*.jpg;*.bmp;*.jpeg" }; if (openFile.ShowDialog() == true) { FileStream fileStream = File.Open(openFile.FileName, FileMode.Open); BarCodeScan scan = new BarCodeScan(); Response <string> result = scan.GetBarCode(fileStream); if (!result.Success) { MessageBox.Show(result.Errors, "Error", MessageBoxButton.OK, MessageBoxImage.Error); } else { BarCode = result.Data; if (string.IsNullOrEmpty(BarCode)) { MessageBox.Show("No valid barcode was obtained,Please Retry", "Notice", MessageBoxButton.OK, MessageBoxImage.Information); } else { VideoCapture.Pause(); Close(); } } fileStream.Close(); } }
/// <summary> /// 拍照 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void BtnPlay_Click(object sender, RoutedEventArgs e) { //抓取控件做成图片 RenderTargetBitmap bmp = new RenderTargetBitmap((int)VideoCapture.ActualWidth, (int)VideoCapture.ActualHeight, 96, 96, PixelFormats.Default); bmp.Render(VideoCapture); BitmapEncoder encoder = new JpegBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(bmp)); string tmpFileName = Path.GetTempFileName(); using (MemoryStream ms = new MemoryStream()) { encoder.Save(ms); File.WriteAllBytes(tmpFileName, ms.ToArray()); } BarCodeScan scan = new BarCodeScan(); using (FileStream fileSteam = File.OpenRead(tmpFileName)) { Response <string> result = scan.GetBarCode(fileSteam); if (!result.Success) { MessageBox.Show("No valid barcode was obtained,Please Retry", "Error", MessageBoxButton.OK, MessageBoxImage.Error); VideoCapture.Play(); } else { BarCode = result.Data; if (string.IsNullOrEmpty(BarCode)) { MessageBox.Show("No valid barcode was obtained,Please Retry", "Notice", MessageBoxButton.OK, MessageBoxImage.Information); } else { VideoCapture.Pause(); this.Close(); } } } }
/// <summary> /// 拍照识别 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void BtnPlay_Click(object sender, RoutedEventArgs e) { //抓取控件做成图片 RenderTargetBitmap bmp = new RenderTargetBitmap((int)VideoCapture.NaturalVideoWidth, (int)VideoCapture.NaturalVideoHeight, 96, 96, PixelFormats.Default); //VideoCapture.Stretch = Stretch.Fill; VideoCapture.Measure(VideoCapture.RenderSize); VideoCapture.Arrange(new Rect(VideoCapture.RenderSize)); bmp.Render(VideoCapture); BitmapEncoder encoder = new JpegBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(bmp)); BarCodeScan scan = new BarCodeScan(); using (MemoryStream ms = new MemoryStream()) { encoder.Save(ms); Response <string> result = scan.GetBarCode(ms); if (!result.Success) { MessageBox.Show(result.Errors, "Error", MessageBoxButton.OK, MessageBoxImage.Error); VideoCapture.Play(); } else { BarCode = result.Data; if (string.IsNullOrEmpty(BarCode)) { MessageBox.Show("No valid barcode was obtained,Please Retry", "Notice", MessageBoxButton.OK, MessageBoxImage.Information); } else { VideoCapture.Pause(); Close(); } } } }