Programm_Wirl/AA-Forms/FormKamera.cs
2025-12-03 09:05:30 +01:00

129 lines
3.4 KiB
C#

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;
}
/// <summary>
/// PictureBox mit Live Bild der Kamera laden und anzeigen.
/// </summary>
/// <param name="sender"></param>
/// <param name="eventArgs"></param>
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();
}));
}
}
}
/// <summary>
/// Die Bitmap wir Horizontal geflippt damit die Orientierung beim QR-Scann besser ist.
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
private Bitmap FlipHorizontal(Bitmap source)
{
Bitmap flipped = (Bitmap)source.Clone();
flipped.RotateFlip(RotateFlipType.RotateNoneFlipX); // ✅ Funktioniert!
return flipped;
}
/// <summary>
/// Scannen wird hier gestoppt.
/// </summary>
private void StopScanning()
{
if (videoSource != null)
{
videoSource.SignalToStop();
videoSource = null;
isScanning = false;
}
}
/// <summary>
/// Wenn QR-Code scannen nicht möglich ist, kann händische Eingabe gewählt werden.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonHand_Click(object sender, EventArgs e)
{
StopScanning();
this.DialogResult = DialogResult.No;
this.Close();
}
/// <summary>
/// Scanning beenden falls Form geschlossen wird.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void FormKamera_FormClosing(object sender, FormClosingEventArgs e)
{
if (isScanning) { StopScanning(); this.DialogResult = DialogResult.Cancel; }
}
}
}