cdiego

Leer códigos QR C#

Nov 11th, 2013
3,970
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.09 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using AForge.Video.DirectShow;
  10. using BarcodeLib.BarcodeReader;
  11.  
  12. namespace LeerQR
  13. {
  14.     public partial class Form1 : Form
  15.     {
  16.         public Form1()
  17.         {
  18.             InitializeComponent();
  19.         }
  20.         /**************
  21.          *
  22.         LEER CODIGOS QR CON WEBCAM
  23.          *
  24.          * importar libreria barcodelib
  25.         **************/
  26.         //AGREGAR USING*/
  27.         //VARIABLE PARA LISTA DE DISPOSITIVOS
  28.         private FilterInfoCollection Dispositivos;
  29.         //VARIABLE PARA FUENTE DE VIDEO
  30.         private VideoCaptureDevice FuenteDeVideo;
  31.         private void Form1_Load(object sender, EventArgs e)
  32.         {
  33.             //LISTAR DISPOSITIVOS DE ENTRADA DE VIDEO
  34.             Dispositivos = new FilterInfoCollection(FilterCategory.VideoInputDevice);
  35.             //CARGAR TODOS LOS DISPOSITIVOS AL COMBO
  36.             foreach (FilterInfo x in Dispositivos)
  37.             {
  38.                 comboBox1.Items.Add(x.Name);
  39.             }
  40.             comboBox1.SelectedIndex = 0;
  41.         }
  42.         private void button1_Click(object sender, EventArgs e)
  43.         {
  44.             //EMPEZAR A LEER
  45.             timer1.Enabled = true;
  46.  
  47.             //ESTABLECER EL DISPOSITIVO SELECCIONADO COMO FUENTE DE VIDEO
  48.             FuenteDeVideo = new VideoCaptureDevice(Dispositivos[comboBox1.SelectedIndex].MonikerString);
  49.             //INICIALIZAR EL CONTROL
  50.             videoSourcePlayer1.VideoSource = FuenteDeVideo;
  51.             //INICIAR RECEPCION DE IMAGENES
  52.             videoSourcePlayer1.Start();
  53.         }
  54.  
  55.         private void button2_Click(object sender, EventArgs e)
  56.         {
  57.             //DEJAR DE LEER
  58.             timer1.Enabled = false;
  59.            
  60.            
  61.             //DETENER RECEPCION DE IMAGENES
  62.             videoSourcePlayer1.SignalToStop();
  63.         }
  64.  
  65.         private void timer1_Tick(object sender, EventArgs e)
  66.         {
  67.             //ESTAR SEGUROS QUE HAY UNA IMAGEN DESDE LA WEBCAM
  68.             if (videoSourcePlayer1.GetCurrentVideoFrame() != null)
  69.             {
  70.                 //IBTENER IMAGEN DE LA WEBCAM
  71.                 Bitmap img = new Bitmap(videoSourcePlayer1.GetCurrentVideoFrame());
  72.                 //UTILIZAR LA LIBRERIA Y LEER EL CÓDIGO
  73.                 string[] resultados = BarcodeReader.read(img, BarcodeReader.QRCODE);
  74.                 //QUITAR LA IMAGEN DE MEMORIA
  75.                 img.Dispose();
  76.                 //OBTENER LAS LECTURAS CUANDO SE LEA ALGO
  77.                 if (resultados != null && resultados.Count() > 0)
  78.                 {
  79.                     //AGREGAR EL TEXTO OBTENIDO A LA LISTA
  80.                     if (resultados[0].IndexOf("1111") != -1)
  81.                     {
  82.                         //QUITAR EL CODIGO DE VERIFICACION
  83.                         resultados[0] = resultados[0].Replace("1111", "");
  84.                         listBox1.Items.Add(resultados[0]);
  85.                     }
  86.                 }
  87.             }
  88.         }
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment