Ejemplo n.º 1
0
static inline void
expect (zbar::zbar_symbol_type_t type,
        std::string data)
{
    if(expect_type)
        error(std::string("missing: ") + zbar_get_symbol_name(expect_type) +
              " " + expect_data);
    expect_type = type;
    expect_data = data;
}
Ejemplo n.º 2
0
static inline int zbar_gtk_process_image (ZBarGtk *self,
                                          zbar_image_t *image)
{
    ZBarGtkPrivate *zbar = ZBAR_GTK_PRIVATE(self->_private);

    if(!image)
        return(-1);

    zbar_image_t *tmp = zbar_image_convert(image, fourcc('Y','8','0','0'));
    if(!tmp)
        return(-1);

    zbar_image_scanner_recycle_image(zbar->scanner, image);
    int rc = zbar_scan_image(zbar->scanner, tmp);
    zbar_image_set_symbols(image, zbar_image_get_symbols(tmp));
    zbar_image_destroy(tmp);
    if(rc < 0)
        return(rc);

    gdk_threads_enter();

    if(rc && zbar->thread) {
        /* update decode results */
        const zbar_symbol_t *sym;
        for(sym = zbar_image_first_symbol(image);
            sym;
            sym = zbar_symbol_next(sym))
            if(!zbar_symbol_get_count(sym)) {
                zbar_symbol_type_t type = zbar_symbol_get_type(sym);
                const char *data = zbar_symbol_get_data(sym);
                g_signal_emit(self, zbar_gtk_signals[DECODED], 0,
                              type, data);

                /* FIXME skip this when unconnected? */
                gchar *text = g_strconcat(zbar_get_symbol_name(type),
                                          zbar_get_addon_name(type),
                                          ":",
                                          data,
                                          NULL);
                g_signal_emit(self, zbar_gtk_signals[DECODED_TEXT], 0, text);
                g_free(text);
            }
    }

    if(zbar->window) {
        rc = zbar_window_draw(zbar->window, image);
        gtk_widget_queue_draw(GTK_WIDGET(self));
    }
    else
        rc = -1;
    gdk_threads_leave();
    return(rc);
}
Ejemplo n.º 3
0
static void my_handler (zbar_image_t *image,
                        const void *userdata)
{
    /* extract results */
    const zbar_symbol_t *symbol = zbar_image_first_symbol(image);
    for(; symbol; symbol = zbar_symbol_next(symbol)) {
        /* do something useful with results */
        zbar_symbol_type_t typ = zbar_symbol_get_type(symbol);
        const char *data = zbar_symbol_get_data(symbol);
        printf("decoded %s symbol \"%s\"\n",
               zbar_get_symbol_name(typ), data);
    }
}
Ejemplo n.º 4
0
/* decode signal callback
 * puts the decoded result in the textbox
 */
static void decoded (GtkWidget *widget,
                     zbar_symbol_type_t symbol,
                     const char *result,
                     gpointer data)
{
    GtkTextBuffer *resultbuf = gtk_text_view_get_buffer(results);
    GtkTextIter end;
    gtk_text_buffer_get_end_iter(resultbuf, &end);
    gtk_text_buffer_insert(resultbuf, &end, zbar_get_symbol_name(symbol), -1);
    gtk_text_buffer_insert(resultbuf, &end, ":", -1);
    gtk_text_buffer_insert(resultbuf, &end, result, -1);
    gtk_text_buffer_insert(resultbuf, &end, "\n", -1);
    gtk_text_view_scroll_to_iter(results, &end, 0, FALSE, 0, 0);
}
Ejemplo n.º 5
0
char *zbar_symbol_xml (const zbar_symbol_t *sym,
                       char **buf,
                       unsigned *len)
{
    const char *type = zbar_get_symbol_name(sym->type);
    /* FIXME binary data */
    unsigned datalen = strlen(sym->data);
    unsigned maxlen = (strlen(xmlfmt[0]) + strlen(xmlfmt[1]) +
                       strlen(xmlfmt[2]) + strlen(xmlfmt[3]) +
                       strlen(type) + datalen + MAX_INT_DIGITS + 1);
    if(!*buf || (*len < maxlen)) {
        if(*buf)
            free(*buf);
        *buf = (char*)malloc(maxlen);
        /* FIXME check OOM */
        *len = maxlen;
    }

    int n = snprintf(*buf, maxlen, xmlfmt[0], type, sym->quality);
    assert(n > 0);
    assert(n <= maxlen);

    if(sym->cache_count) {
        int i = snprintf(*buf + n, maxlen - n, xmlfmt[1], sym->cache_count);
        assert(i > 0);
        n += i;
        assert(n <= maxlen);
    }

    int i = strlen(xmlfmt[2]);
    memcpy(*buf + n, xmlfmt[2], i + 1);
    n += i;
    assert(n <= maxlen);

    /* FIXME binary data */
    /* FIXME handle "]]>" */
	strncpy_s(*buf + n, maxlen - n, sym->data, datalen + 1);

    n += datalen;
    assert(n <= maxlen);

    i = strlen(xmlfmt[3]);
    memcpy(*buf + n, xmlfmt[3], i + 1);
    n += i;
    assert(n <= maxlen);

    *len = n;
    return(*buf);
}
Ejemplo n.º 6
0
int main (int argc, const char *argv[])
{
  processor = zbar_processor_create(0);
  assert(processor);
  if(zbar_processor_init(processor, NULL, 0)) {
      zbar_processor_error_spew(processor, 0);
      return(1);
  }

  zbar_image_t *zimage = zbar_image_create();
  assert(zimage);
  zbar_image_set_format(zimage, *(unsigned long*)"Y800");

  int width = js_get_width();
  int height = js_get_height();
  zbar_image_set_size(zimage, width, height);

  size_t bloblen = width * height;
  unsigned char *blob = malloc(bloblen);
  zbar_image_set_data(zimage, blob, bloblen, zbar_image_free_data);

  js_read_image(blob, bloblen);

  zbar_process_image(processor, zimage);

  // print results
  const zbar_symbol_t *sym = zbar_image_first_symbol(zimage);
  for(; sym; sym = zbar_symbol_next(sym)) {
      zbar_symbol_type_t typ = zbar_symbol_get_type(sym);
      if(typ == ZBAR_PARTIAL)
          continue;
      js_output_result(zbar_get_symbol_name(typ),
                       zbar_get_addon_name(typ),
                       zbar_symbol_get_data(sym));
  }

  zbar_image_destroy(zimage);

  if(zbar_processor_is_visible(processor)) {
     zbar_processor_user_wait(processor, -1);
  }

  zbar_processor_destroy(processor);

  return 0;
}
Ejemplo n.º 7
0
static void data_handler (zbar_image_t *img, const void *userdata)
{
    const zbar_symbol_t *sym = zbar_image_first_symbol(img);
    assert(sym);
    int n = 0;
    for(; sym; sym = zbar_symbol_next(sym)) {
        if(zbar_symbol_get_count(sym))
            continue;

        zbar_symbol_type_t type = zbar_symbol_get_type(sym);
        if(type == ZBAR_PARTIAL)
            continue;

        if(!format) {
            printf("%s:", zbar_get_symbol_name(type));
            if(fwrite(zbar_symbol_get_data(sym),
                      zbar_symbol_get_data_length(sym),
                      1, stdout) != 1)
                continue;
        }
        else if(format == RAW) {
            if(fwrite(zbar_symbol_get_data(sym),
                      zbar_symbol_get_data_length(sym),
                      1, stdout) != 1)
                continue;
        }
        else if(format == XML) {
            if(!n)
                printf("<index num='%u'>\n", zbar_image_get_sequence(img));
            zbar_symbol_xml(sym, &xml_buf, &xml_len);
            if(fwrite(xml_buf, xml_len, 1, stdout) != 1)
                continue;
        }
        printf("\n");
        n++;
    }

    if(format == XML && n)
        printf("</index>\n");
    fflush(stdout);

    if(!quiet && n)
        fprintf(stderr, BELL);
}
Ejemplo n.º 8
0
int main (int argc, char **argv)
{
    if(argc < 2) return(1);

    /* create a reader */
    scanner = zbar_image_scanner_create();

    /* configure the reader */
    zbar_image_scanner_set_config(scanner, 0, ZBAR_CFG_ENABLE, 1);

    /* obtain image data */
    int width = 1920, height = 1080;
    void *raw = NULL;
    get_yuv_data(argv[1], &width, &height, &raw);

    /* wrap image data */
    zbar_image_t *image = zbar_image_create();
    zbar_image_set_format(image, *(int*)"Y800");
    zbar_image_set_size(image, width, height);
    zbar_image_set_data(image, raw, width * height, zbar_image_free_data);

    /* scan the image for barcodes */
    int n = zbar_scan_image(scanner, image);
    if(0==n||-1==n)
    {
      printf("no symbols were found or -1 if an error occurs\n");
    }
    /* extract results */
    const zbar_symbol_t *symbol = zbar_image_first_symbol(image);
    for(; symbol; symbol = zbar_symbol_next(symbol)) {
        /* do something useful with results */
        zbar_symbol_type_t typ = zbar_symbol_get_type(symbol);
        const char *data = zbar_symbol_get_data(symbol);
        printf("decoded %s symbol \"%s\"\n",
               zbar_get_symbol_name(typ), data);
    }

    /* clean up */
    zbar_image_destroy(image);
    zbar_image_scanner_destroy(scanner);

    return(0);
}
Ejemplo n.º 9
0
static int scan_image (const char *filename)
{
    if(exit_code == 3)
        return(-1);

    int found = 0;
    MagickWand *images = NewMagickWand();
    if(!MagickReadImage(images, filename) && dump_error(images))
        return(-1);

    unsigned seq, n = MagickGetNumberImages(images);
    for(seq = 0; seq < n; seq++) {
        if(exit_code == 3)
            return(-1);

        if(!MagickSetIteratorIndex(images, seq) && dump_error(images))
            return(-1);

        zbar_image_t *zimage = zbar_image_create();
        assert(zimage);
        zbar_image_set_format(zimage, zbar_fourcc('Y','8','0','0'));

        int width = MagickGetImageWidth(images);
        int height = MagickGetImageHeight(images);
        zbar_image_set_size(zimage, width, height);

        // extract grayscale image pixels
        // FIXME color!! ...preserve most color w/422P
        // (but only if it's a color image)
        size_t bloblen = width * height;
        unsigned char *blob = malloc(bloblen);
        zbar_image_set_data(zimage, blob, bloblen, zbar_image_free_data);

        if(!MagickExportImagePixels(images, 0, 0, width, height,
                                    "I", CharPixel, blob))
            return(-1);

        if(xmllvl == 1) {
            xmllvl++;
            printf("<source href='%s'>\n", filename);
        }

        zbar_process_image(processor, zimage);

        // output result data
        const zbar_symbol_t *sym = zbar_image_first_symbol(zimage);
        for(; sym; sym = zbar_symbol_next(sym)) {
            zbar_symbol_type_t typ = zbar_symbol_get_type(sym);
            unsigned len = zbar_symbol_get_data_length(sym);
            if(typ == ZBAR_PARTIAL)
                continue;
            else if(xmllvl <= 0) {
                if(!xmllvl)
                    printf("%s:", zbar_get_symbol_name(typ));
                if(len &&
                   fwrite(zbar_symbol_get_data(sym), len, 1, stdout) != 1) {
                    exit_code = 1;
                    return(-1);
                }
            }
            else {
                if(xmllvl < 3) {
                    xmllvl++;
                    printf("<index num='%u'>\n", seq);
                }
                zbar_symbol_xml(sym, &xmlbuf, &xmlbuflen);
                if(fwrite(xmlbuf, xmlbuflen, 1, stdout) != 1) {
                    exit_code = 1;
                    return(-1);
                }
            }
            printf("\n");
            found++;
            num_symbols++;
        }
        if(xmllvl > 2) {
            xmllvl--;
            printf("</index>\n");
        }
        fflush(stdout);

        zbar_image_destroy(zimage);

        num_images++;
        if(zbar_processor_is_visible(processor)) {
            int rc = zbar_processor_user_wait(processor, -1);
            if(rc < 0 || rc == 'q' || rc == 'Q')
                exit_code = 3;
        }
    }

    if(xmllvl > 1) {
        xmllvl--;
        printf("</source>\n");
    }

    if(!found)
        notfound++;

    DestroyMagickWand(images);
    return(0);
}
Ejemplo n.º 10
0
static int qr_detect_img(char *img)
{
    int ret = -1;
    zbar_image_set_data(zimg, img, 640*360, NULL);
    zbar_scan_image(zscn, zimg);

    const zbar_symbol_t *symbol = zbar_image_first_symbol(zimg);

    //for(; symbol; symbol = zbar_symbol_next(symbol))
    if (symbol)
    {
        zbar_symbol_type_t typ = zbar_symbol_get_type(symbol);
        const char *data = zbar_symbol_get_data(symbol);
        DBG("decoded %s symbol \"%s\"\n", zbar_get_symbol_name(typ), data);

        char ssid[128];
        memset(ssid, 0, sizeof(ssid));
        char pwd[128];
        memset(pwd, 0, sizeof(pwd));
        char sequence[128];
        memset(sequence, 0, sizeof(sequence));

        qr_str_split(data, "ssid:", "\n", ssid, sizeof(ssid));
        DBG("ssid: %s\n", ssid);
        qr_str_split(data, "pwd:", "\n", pwd, sizeof(pwd));
        DBG("pwd: %s\n", pwd);
        qr_str_split(data, "sequenceid:", "\n", sequence, sizeof(sequence));
        DBG("sequenceid: %s\n", sequence);

        char buffer[1024];
        memset(buffer, 0, sizeof(buffer));

        if (strlen(ssid) && strlen(pwd))
        {
            memset(buffer, 0, sizeof(buffer));
            sprintf(buffer, "ctrl_interface=/var/run/wpa_supplicant\n"
                            "network={\n"
                            "ssid=\"%s\"\n"
                            "psk=\"%s\"\n"
                            "}\n",
                            ssid,
                            pwd
                            );

            ret= qr_save2file(path_wpa, buffer, strlen(buffer));
            CHECK(ret == 0, -1, "Error with: %#x\n", ret);
        }

        if (strlen(sequence))
        {
            memset(buffer, 0, sizeof(buffer));
            sprintf(buffer, "%s", sequence);

            ret= qr_save2file(path_sequence, buffer, strlen(buffer));
            CHECK(ret == 0, -1, "Error with: %#x\n", ret);
        }

        ret = 0;
    }

    return ret;
}
Ejemplo n.º 11
0
/**
 * Entry point of the program. The MAMain function
 * needs to be declared as extern "C".
 */
