using OpenCvSharp; using System; using System.Windows; using System.Windows.Media.Imaging; using ZXing; using ZXing.OpenCV; using System.Windows.Media; using System.Drawing; using System.IO; using ZXing.Common; // Para DecodingOptions y otros using ZXing.Windows.Compatibility; using BarcodeReader = ZXing.OpenCV.BarcodeReader; // Asegúrate de añadir este using using System.Media; using System.Windows.Controls; using Brushes = System.Windows.Media.Brushes; namespace QRReaderFromAndroid { /// /// Interaction logic for MainWindow.xaml /// public partial class MainWindow : System.Windows.Window { private VideoCapture _capture; private BarcodeReader _reader; private HashSet _scannedBarcodes = new HashSet(); private const string FilePath = "scanned_barcodes.txt"; private SoundPlayer _soundPlayer = new SoundPlayer("D:\\Proyectos\\VisualStudio\\QRReaderFromAndroid\\store-scanner-beep-90395.wav"); // Configura la ruta al archivo de sonido public MainWindow() { InitializeComponent(); _reader = new BarcodeReader(); _capture = new VideoCapture(0); _capture.FrameWidth = 640; _capture.FrameHeight = 480; CompositionTarget.Rendering += UpdateFrame; LoadScannedBarcodes(); } public static BitmapSource MatToBitmapSource(Mat mat) { var bitmap = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(mat); var bitmapData = bitmap.LockBits( new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, bitmap.PixelFormat); var bitmapSource = BitmapSource.Create( bitmapData.Width, bitmapData.Height, bitmap.HorizontalResolution, bitmap.VerticalResolution, System.Windows.Media.PixelFormats.Bgr24, null, bitmapData.Scan0, bitmapData.Stride * bitmapData.Height, bitmapData.Stride); bitmap.UnlockBits(bitmapData); bitmap.Dispose(); return bitmapSource; } private void UpdateFrame(object sender, EventArgs e) { using (var frame = new Mat()) { _capture.Read(frame); if (!frame.Empty()) { var bitmap = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(frame); var result = ScanBarcode(bitmap); if (result != null && result.Text.Length == 13) { // Actualizar UI de forma segura barcodeText.Text = $"Scanned: {result.Text}"; Dispatcher.Invoke(() => { if (!_scannedBarcodes.Contains(result.Text)) { _scannedBarcodes.Add(result.Text); AddBarcodeToList(result.Text); SaveBarcode(result.Text); _soundPlayer.Play(); } else { SelectBarcodeInList(result.Text); //_soundPlayer.Play(); } }); } cameraImage.Source = BitmapToImageSource(bitmap); } } } private void AddBarcodeToList(string barcode) { barcodeList.Items.Add(barcode); } private void SelectBarcodeInList(string barcode) { int index = barcodeList.Items.IndexOf(barcode); if (index != -1) { barcodeList.SelectedIndex = index; barcodeList.ScrollIntoView(barcodeList.SelectedItem); // Asegura que el ítem seleccionado sea visible } } private void LoadScannedBarcodes() { if (File.Exists(FilePath)) { var barcodes = File.ReadAllLines(FilePath).Where(code => code.Length == 13); foreach (var barcode in barcodes) { _scannedBarcodes.Add(barcode); barcodeList.Items.Add(barcode); } } } private Result ScanBarcode(Bitmap bitmap) { var barcodeReader = new BarcodeReaderGeneric { AutoRotate = true, Options = new ZXing.Common.DecodingOptions { TryHarder = true } }; var luminanceSource = new BitmapLuminanceSource(bitmap); var result = barcodeReader.Decode(luminanceSource); return result; } private BitmapSource BitmapToImageSource(Bitmap bitmap) { using (var memory = new MemoryStream()) { bitmap.Save(memory, System.Drawing.Imaging.ImageFormat.Bmp); memory.Position = 0; var bitmapImage = new BitmapImage(); bitmapImage.BeginInit(); bitmapImage.StreamSource = memory; bitmapImage.CacheOption = BitmapCacheOption.OnLoad; bitmapImage.EndInit(); return bitmapImage; } } private void SaveBarcode(string barcode) { string filePath = "scanned_barcodes.txt"; using (StreamWriter sw = new StreamWriter(filePath, true)) { sw.WriteLine(barcode); } } protected override void OnClosed(EventArgs e) { _capture.Release(); _capture.Dispose(); CompositionTarget.Rendering -= UpdateFrame; base.OnClosed(e); } } }