Autore Topic: [C] Leggere codici a barre con GStreamer mediante una webcam  (Letto 568 volte)

Offline vuott

  • Moderatore globale
  • Senatore Gambero
  • *****
  • Post: 11.179
  • Ne mors quidem nos iunget
    • Mostra profilo
Il sistema multimediale "GStreamer" offre, fra l'altro, la possibilità di leggere i codici a barre mediante una webcam.
Il particolare plugin di GStreamer, chiamato "ZBAR", restituirà il tipo ed il codice.

Mostro di seguito un semplice codice da me scritto in C:

Codice: [Seleziona]
#include <stdio.h>
#include <gst/gst.h>

gint cnt = 0;

gboolean gestione_messaggio_bus (GstBus * bus, GstMessage * messaggio, gpointer data) {

GMainLoop *loop = (GMainLoop *)data;

switch (messaggio->type) {
case GST_MESSAGE_ELEMENT:  {
cnt++;
g_message ("Messaggio n. %d: %s    qualita': %d\n Tipo: %s  -  Codice: %s", cnt, gst_structure_get_name (gst_message_get_structure(messaggio)),
g_value_get_int (gst_structure_get_value(gst_message_get_structure (messaggio), "quality")),
g_value_get_string (gst_structure_get_value(gst_message_get_structure (messaggio), "type")),
g_value_get_string (gst_structure_get_value(gst_message_get_structure (messaggio), "symbol")) );
                        /* g_print("%s\n", gst_structure_to_string(gst_message_get_structure(messaggio)); */
break;
}
case GST_MESSAGE_WARNING:
break;
      case GST_MESSAGE_EOS:
case GST_MESSAGE_ERROR: {
g_message ("Messaggio n. %d: Errore !", cnt);
g_main_loop_quit (loop);
break;
}
default:
break;
}
return TRUE;
}


int main() {

GMainLoop *loop;
GstElement *pipeline;
GstBus *bus;

gst_init (NULL, NULL);

loop = g_main_loop_new (NULL, FALSE);
pipeline = gst_parse_launch (" v4l2src ! videoconvert ! zbar ! videoconvert ! xvimagesink", NULL);

bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
gst_bus_add_watch (bus, gestione_messaggio_bus, loop);

gst_element_set_state (pipeline, GST_STATE_PLAYING);

g_main_loop_run (loop);

gst_element_set_state (pipeline, GST_STATE_NULL);
gst_object_unref (pipeline);
gst_object_unref (bus);

return 0;

}

Va compilato con: `pkg-config --cflags --libs gstreamer-1.0`

Come utilizzarlo:
- collegare una webcam al computer;
- lanciare il codice;
- avvicinare (...non troppo) il codice a barre alla webcam, cercando (...con pazienza) la distanza e l'inclinazione ottimali.



Per vedere anche una versione per Gambas:
http://www.gambas-it.org/smf/index.php?topic=5856
« Chiunque, non ricorrendo lo stato di necessità, nel proprio progetto Gambas fa uso delle istruzioni Shell o Exec, è punito con la sanzione pecuniaria da euro 20,00 a euro 60,00. »