extern "C" int MAMain()
{
	MAEvent event;

	int imgSize = maGetImageSize(RES_BARCODE_IMAGE);

	int imgW = EXTENT_X(imgSize);
	int imgH = EXTENT_Y(imgSize);

	int imgDataSize = imgW * imgH;
	int* imgData = (int*) malloc(imgDataSize * 4);

	MARect imgRect;
	imgRect.left = 0;
	imgRect.top = 0;
	imgRect.width = imgW;
	imgRect.height = imgH;

	maGetImageData(RES_BARCODE_IMAGE, imgData, &imgRect, imgW);

	unsigned char* fixedImg = (unsigned char*) malloc(imgDataSize);

	printf("Converting image\n");

	createLuminosity(imgData, fixedImg, imgDataSize);

	printf("Scanning for barcodes\n");

	// create a reader
	zbar::ImageScanner scanner = zbar::zbar_image_scanner_create();

	// configure the reader
	zbar::zbar_image_scanner_set_config(scanner, zbar::ZBAR_NONE, zbar::ZBAR_CFG_ENABLE, 1);

	// wrap image data
	zbar::zbar_image_t *image = zbar::zbar_image_create();
	zbar::zbar_image_set_format(image, 0x30303859);// "Y800" = 0x30303859

	zbar::zbar_image_set_size(image, imgW, imgH);

	zbar::zbar_image_set_data(image, fixedImg, imgW * imgH, NULL);//zbar_image_free_data);

	// scan the image for barcodes
	zbar_scan_image(scanner, image);

	// extract results
	bool result = false;
	const zbar::zbar_symbol_t *symbol = zbar_image_first_symbol(image);
	for(; symbol; symbol = zbar_symbol_next(symbol)) {
		// do something useful with results
		zbar::zbar_symbol_type_t typ = zbar_symbol_get_type(symbol);
		const char *data = zbar_symbol_get_data(symbol);
		printf("decoded %s symbol \"%s\"\n",
			   zbar_get_symbol_name(typ), data);
		result = true;
	}

	// clean up
	zbar_image_destroy(image);

	if(!result)
		printf("No symbols found.\n");

	printf("Press zero, back or touch screen to exit\n");

	while (TRUE)
	{
		maWait(0);
		maGetEvent(&event);

		if (EVENT_TYPE_CLOSE == event.type)
		{
			// Exit while loop.
			break;
		}
		else if (EVENT_TYPE_KEY_PRESSED == event.type)
		{
			if (MAK_BACK == event.key || MAK_0 == event.key)
			{
				// Exit while loop.
				break;
			}
		}
		else if (EVENT_TYPE_POINTER_PRESSED == event.type)
		{
			break;
		}
	}

	return 0;
}
Ejemplo n.º 12
0
void process_QR(IplImage* img, QR_Data * data, IplImage* outimg)
{
	// Data extracted from the ZBar Image
	int width = 0;
	int height = 0;
	void *raw = NULL;

	// Data from the QR code and its position/angle
	int qr_length = 0;		// The length of the code in pixels
	double qr_distance = 0; // How far the qr code is (altitude)
	double qr_angle = 0;	// Angle of the code from the right x-axis
	double qr_angle_deg = 0;// Same as above but in degrees
	double dis2Mid = 0;		// Distance from the camera middle to code
	double theta1 = 0;		// the arctan of the y' and x' axes
	double theta2 = 0;		// the angle between the two axes
	double theta2_deg = 0;	// theta2 in radians
	double x_d = 0;
	double y_d = 0;
	double x_ab = 0;
	double y_ab = 0;
	int qr_x, qr_y;	// The data from the QR Code
	char text[80];

	// ZBar Scanner for C
	zbar_image_scanner_t* scanner = zbar_image_scanner_create();
	// configure the scanner
	zbar_image_scanner_set_config(scanner, 0, ZBAR_CFG_ENABLE, 1);

	// Extract data from the image
	width = img->width;
	height = img->height;
	raw = (void *) img->imageData;

	// Wrap the image data
	zbar_image_t *image = zbar_image_create();
	zbar_image_set_format(image, *(int*)"Y800");
    zbar_image_set_size(image, width, height);
    zbar_image_set_data(image, raw, width * height, zbar_image_free_data);

	// Scan the image for QR
	int n = zbar_scan_image(scanner, image);

	 /* extract results */
    const zbar_symbol_t *symbol = zbar_image_first_symbol(image);
    for(; symbol; symbol = zbar_symbol_next(symbol)) {
		// Cycle through each symbol found
        zbar_symbol_type_t typ = zbar_symbol_get_type(symbol);
        const char *data = zbar_symbol_get_data(symbol);
        printf("decoded %s symbol \"%s\"\n",
			zbar_get_symbol_name(typ), data);

		sscanf(data, "%d %d", &qr_x, &qr_y);
		printf("QR_X: %i\n", qr_x);
		printf("QR_Y: %i\n", qr_y);

		// Find the angle between the lines
		CvMemStorage* storage = cvCreateMemStorage(0);
		CvSeq* ptseq = cvCreateSeq(CV_SEQ_KIND_GENERIC|CV_32SC2, 
				sizeof(CvContour), sizeof(CvPoint), storage);

		CvPoint pts[4];
		int i = 0;
		for (i = 0; i < 4; ++i) {
			CvPoint point = cvPoint(zbar_symbol_get_loc_x(symbol,i),
					zbar_symbol_get_loc_y(symbol,i));
			cvSeqPush(ptseq, &point);
			pts[i] = point;
		}
		CvBox2D rect = cvMinAreaRect2(ptseq, 0);
		// Draw the outline rectangle

		for (i = 0; i < 4; ++i) {
			cvLine(outimg, pts[i], pts[(i+1)%4], 
					CV_RGB(0, 0, 255), 5, 8, 0);
		}

		// Get the distance from the code to the camera
		qr_length = sqrt(abs(pts[0].x * pts[0].x - pts[1].x * pts[1].x) +
				abs(pts[0].y * pts[0].y - pts[1].y * pts[1].y));
		qr_distance = qr_length * DISTANCE_M + DISTANCE_B;
		printf("Length: %i\n", qr_length);
		printf("Distance: %f\n", qr_distance);

		// Find the relative location
		// Get the angle of the circled rectangle
		qr_angle = -rect.angle;
		if (pts[0].x > pts[3].x && pts[0].y > pts[3].y) qr_angle += 90;
		else if (pts[0].x > pts[3].x && pts[0].y < pts[3].y) qr_angle += 180;
		else if (pts[0].x < pts[3].x && pts[0].y < pts[3].y) qr_angle += 270;
		else if (pts[0].x == pts[1].x && pts[0].y == pts[3].y) {
			if (pts[0].x < pts[3].x && pts[0].y < pts[1].y)
				qr_angle = 0;
			else qr_angle = 180;
		}
		else if (pts[0].x == pts[3].x && pts[0].y == pts[1].y) {
			if (pts[0].x < pts[1].x && pts[0].y > pts[3].y) 
				qr_angle = 90;
			else qr_angle = 270;
		}
		printf("Angle: %f\n", qr_angle);

		//Draw a line on the angle
		qr_angle = qr_angle * 3.1415 / 180;
		CvPoint mid = cvPoint((pts[0].x + pts[2].x) / 2, 
				(pts[0].y + pts[2].y)/2);
		CvPoint p2 = cvPoint(mid.x + 25*cos(qr_angle), 
				mid.y - 25*sin(qr_angle));
		cvLine(outimg,mid, p2, CV_RGB(0,255,0),5,8,0);

		// Get the relative location based on the data of the QR code
		// QR format: x y
		// x and y are seperated by a single space
		// Check if the QR is in the right format
		cvLine(outimg,mid, cvPoint(MID_X,MID_Y), CV_RGB(255,0,0),5,8,0);

		// Relative position (in pixel)
		dis2Mid = sqrt((mid.x - MID_X) * (mid.x - MID_X) + 
				(mid.y - MID_Y) * (mid.y - MID_Y));
		printf("Distance to Quad: %f\n", dis2Mid);

		theta1 = atan2(MID_Y - mid.y, MID_X - mid.x) * 180 / MATH_PI;
		qr_angle_deg = qr_angle * 180 / MATH_PI;
		theta2_deg = 90 - theta1 - qr_angle_deg; 
		theta2 = theta2_deg * MATH_PI / 180;
		x_d = dis2Mid * sin(theta2);
		y_d = dis2Mid * cos(theta2);

		// Display message onto the image
		CvFont font;
		cvInitFont(&font, CV_FONT_HERSHEY_SIMPLEX, 0.5, 0.5, 0, 1, 8);
		sprintf(text, "Attitude: %f", qr_distance);
		cvPutText(outimg, text, cvPoint(30,30), 
				&font, cvScalar(255, 255, 255, 0));
		sprintf(text, "Angle: %f", qr_angle_deg);
		cvPutText(outimg, text, cvPoint(30,50), 
				&font, cvScalar(255, 255, 255, 0));
		x_ab = x_d + qr_x;
		y_ab = y_d + qr_y;
		sprintf(text, "Abs. Pos: (%f, %f)", x_ab, y_ab);
		cvPutText(outimg, text, cvPoint(30,70), 
				&font, cvScalar(255, 255, 255, 0));
    }
}
Ejemplo n.º 13
0
static GstFlowReturn
gst_zbar_transform_frame_ip (GstVideoFilter * vfilter, GstVideoFrame * frame)
{
  GstZBar *zbar = GST_ZBAR (vfilter);
  gpointer data;
  gint stride, height;
  zbar_image_t *image;
  const zbar_symbol_t *symbol;
  int n;

  image = zbar_image_create ();

  /* all formats we support start with an 8-bit Y plane. zbar doesn't need
   * to know about the chroma plane(s) */
  data = GST_VIDEO_FRAME_COMP_DATA (frame, 0);
  stride = GST_VIDEO_FRAME_COMP_STRIDE (frame, 0);
  height = GST_VIDEO_FRAME_HEIGHT (frame);

  zbar_image_set_format (image, GST_MAKE_FOURCC ('Y', '8', '0', '0'));
  zbar_image_set_size (image, stride, height);
  zbar_image_set_data (image, (gpointer) data, stride * height, NULL);

  /* scan the image for barcodes */
  n = zbar_scan_image (zbar->scanner, image);
  if (n == 0)
    goto out;

  /* extract results */
  symbol = zbar_image_first_symbol (image);
  for (; symbol; symbol = zbar_symbol_next (symbol)) {
    zbar_symbol_type_t typ = zbar_symbol_get_type (symbol);
    const char *data = zbar_symbol_get_data (symbol);
    gint quality = zbar_symbol_get_quality (symbol);

    GST_DEBUG_OBJECT (zbar, "decoded %s symbol \"%s\" at quality %d",
        zbar_get_symbol_name (typ), data, quality);

    if (zbar->cache && zbar_symbol_get_count (symbol) != 0)
      continue;

    if (zbar->message) {
      GstMessage *m;
      GstStructure *s;

      /* post a message */
      s = gst_structure_new ("barcode",
          "timestamp", G_TYPE_UINT64, GST_BUFFER_TIMESTAMP (frame->buffer),
          "type", G_TYPE_STRING, zbar_get_symbol_name (typ),
          "symbol", G_TYPE_STRING, data, "quality", G_TYPE_INT, quality, NULL);
      m = gst_message_new_element (GST_OBJECT (zbar), s);
      gst_element_post_message (GST_ELEMENT (zbar), m);
    }
  }

out:
  /* clean up */
  zbar_image_scanner_recycle_image (zbar->scanner, image);
  zbar_image_destroy (image);

  return GST_FLOW_OK;
}
Ejemplo n.º 14
0
int c_zbar(IMG(x), int*m, TBarcode* result) {
    
    //printf("hello\n");
    /* create a reader */
    zbar_image_scanner_t * scanner = zbar_image_scanner_create();

    /* configure the reader */
    zbar_image_scanner_set_config(scanner, 0, ZBAR_CFG_ENABLE, 1);

    /* obtain image data */
    int width = xsc2+1, height = xsr2+1;
    if (xsc1!=0 || xsr1!=0 || xsstep != width) {
        printf("c1=%d c2=%d r1=%d r2=%d step=%d\n",xsc1,xsc2,xsr1,xsr2,xsstep);
        return 1;
    }

    void *raw = xpSrc;
    //get_data(argv[1], &width, &height, &raw);

    /* wrap image data */
    zbar_image_t *image = zbar_image_create();
    zbar_image_set_format(image, *(int*)"Y800");
    zbar_image_set_size(image, width, height);
    zbar_image_set_data(image, raw, width * height, NULL);
    
    //printf("after set\n");
    /* scan the image for barcodes */
    int n = zbar_scan_image(scanner, image);
    //printf("scan_image code: %d\n",n);
    /* extract results */
    int k = 0;
    const zbar_symbol_t *symbol = zbar_image_first_symbol(image);
    for(; symbol; symbol = zbar_symbol_next(symbol)) {
        /* do something useful with results */
        zbar_symbol_type_t typ = zbar_symbol_get_type(symbol);
        result[k].symbol_type = zbar_get_symbol_name(typ);
        const char *data = zbar_symbol_get_data(symbol);
        result[k].symbol_value = data;
        // printf("decoded %s symbol \"%s\"\n",zbar_get_symbol_name(typ), data);

        int np = zbar_symbol_get_loc_size(symbol);
        //printf("np=%d\n",np);
        int j,x,y,r1,c1,r2,c2;
        c1 = c2 = zbar_symbol_get_loc_x(symbol,0);
        r1 = r2 = zbar_symbol_get_loc_y(symbol,0);
        for (j=1; j<np; j++) {
            x = zbar_symbol_get_loc_x(symbol,j);
            y = zbar_symbol_get_loc_y(symbol,j);
            if (y<r1) r1 = y;
            if (y>r2) r2 = y;
            if (x<c1) c1 = x;
            if (x>c2) c2 = x;
            //printf("j=%d\n",j);
        }
        result[k].bbr1 = r1;
        result[k].bbr2 = r2;
        result[k].bbc1 = c1;
        result[k].bbc2 = c2;
        k++;
        if(k>=50) break;
    }

    /* clean up */
    zbar_image_destroy(image);

    *m = k;
    
    return n;
}
Ejemplo n.º 15
0
/* API lock is already held */
int _zbar_process_image (zbar_processor_t *proc,
                         zbar_image_t *img)
{
    uint32_t force_fmt = proc->force_output;
    int nsyms, rc;
    if(img) {
        zbar_image_t *tmp;
        uint32_t format = zbar_image_get_format(img);
        zprintf(16, "processing: %.4s(%08lx) %dx%d @%p\n",
                (char*)&format, format,
                zbar_image_get_width(img), zbar_image_get_height(img),
                zbar_image_get_data(img));

        /* FIXME locking all other interfaces while processing is conservative
         * but easier for now and we don't expect this to take long...
         */
        tmp = zbar_image_convert(img, fourcc('Y','8','0','0'));
        if(!tmp)
            goto error;

        if(proc->syms) {
            zbar_symbol_set_ref(proc->syms, -1);
            proc->syms = NULL;
        }
        zbar_image_scanner_recycle_image(proc->scanner, img);
        nsyms = zbar_scan_image(proc->scanner, tmp);
        _zbar_image_swap_symbols(img, tmp);

        zbar_image_destroy(tmp);
        tmp = NULL;
        if(nsyms < 0)
            goto error;

        proc->syms = zbar_image_scanner_get_results(proc->scanner);
        if(proc->syms)
            zbar_symbol_set_ref(proc->syms, 1);

        if(_zbar_verbosity >= 8) {
            const zbar_symbol_t *sym = zbar_image_first_symbol(img);
            while(sym) {
                zbar_symbol_type_t type = zbar_symbol_get_type(sym);
                int count = zbar_symbol_get_count(sym);
                zprintf(8, "%s%s: %s (%d pts) (q=%d) (%s)\n",
                        zbar_get_symbol_name(type),
                        zbar_get_addon_name(type),
                        zbar_symbol_get_data(sym),
                        zbar_symbol_get_loc_size(sym),
                        zbar_symbol_get_quality(sym),
                        (count < 0) ? "uncertain" :
                        (count > 0) ? "duplicate" : "new");
                sym = zbar_symbol_next(sym);
            }
        }

        if(nsyms) {
            /* FIXME only call after filtering */
            _zbar_mutex_lock(&proc->mutex);
            _zbar_processor_notify(proc, EVENT_OUTPUT);
            _zbar_mutex_unlock(&proc->mutex);
            if(proc->handler)
                proc->handler(img, proc->userdata);
        }

        if(force_fmt) {
            zbar_symbol_set_t *syms = img->syms;
            img = zbar_image_convert(img, force_fmt);
            if(!img)
                goto error;
            img->syms = syms;
            zbar_symbol_set_ref(syms, 1);
        }
    }

    /* display to window if enabled */
    rc = 0;
    
    if(force_fmt && img)
        zbar_image_destroy(img);
    return(rc);

error:
    return(err_capture(proc, SEV_ERROR, ZBAR_ERR_UNSUPPORTED,
                       __func__, "unknown image format"));
}
Ejemplo n.º 16
0
static void symbol_handler (zbar_decoder_t *dcode)
{
    zbar_image_scanner_t *iscn = zbar_decoder_get_userdata(dcode);
    zbar_symbol_type_t type = zbar_decoder_get_type(dcode);
    int x = 0, y = 0, dir;
    const char *data;
    unsigned datalen;
    zbar_symbol_t *sym;

#ifdef ENABLE_QRCODE
    if(type == ZBAR_QRCODE) {
        qr_handler(iscn);
        return;
    }
#else
    assert(type != ZBAR_QRCODE);
#endif

    if(TEST_CFG(iscn, ZBAR_CFG_POSITION)) {
        /* tmp position fixup */
        int w = zbar_scanner_get_width(iscn->scn);
        int u = iscn->umin + iscn->du * zbar_scanner_get_edge(iscn->scn, w, 0);
        if(iscn->dx) {
            x = u;
            y = iscn->v;
        }
        else {
            x = iscn->v;
            y = u;
        }
    }

    /* FIXME debug flag to save/display all PARTIALs */
    if(type <= ZBAR_PARTIAL) {
        zprintf(256, "partial symbol @(%d,%d)\n", x, y);
        return;
    }

    data = zbar_decoder_get_data(dcode);
    datalen = zbar_decoder_get_data_length(dcode);

    /* FIXME need better symbol matching */
    for(sym = iscn->syms->head; sym; sym = sym->next)
        if(sym->type == type &&
           sym->datalen == datalen &&
           !memcmp(sym->data, data, datalen)) {
            sym->quality++;
            zprintf(224, "dup symbol @(%d,%d): dup %s: %.20s\n",
                    x, y, zbar_get_symbol_name(type), data);
            if(TEST_CFG(iscn, ZBAR_CFG_POSITION))
                /* add new point to existing set */
                /* FIXME should be polygon */
                sym_add_point(sym, x, y);
            return;
        }

    sym = _zbar_image_scanner_alloc_sym(iscn, type, datalen + 1);
    sym->configs = zbar_decoder_get_configs(dcode, type);
    sym->modifiers = zbar_decoder_get_modifiers(dcode);
    /* FIXME grab decoder buffer */
    memcpy(sym->data, data, datalen + 1);

    /* initialize first point */
    if(TEST_CFG(iscn, ZBAR_CFG_POSITION)) {
        zprintf(192, "new symbol @(%d,%d): %s: %.20s\n",
                x, y, zbar_get_symbol_name(type), data);
        sym_add_point(sym, x, y);
    }

    dir = zbar_decoder_get_direction(dcode);
    if(dir)
        sym->orient = (iscn->dy != 0) + ((iscn->du ^ dir) & 2);

    _zbar_image_scanner_add_sym(iscn, sym);
}
Ejemplo n.º 17
0
static int scan_image (IplImage * image)
{
  if(exit_code == 3)
    return(-1);

  int found = 0;
  if(exit_code == 3)
    return(-1);
      
  zbar_image_t *zimage = zbar_image_create();
  assert(zimage);
  zbar_image_set_format(zimage, *(unsigned long*)"Y800");
    
    
  int width = image->width;
  int height = image->height;
  zbar_image_set_size(zimage, width, height);
    
  // extract grayscale image pixels
  // FIXME color!! ...preserve most color w/422P
  // (but only if it's a color image)
  size_t bloblen = width * height;
  unsigned char *blob = malloc(bloblen);
  zbar_image_set_data(zimage, blob, bloblen, zbar_image_free_data);
      
      
  memcpy(blob, image->imageData, image->imageSize); 
      
  if(xmllvl == 1) 
  {
    xmllvl++;
    //printf("<source href='%s'>\n", filename);
  }
      
  zbar_process_image(processor, zimage);
      
  // output result data
  const zbar_symbol_t *sym = zbar_image_first_symbol(zimage);
  for(; sym; sym = zbar_symbol_next(sym)) 
  {
    zbar_symbol_type_t typ = zbar_symbol_get_type(sym);
    if(typ == ZBAR_PARTIAL)
      continue;
    else if(!xmllvl)
      printf("%s%s:%s\n",
             zbar_get_symbol_name(typ),
             zbar_get_addon_name(typ),
             zbar_symbol_get_data(sym));
    else if(xmllvl < 0)
      printf("%s\n", zbar_symbol_get_data(sym));
    else {
      if(xmllvl < 3) 
      {
        xmllvl++;
        //printf("<index num='%u'>\n", seq);
      }
      zbar_symbol_xml(sym, &xmlbuf, &xmlbuflen);
      printf("%s\n", xmlbuf);
    }
    found++;
    num_symbols++;
  }
  if(xmllvl > 2) 
  {
    xmllvl--;
    printf("</index>\n");
  }
  fflush(stdout);
      
  zbar_image_destroy(zimage);
      
  num_images++;
  if(zbar_processor_is_visible(processor)) 
  {
    printf("waiting for q\n");
    int rc = zbar_processor_user_wait(processor, -1);
    if(rc < 0 || rc == 'q' || rc == 'Q')
      exit_code = 3;
  }
  //}
    
  if(xmllvl > 1) 
  {
    xmllvl--;
    printf("</source>\n");
  }
    
  if(!found)
    notfound++;
    
  //DestroyMagickWand(images);
  return(0);
}//scan_image
Ejemplo n.º 18
0
static GstFlowReturn
gst_skor_sink_transform_frame_ip (GstVideoFilter * vfilter, GstVideoFrame * frame)
{
  GstSkorSink *sink = GST_SKORSINK (vfilter);
  gpointer data;
  gint stride, height;
  zbar_image_t *image;
  const zbar_symbol_t *symbol;
  int n;

  image = zbar_image_create ();

  /* all formats we support start with an 8-bit Y plane. zbar doesn't need
   * to know about the chroma plane(s) */
  data = GST_VIDEO_FRAME_COMP_DATA (frame, 0);
  stride = GST_VIDEO_FRAME_COMP_STRIDE (frame, 0);
  height = GST_VIDEO_FRAME_HEIGHT (frame);

  zbar_image_set_format (image, GST_MAKE_FOURCC ('Y', '8', '0', '0'));
  zbar_image_set_size (image, stride, height);
  zbar_image_set_data (image, (gpointer) data, stride * height, NULL);

  /* scan the image for barcodes */
  n = zbar_scan_image (sink->scanner, image);
  if (G_UNLIKELY (n == -1)) {
    GST_WARNING_OBJECT (sink, "Error trying to scan frame. Skipping");
    goto out;
  }
  if (n == 0)
    goto out;

  /* extract results */
  symbol = zbar_image_first_symbol (image);
  for (; symbol; symbol = zbar_symbol_next (symbol)) {
    zbar_symbol_type_t typ = zbar_symbol_get_type (symbol);
    const char *data = zbar_symbol_get_data (symbol);
    gint quality = zbar_symbol_get_quality (symbol);

    GST_DEBUG_OBJECT (sink, "decoded %s symbol \"%s\" at quality %d",
        zbar_get_symbol_name (typ), data, quality);

    if (sink->cache && zbar_symbol_get_count (symbol) != 0)
      continue;

    if (sink->data_consumer)
      sink->data_consumer (data);

    if (sink->message) {
      GstMessage *m;
      GstStructure *s;
      GstSample *sample;
      GstCaps *sample_caps;

      s = gst_structure_new ("barcode",
          "timestamp", G_TYPE_UINT64, GST_BUFFER_TIMESTAMP (frame->buffer),
          "type", G_TYPE_STRING, zbar_get_symbol_name (typ),
          "symbol", G_TYPE_STRING, data, "quality", G_TYPE_INT, quality, NULL);

      if (sink->attach_frame) {
        /* create a sample from image */
        sample_caps = gst_video_info_to_caps (&frame->info);
        sample = gst_sample_new (frame->buffer, sample_caps, NULL, NULL);
        gst_caps_unref (sample_caps);
        gst_structure_set (s, "frame", GST_TYPE_SAMPLE, sample, NULL);
        gst_sample_unref (sample);
      }

      m = gst_message_new_element (GST_OBJECT (sink), s);
      gst_element_post_message (GST_ELEMENT (sink), m);

    } else if (sink->attach_frame)
      GST_WARNING_OBJECT (sink,
          "attach-frame=true has no effect if message=false");
  }

out:
  /* clean up */
  zbar_image_scanner_recycle_image (sink->scanner, image);
  zbar_image_destroy (image);

  return GST_FLOW_OK;
}