Сканирование книг с помощью C# сканера штрих-кодов
Категория: Штрих-коды; .NET
09 июня 2025



/// <summary>
/// Reads ISBN barcodes from a <see cref="System.Drawing.Bitmap"/>.
/// </summary>
/// <param name="bitmap">A bitmap with barcodes.</param>
public static void ReadIsbnBarcodesFromBitmap(System.Drawing.Bitmap bitmap)
{
// create barcode reader
using (Vintasoft.Barcode.BarcodeReader reader = new Vintasoft.Barcode.BarcodeReader())
{
// specify that reader must search for ISBN barcodes
reader.Settings.ScanBarcodeTypes = Vintasoft.Barcode.BarcodeType.ISBN;
// read barcodes from image
Vintasoft.Barcode.IBarcodeInfo[] infos = Vintasoft.Barcode.GdiExtensions.ReadBarcodes(reader, bitmap);
// if barcodes are not detected
if (infos.Length == 0)
{
System.Console.WriteLine("No barcodes found.");
}
// if barcodes are detected
else
{
// get information about extracted barcodes
System.Console.WriteLine(string.Format("{0} barcodes found:", infos.Length));
System.Console.WriteLine();
for (int i = 0; i < infos.Length; i++)
{
Vintasoft.Barcode.IBarcodeInfo info = infos[i];
System.Console.WriteLine(string.Format("[{0}:{1}]", i + 1, info.BarcodeType));
System.Console.WriteLine(string.Format("Value: {0}", info.Value));
System.Console.WriteLine(string.Format("Region: {0}", info.Region));
System.Console.WriteLine();
}
}
}
}
public partial class IsbnBarcodeScannerForm : Form
{
/// <summary>
/// The camera barcode scanner.
/// </summary>
ImagingCameraBarcodeScanner _cameraBarcodeScanner;
/// <summary>
/// Initializes a new instance of the <see cref="IsbnBarcodeScannerForm"/> class.
/// </summary>
public IsbnBarcodeScannerForm()
{
InitializeComponent();
_cameraBarcodeScanner = new ImagingCameraBarcodeScanner();
_cameraBarcodeScanner.BarcodeScanner.FrameScanFinished += BarcodeScanner_FrameScanFinished;
_cameraBarcodeScanner.ScanningException += CameraBarcodeScanner_ScanningException;
_cameraBarcodeScanner.BarcodeScanner.ScannerSettings.ScanBarcodeTypes = Vintasoft.Barcode.BarcodeType.QR;
}
/// <summary>
/// Starts the capturing of images from seleced device and recognition of barcodes in captured images.
/// </summary>
private void startButton_Click(object sender, EventArgs e)
{
textBox1.Text = "Scan Barcodes Types: " + _cameraBarcodeScanner.BarcodeScanner.ScannerSettings.ScanBarcodeTypes.ToString();
_cameraBarcodeScanner.CaptureDevice = _cameraBarcodeScanner.CaptureDevices[0];
_cameraBarcodeScanner.StartScanning();
}
/// <summary>
/// Stops the capturing of images from seleced device and recognition of barcodes in captured images.
/// </summary>
private void stopButton_Click(object sender, EventArgs e)
{
textBox1.Text = "";
_cameraBarcodeScanner.StopScanning();
}
/// <summary>
/// Shows an error.
/// </summary>
private void CameraBarcodeScanner_ScanningException(object sender, Vintasoft.Imaging.ExceptionEventArgs e)
{
MessageBox.Show(e.Exception.ToString());
}
/// <summary>
/// Handles the FrameScanFinished event of the BarcodeScanner.
/// </summary>
private void BarcodeScanner_FrameScanFinished(object sender, Vintasoft.Barcode.FrameScanFinishedEventArgs e)
{
if (e.FoundBarcodes.Length > 0)
Invoke(new ShowRecognitionResultsDelegate(ShowRecognitionResults), new object[] { e.FoundBarcodes });
}
/// <summary>
/// Shows barcode recognition results.
/// </summary>
private void ShowRecognitionResults(Vintasoft.Barcode.IBarcodeInfo[] barcodes)
{
StringBuilder result = new StringBuilder();
foreach (Vintasoft.Barcode.IBarcodeInfo barcode in barcodes)
result.AppendLine(string.Format("{0}: {1}", barcode.BarcodeType, barcode.Value));
// show barcode recognition in text box
textBox1.Text = result.ToString();
}
delegate void ShowRecognitionResultsDelegate(Vintasoft.Barcode.IBarcodeInfo[] barcodeInfo);
}
/// <summary>
/// Allows to capture images from DirectShow camera (VintaSoft Imaging .NET SDK) and recognize barcodes in captured images (VintaSoft Barcode .NET SDK).
/// </summary>
public class ImagingCameraBarcodeScanner : IDisposable
{
#region Fields
/// <summary>
/// Monitor for capture devices.
/// </summary>
Vintasoft.Imaging.Media.ImageCaptureDevicesMonitor _captureDevicesMonitor;
/// <summary>
/// Image capture source for barcode recognition.
/// </summary>
Vintasoft.Imaging.Media.ImageCaptureSource _imageCaptureSource;
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="ImagingCameraBarcodeScanner"/> class.
/// </summary>
public ImagingCameraBarcodeScanner()
{
_barcodeScanner = new Vintasoft.Barcode.CameraBarcodeScanner();
_barcodeScanner.FrameScanException += BarcodeScanner_FrameScanException;
_barcodeScanner.ScannerSettings.ScanBarcodeTypes = Vintasoft.Barcode.BarcodeType.ISBN;
_captureDevicesMonitor = new Vintasoft.Imaging.Media.ImageCaptureDevicesMonitor();
_captureDevicesMonitor.CaptureDevicesChanged += CaptureDevicesMonitor_CaptureDevicesChanged;
if (!Vintasoft.Imaging.ImagingEnvironment.IsInDesignMode)
_captureDevicesMonitor.Start();
_imageCaptureSource = new Vintasoft.Imaging.Media.ImageCaptureSource();
_imageCaptureSource.CaptureCompleted += ImageCaptureSource_CaptureCompleted;
}
#endregion
#region Properties
Vintasoft.Imaging.Media.ImageCaptureDevice _captureDevice;
/// <summary>
/// Gets or sets current capture device.
/// </summary>
public Vintasoft.Imaging.Media.ImageCaptureDevice CaptureDevice
{
get
{
return _captureDevice;
}
set
{
if (_captureDevice != value)
{
_captureDevice = value;
UpdateCapureFormats();
}
}
}
ReadOnlyCollection<Vintasoft.Imaging.Media.ImageCaptureFormat> _captureFormats = null;
/// <summary>
/// Gets the available capture formats of current capture device.
/// </summary>
/// <seealso cref="CaptureDevice"/>
public ReadOnlyCollection<Vintasoft.Imaging.Media.ImageCaptureFormat> CaptureFormats
{
get
{
return _captureFormats;
}
}
/// <summary>
/// Gets available capture devices.
/// </summary>
public ReadOnlyCollection<Vintasoft.Imaging.Media.ImageCaptureDevice> CaptureDevices
{
get
{
return Vintasoft.Imaging.Media.ImageCaptureDeviceConfiguration.GetCaptureDevices();
}
}
Vintasoft.Barcode.CameraBarcodeScanner _barcodeScanner;
/// <summary>
/// Gets the barcode scanner.
/// </summary>
public Vintasoft.Barcode.CameraBarcodeScanner BarcodeScanner
{
get
{
return _barcodeScanner;
}
}
/// <summary>
/// Gets a value indicating whether barcode scanning is started.
/// </summary>
public bool IsStarted
{
get
{
return _imageCaptureSource.State == Vintasoft.Imaging.Media.ImageCaptureState.Started;
}
}
#endregion
#region Methods
#region PUBLIC
/// <summary>
/// Starts the capturing of images from seleced device and recognition of barcodes in captured images.
/// </summary>
public void StartScanning()
{
try
{
if (CaptureDevice == null)
throw new ArgumentNullException("CaptureDevice");
if (CaptureFormats == null || CaptureFormats.Count == 0)
throw new ArgumentNullException("CaptureFormats");
if (_imageCaptureSource.State != Vintasoft.Imaging.Media.ImageCaptureState.Started)
{
if (CaptureDevice.DesiredFormat == null)
CaptureDevice.DesiredFormat = CaptureFormats[0];
_barcodeScanner.StartScanning();
_imageCaptureSource.CaptureDevice = _captureDevice;
_imageCaptureSource.Start();
_imageCaptureSource.CaptureAsync();
OnScanningStart(EventArgs.Empty);
}
}
catch (Exception ex)
{
OnScanningException(new Vintasoft.Imaging.ExceptionEventArgs(ex));
}
}
/// <summary>
/// Stops the capturing of images from seleced device and recognition of barcodes in captured images.
/// </summary>
public void StopScanning()
{
try
{
if (_imageCaptureSource.State != Vintasoft.Imaging.Media.ImageCaptureState.Stopped)
{
_barcodeScanner.StopScanning();
_imageCaptureSource.Stop();
OnScanningStop(EventArgs.Empty);
}
}
catch (Exception ex)
{
OnScanningException(new Vintasoft.Imaging.ExceptionEventArgs(ex));
}
}
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public void Dispose()
{
_captureDevicesMonitor.CaptureDevicesChanged -= CaptureDevicesMonitor_CaptureDevicesChanged;
_barcodeScanner.BarcodeReader.Dispose();
}
#endregion
#region PROTECTED
/// <summary>
/// Raises the <see cref="CaptureDevicesChanged" /> event.
/// </summary>
/// <param name="args">The <see cref="ImageCaptureDevicesChangedEventArgs"/> instance containing the event data.</param>
protected virtual void OnCaptureDevicesChanged(Vintasoft.Imaging.Media.ImageCaptureDevicesChangedEventArgs args)
{
if (CaptureDevicesChanged != null)
CaptureDevicesChanged(this, args);
}
/// <summary>
/// Raises the <see cref="ScanningException" /> event.
/// </summary>
/// <param name="args">The <see cref="ExceptionEventArgs"/> instance containing the event data.</param>
protected virtual void OnScanningException(Vintasoft.Imaging.ExceptionEventArgs args)
{
if (ScanningException != null)
ScanningException(this, args);
}
/// <summary>
/// Raises the <see cref="ScanningStart" /> event.
/// </summary>
/// <param name="args">The <see cref="EventArgs"/> instance containing the event data.</param>
protected virtual void OnScanningStart(EventArgs args)
{
if (ScanningStart != null)
ScanningStart(this, args);
}
/// <summary>
/// Raises the <see cref="ScanningStop" /> event.
/// </summary>
/// <param name="args">The <see cref="EventArgs"/> instance containing the event data.</param>
protected virtual void OnScanningStop(EventArgs args)
{
if (ScanningStop != null)
ScanningStop(this, args);
}
#endregion
#region PRIVATE
/// <summary>
/// Handles the FrameScanException event of the BarcodeScanner control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="Vintasoft.Barcode.FrameScanExceptionEventArgs"/> instance containing the event data.</param>
private void BarcodeScanner_FrameScanException(object sender, Vintasoft.Barcode.FrameScanExceptionEventArgs e)
{
OnScanningException(new Vintasoft.Imaging.ExceptionEventArgs(e.Exception));
}
/// <summary>
/// Handles the CaptureCompleted event of the ImageCaptureSource.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="ImageCaptureCompletedEventArgs"/> instance containing the event data.</param>
private void ImageCaptureSource_CaptureCompleted(object sender, Vintasoft.Imaging.Media.ImageCaptureCompletedEventArgs e)
{
try
{
// get captured image
Vintasoft.Imaging.VintasoftImage image = e.GetCapturedImage();
// recognize barcodes from captured image
using (Vintasoft.Imaging.VintasoftBitmap bitmap = image.GetAsVintasoftBitmap())
_barcodeScanner.ScanFrame(bitmap);
// if image capturing is started
if (_imageCaptureSource.State == Vintasoft.Imaging.Media.ImageCaptureState.Started)
{
// capture next image
_imageCaptureSource.CaptureAsync();
}
}
catch (Exception ex)
{
OnScanningException(new Vintasoft.Imaging.ExceptionEventArgs(ex));
}
}
/// <summary>
/// Handles the CaptureDevicesChanged event of the CaptureDevicesMonitor.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="ImageCaptureDevicesChangedEventArgs"/> instance containing the event data.</param>
private void CaptureDevicesMonitor_CaptureDevicesChanged(object sender, Vintasoft.Imaging.Media.ImageCaptureDevicesChangedEventArgs e)
{
if (Array.IndexOf(e.RemovedDevices, CaptureDevice) >= 0)
{
StopScanning();
}
OnCaptureDevicesChanged(e);
}
/// <summary>
/// Updates the capture formats.
/// </summary>
private void UpdateCapureFormats()
{
Vintasoft.Imaging.Media.ImageCaptureDevice device = CaptureDevice;
if (device == null || device.SupportedFormats == null)
{
_captureFormats = null;
}
else
{
List<Vintasoft.Imaging.Media.ImageCaptureFormat> captureFormats = new List<Vintasoft.Imaging.Media.ImageCaptureFormat>();
List<uint> imageCaptureFormatSizes = new List<uint>();
for (int i = 0; i < device.SupportedFormats.Count; i++)
{
// if format has bit depth less or equal than 12 bit
if (device.SupportedFormats[i].BitsPerPixel <= 12)
{
// ignore formats with bit depth less or equal than 12 bit because they may cause issues on Windows 8
continue;
}
uint imageCaptureFormatSize = (uint)(device.SupportedFormats[i].Width | (device.SupportedFormats[i].Height << 16));
if (!imageCaptureFormatSizes.Contains(imageCaptureFormatSize))
{
imageCaptureFormatSizes.Add(imageCaptureFormatSize);
captureFormats.Add(device.SupportedFormats[i]);
}
}
_captureFormats = captureFormats.AsReadOnly();
}
}
#endregion
#endregion
#region Events
/// <summary>
/// Occurs when barcode scanning is started.
/// </summary>
public event EventHandler ScanningStart;
/// <summary>
/// Occurs when barcode scanning is stopped.
/// </summary>
public event EventHandler ScanningStop;
/// <summary>
/// Occurs when barcode scanning error occurs.
/// </summary>
public event EventHandler<Vintasoft.Imaging.ExceptionEventArgs> ScanningException;
/// <summary>
/// Occurs when <see cref="CaptureDevices"/> property is changed.
/// </summary>
public event EventHandler<Vintasoft.Imaging.Media.ImageCaptureDevicesChangedEventArgs> CaptureDevicesChanged;
#endregion
}