using AForge.Video; using AForge.Video.DirectShow; using DatenDB; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using ZXing; namespace Deckungsbeitrag { public partial class FormKamera : Form { public Kunde kunde; //QR-Code Scan mit integrierter Kamera private VideoCaptureDevice videoSource; private FilterInfoCollection videoDevices; private BarcodeReader barcodeReader; private bool isScanning = false; public FormKamera() { InitializeComponent(); } public FormKamera(FilterInfoCollection videoDevices) : this() { this.videoDevices = videoDevices; } private void FormKamera_Load(object sender, EventArgs e) { barcodeReader = new BarcodeReader(); videoSource = new VideoCaptureDevice(videoDevices[0].MonikerString); videoSource.NewFrame += VideoSource_NewFrame; videoSource.Start(); isScanning = true; } /// /// PictureBox mit Live Bild der Kamera laden und anzeigen. /// /// /// private void VideoSource_NewFrame(object sender, NewFrameEventArgs eventArgs) { using (Bitmap unflippedbitmap = (Bitmap)eventArgs.Frame.Clone()) { Bitmap bitmap = FlipHorizontal(unflippedbitmap); pictureBoxKamera.Image?.Dispose(); pictureBoxKamera.Image = (Bitmap)bitmap.Clone(); var result = barcodeReader.Decode(bitmap); if (result != null && result.BarcodeFormat == BarcodeFormat.QR_CODE) { this.Invoke(new Action(() => { this.kunde = Kunde.GetKunde(result.Text, null, null); // Direkt in TextBox schreiben pictureBoxKamera.Image?.Dispose(); StopScanning(); System.Media.SystemSounds.Asterisk.Play(); this.DialogResult = DialogResult.OK; this.Close(); })); } } } /// /// Die Bitmap wir Horizontal geflippt damit die Orientierung beim QR-Scann besser ist. /// /// /// private Bitmap FlipHorizontal(Bitmap source) { Bitmap flipped = (Bitmap)source.Clone(); flipped.RotateFlip(RotateFlipType.RotateNoneFlipX); // ✅ Funktioniert! return flipped; } /// /// Scannen wird hier gestoppt. /// private void StopScanning() { if (videoSource != null) { videoSource.SignalToStop(); videoSource = null; isScanning = false; } } /// /// Wenn QR-Code scannen nicht möglich ist, kann händische Eingabe gewählt werden. /// /// /// private void buttonHand_Click(object sender, EventArgs e) { StopScanning(); this.DialogResult = DialogResult.No; this.Close(); } /// /// Scanning beenden falls Form geschlossen wird. /// /// /// private void FormKamera_FormClosing(object sender, FormClosingEventArgs e) { if (isScanning) { StopScanning(); this.DialogResult = DialogResult.Cancel; } } } }