Exemplo n.º 1
0
int zbar_video_init (zbar_video_t *vdo,
                     unsigned long fmt)
{
    if(vdo->initialized)
        /* FIXME re-init different format? */
        return(err_capture(vdo, SEV_ERROR, ZBAR_ERR_INVALID, __func__,
                           "already initialized, re-init unimplemented"));

    if(vdo->init(vdo, fmt))
        return(-1);
    vdo->format = fmt;
    if(video_init_images(vdo))
        return(-1);
#ifdef HAVE_LIBJPEG
    const zbar_format_def_t *vidfmt = _zbar_format_lookup(fmt);
    if(vidfmt && vidfmt->group == ZBAR_FMT_JPEG) {
        /* prepare for decoding */
        if(!vdo->jpeg)
            vdo->jpeg = _zbar_jpeg_decomp_create();
        if(vdo->jpeg_img)
            zbar_image_destroy(vdo->jpeg_img);

        /* create intermediate image for decoder to use*/
        zbar_image_t *img = vdo->jpeg_img = zbar_image_create();
        img->format = fourcc('Y','8','0','0');
        img->width = vdo->width;
        img->height = vdo->height;
        img->datalen = vdo->width * vdo->height;
    }
#endif
    vdo->initialized = 1;
    return(0);
}
Exemplo n.º 2
0
int main (int argc, char **argv)
{
    zbar_set_verbosity(32);

    zbar_processor_t *proc = zbar_processor_create(0);
    assert(proc);
    if(zbar_processor_init(proc, NULL, 1))
        return(2);

    zbar_image_t *img = zbar_image_create();
    zbar_image_set_size(img, 8, 8);
    zbar_image_set_format(img, fourcc('J','P','E','G'));
    zbar_image_set_data(img, jpeg, sizeof(jpeg), NULL);

    zbar_image_t *test = zbar_image_convert(img, fourcc('Y','8','0','0'));
    if(!test)
        return(2);
    printf("converted: %d x %d (%lx) %08lx\n",
           zbar_image_get_width(test),
           zbar_image_get_height(test),
           zbar_image_get_data_length(test),
           zbar_image_get_format(test));

    if(zbar_process_image(proc, test) < 0)
        return(3);
    if(zbar_processor_set_visible(proc, 1))
        return(4);

    zbar_processor_user_wait(proc, -1);
    return(0);
}
Exemplo n.º 3
0
	inline void zbar_image_free_data (zbar_image_t *img)
	{
		if(!img)
			return;
		if(img->src) {
			/* replace video image w/new copy */
			assert(img->refcnt); /* FIXME needs lock */
			zbar_image_t *newimg = zbar_image_create();
			memcpy(newimg, img, sizeof(zbar_image_t));
			/* recycle video image */
			newimg->cleanup(newimg);
			/* detach old image from src */
			img->cleanup = NULL;
			img->src = NULL;
			img->srcidx = -1;
		}
		else if(img->cleanup && img->data) {
			if(img->cleanup != zbar_image_free_data) {
				/* using function address to detect this case is a bad idea;
				* windows link libraries add an extra layer of indirection...
				* this works around that problem (bug #2796277)
				*/
				zbar_image_cleanup_handler_t *cleanup = img->cleanup;
				img->cleanup = zbar_image_free_data;
				cleanup(img);
			}
			else
				free((void*)img->data);
		}
		img->data = NULL;
	}
Exemplo n.º 4
0
zbar_video_t *zbar_video_create ()
{
    zbar_video_t *vdo = calloc(1, sizeof(zbar_video_t));
    int i;
    if(!vdo)
        return(NULL);
    err_init(&vdo->err, ZBAR_MOD_VIDEO);
    vdo->fd = -1;

    (void)_zbar_mutex_init(&vdo->qlock);

    /* pre-allocate images */
    vdo->num_images = ZBAR_VIDEO_IMAGES_MAX;
    vdo->images = calloc(ZBAR_VIDEO_IMAGES_MAX, sizeof(zbar_image_t*));
    if(!vdo->images) {
        zbar_video_destroy(vdo);
        return(NULL);
    }

    for(i = 0; i < ZBAR_VIDEO_IMAGES_MAX; i++) {
        zbar_image_t *img = vdo->images[i] = zbar_image_create();
        if(!img) {
            zbar_video_destroy(vdo);
            return(NULL);
        }
        img->refcnt = 0;
        img->cleanup = _zbar_video_recycle_image;
        img->srcidx = i;
        img->src = vdo;
    }

    return(vdo);
}
Exemplo n.º 5
0
const char* rho_barcode_barcode_recognize(const char* filename) {
  void* img_buf = 0;
  int img_width;
  int img_height;	

  	
  rho_platform_image_load_grayscale(filename, &img_buf, &img_width, &img_height);

  if (img_buf != 0)
  {
	zbar_image_scanner_t* zbar_img_scanner = zbar_image_scanner_create();
	zbar_image_t* zbar_img = zbar_image_create();
	const zbar_symbol_t* zbar_symbol = 0;

        zbar_image_scanner_set_config(zbar_img_scanner, ZBAR_NONE, ZBAR_CFG_ENABLE, 1);

	zbar_image_set_format(zbar_img, zbar_fourcc('Y','8','0','0'));
	zbar_image_set_size(zbar_img, img_width, img_height);
	//zbar_image_set_data(zbar_img, img_buf, img_width * img_height, zbar_image_free_data);
	zbar_image_set_data(zbar_img, img_buf, img_width * img_height, 0);
		
	zbar_scan_image(zbar_img_scanner, zbar_img);

	
	// get result
	//zbar_symbol_set_t* zbar_symbols = zbar_image_get_symbols(zbar_img);
	zbar_symbol = zbar_image_first_symbol(zbar_img);

	if (zbar_symbol != 0) {
		//printf(zbar_symbol_get_data(zbar_symbol));
             //sprintf(strbuf, "IMG [%d x %d ]\nCODE = %s",img_width, img_height, zbar_symbol_get_data(zbar_symbol)); 
             strcpy(strbuf, zbar_symbol_get_data(zbar_symbol));
	}
        else {
             //sprintf(strbuf, "IMG [%d x %d ]\nCODE IS UNRECOGNIZED ",img_width, img_height); 
             strcpy(strbuf, "");
        }


	zbar_image_destroy(zbar_img);
	zbar_image_scanner_destroy(zbar_img_scanner);

  }
  else {
		//sprintf(strbuf, "NO IMG TO RECOGNIZE",img_width, img_height); 
        strcpy(strbuf, "");
  }

  if (img_buf) {
    rho_platform_image_free(img_buf);
  }
	  

  return strbuf;
}
Exemplo n.º 6
0
static void qr_detect_init()
{
    zscn = zbar_image_scanner_create();
    zbar_image_scanner_set_config(zscn, 0, ZBAR_CFG_ENABLE, 1);
    zbar_image_scanner_set_config(zscn, 0, ZBAR_CFG_X_DENSITY, 1);
    zbar_image_scanner_set_config(zscn, 0, ZBAR_CFG_Y_DENSITY, 1);

    zimg = zbar_image_create();
    zbar_image_set_size(zimg, 640, 360);
    zbar_image_set_format(zimg, zbar_fourcc('G','R','E','Y'));
}
Exemplo n.º 7
0
static zbar_image_t *_php_zbarcode_image_create(unsigned long width, unsigned long height, unsigned char *image_data)
{
	zbar_image_t *image = zbar_image_create();
	
	if (!image)
		return NULL;
	
	zbar_image_set_format(image, *(int*)"Y800");
	zbar_image_set_size(image, width, height);
	zbar_image_set_data(image, (void *)image_data, width * height, zbar_image_free_data);
	return image;
}
Exemplo n.º 8
0
JNIEXPORT jlong JNICALL
Java_net_sourceforge_zbar_Image_create (JNIEnv *env,
                                        jobject obj)
{
    zbar_image_t *zimg = zbar_image_create();
    if(!zimg) {
        throw_exc(env, "java/lang/OutOfMemoryError", NULL);
        return(0);
    }
    stats.Image_create++;
    return((intptr_t)zimg);
}
Exemplo n.º 9
0
zbar_image_t *zbar_image_copy (const zbar_image_t *src)
{
    zbar_image_t *dst = zbar_image_create();
    dst->format = src->format;
    _zbar_image_copy_size(dst, src);
    dst->datalen = src->datalen;
    dst->data = malloc(src->datalen);
    assert(dst->data);
    memcpy((void*)dst->data, src->data, src->datalen);
    dst->cleanup = zbar_image_free_data;
    return(dst);
}
JNIEXPORT jstring JNICALL Java_com_acdd_qrcode_BarCode_decodeCrop(
		JNIEnv *env,jclass clazz,   jbyteArray img, jint width, jint height,
		jint x, jint y, jint cropw, jint croph) {

	zbar_image_scanner_t *scanner;
	zbar_image_t *zimage;
	zbar_image_t *zgrayimage;

	jbyte *pixbuf;
	jstring s = NULL;

	zbar_set_verbosity(10); // XXX

	pixbuf = (*env)->GetByteArrayElements(env, img, 0);

	zimage = zbar_image_create();
	if (zimage == NULL) {
		(*env)->ReleaseByteArrayElements(env, img, pixbuf, 0);
		return NULL;
	}

	zbar_image_set_format(zimage, *(unsigned long *) "Y800");
	zbar_image_set_size(zimage, width, height);
	zbar_image_set_data(zimage, pixbuf, (*env)->GetArrayLength(env, img),
			zbar_image_free_data);

	zbar_image_set_crop(zimage, x, y, cropw, croph);

	zgrayimage = zbar_image_convert(zimage, *(unsigned long *) "Y800");
	if (zgrayimage == NULL) {
		(*env)->ReleaseByteArrayElements(env, img, pixbuf, 0);
		return NULL;
	}

	zbar_image_destroy(zimage);

	scanner = zbar_image_scanner_create();
	zbar_image_scanner_set_config(scanner, 0, ZBAR_CFG_ENABLE, 1);
	zbar_scan_image(scanner, zgrayimage);

	const zbar_symbol_t *sym;

	sym = zbar_image_first_symbol(zgrayimage);
	if (sym != NULL) {
		const char *sym_data;
		sym_data = zbar_symbol_get_data(sym);
		__android_log_print(ANDROID_LOG_DEBUG, "zbar", "url: %s", sym_data);
		s = (*env)->NewStringUTF(env, sym_data);
	}

	(*env)->ReleaseByteArrayElements(env, img, pixbuf, 0);
	return s;
}
Exemplo n.º 11
0
zbar_image_t *zbar_video_next_image (zbar_video_t *vdo)
{
    unsigned frame;
    zbar_image_t *img;

    if(video_lock(vdo))
        return(NULL);
    if(!vdo->active) {
        video_unlock(vdo);
        return(NULL);
    }

    frame = vdo->frame++;
    img = vdo->dq(vdo);
    if(img) {
        img->seq = frame;
        if(vdo->num_images < 2) {
            /* return a *copy* of the video image and immediately recycle
             * the driver's buffer to avoid deadlocking the resources
             */
            zbar_image_t *tmp = img;
            video_lock(vdo);
            img = vdo->shadow_image;
            vdo->shadow_image = (img) ? img->next : NULL;
            video_unlock(vdo);
                
            if(!img) {
                img = zbar_image_create();
                assert(img);
                img->refcnt = 0;
                img->src = vdo;
                /* recycle the shadow images */

                img->format = vdo->format;
                zbar_image_set_size(img, vdo->width, vdo->height);
                img->datalen = vdo->datalen;
                img->data = malloc(vdo->datalen);
            }
            img->cleanup = _zbar_video_recycle_shadow;
            img->seq = frame;
            memcpy((void*)img->data, tmp->data, img->datalen);
            _zbar_video_recycle_image(tmp);
        }
        else
            img->cleanup = _zbar_video_recycle_image;
        _zbar_image_refcnt(img, 1);
    }
    return(img);
}
Exemplo n.º 12
0
JNIEXPORT jstring JNICALL Java_trikita_obsqr_Zbar_process
	(JNIEnv *env, jobject obj, jint w, jint h, jbyteArray img) {

	zbar_image_scanner_t *scanner;
	zbar_image_t *zimage;
	zbar_image_t *zgrayimage;

	jbyte *pixbuf;
	jstring s = NULL;

	zbar_set_verbosity(10); // XXX
	
	pixbuf = (*env)->GetByteArrayElements(env, img, 0);

	zimage = zbar_image_create();
	if (zimage == NULL) {
		(*env)->ReleaseByteArrayElements(env, img, pixbuf, 0);
		return NULL;
	}
	zbar_image_set_format(zimage, *(unsigned long *) "Y800");
	zbar_image_set_size(zimage, w, h);
	zbar_image_set_data(zimage, pixbuf, (*env)->GetArrayLength(env, img), 
			zbar_image_free_data);

	zgrayimage = zbar_image_convert(zimage, *(unsigned long *) "Y800");
	if (zgrayimage == NULL) {
		(*env)->ReleaseByteArrayElements(env, img, pixbuf, 0);
		return NULL;
	}

	zbar_image_destroy(zimage);

	scanner = zbar_image_scanner_create();
	zbar_image_scanner_set_config(scanner, 0, ZBAR_CFG_ENABLE, 1);
	zbar_scan_image(scanner, zgrayimage);

	const zbar_symbol_t *sym;

	sym = zbar_image_first_symbol(zgrayimage);
	if (sym != NULL) {
		const char *sym_data;
		sym_data = zbar_symbol_get_data(sym);
		__android_log_print(ANDROID_LOG_DEBUG, "zbar", "url: %s", sym_data);
		s = (*env)->NewStringUTF(env, sym_data);
	}

	(*env)->ReleaseByteArrayElements(env, img, pixbuf, 0);
	return s;
}
Exemplo n.º 13
0
static zbarImage*
image_new (PyTypeObject *type,
           PyObject *args,
           PyObject *kwds)
{
    zbarImage *self = (zbarImage*)type->tp_alloc(type, 0);
    if(!self)
        return(NULL);

    self->zimg = zbar_image_create();
    if(!self->zimg) {
        Py_DECREF(self);
        return(NULL);
    }
    zbar_image_set_userdata(self->zimg, self);
    return(self);
}
Exemplo n.º 14
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;
}
Exemplo n.º 15
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);
}
Exemplo n.º 16
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));
    }
}
Exemplo n.º 17
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);
}
Exemplo n.º 18
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;
}
Exemplo n.º 19
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;
}
Exemplo n.º 20
0
static void *zbar_gtk_processing_thread (void *arg)
{
    ZBarGtk *self = ZBAR_GTK(arg);
    if(!self->_private)
        return(NULL);
    ZBarGtkPrivate *zbar = ZBAR_GTK_PRIVATE(self->_private);
    g_object_ref(zbar);
    g_assert(zbar->queue);
    g_async_queue_ref(zbar->queue);

    zbar->scanner = zbar_image_scanner_create();
    g_assert(zbar->scanner);

    /* thread side enabled state */
    gboolean video_enabled = FALSE;
    GValue *msg = NULL;

    while(TRUE) {
        if(!msg)
            msg = g_async_queue_pop(zbar->queue);
        g_assert(G_IS_VALUE(msg));

        GType type = G_VALUE_TYPE(msg);
        if(type == G_TYPE_INT) {
            /* video state change */
            int state = g_value_get_int(msg);
            if(state < 0) {
                /* terminate processing thread */
                g_value_unset(msg);
                g_free(msg);
                msg = NULL;
                break;
            }
            g_assert(state >= 0 && state <= 1);
            video_enabled = (state != 0);
        }
        else if(type == G_TYPE_STRING) {
            /* open new video device */
            const char *video_device = g_value_get_string(msg);
            video_enabled = zbar_gtk_video_open(self, video_device);
        }
        else if(type == GDK_TYPE_PIXBUF) {
            /* scan provided image and broadcast results */
            zbar_image_t *image = zbar_image_create();
            GdkPixbuf *pixbuf = GDK_PIXBUF(g_value_dup_object(msg));
            if(zbar_gtk_image_from_pixbuf(image, pixbuf))
                zbar_gtk_process_image(self, image);
            else
                g_object_unref(pixbuf);
            zbar_image_destroy(image);
        }
        else {
            gchar *dbg = g_strdup_value_contents(msg);
            g_warning("unknown message type (%x) passed to thread: %s\n",
                      (unsigned)type, dbg);
            g_free(dbg);
        }
        g_value_unset(msg);
        g_free(msg);
        msg = NULL;

        if(video_enabled) {
            /* release reference to any previous pixbuf */
            zbar_window_draw(zbar->window, NULL);

            if(zbar_video_enable(zbar->video, 1)) {
                zbar_video_error_spew(zbar->video, 0);
                video_enabled = FALSE;
                continue;
            }
            zbar_image_scanner_enable_cache(zbar->scanner, 1);

            while(video_enabled &&
                  !(msg = g_async_queue_try_pop(zbar->queue))) {
                zbar_image_t *image = zbar_video_next_image(zbar->video);
                if(zbar_gtk_process_image(self, image) < 0)
                    video_enabled = FALSE;
                if(image)
                    zbar_image_destroy(image);
            }

            zbar_image_scanner_enable_cache(zbar->scanner, 0);
            if(zbar_video_enable(zbar->video, 0)) {
                zbar_video_error_spew(zbar->video, 0);
                video_enabled = FALSE;
            }
            /* release video image and revert to logo */
            if(zbar->window) {
                zbar_window_draw(zbar->window, NULL);
                gtk_widget_queue_draw(GTK_WIDGET(self));
            }

            if(!video_enabled)
                /* must have been an error while streaming */
                zbar_gtk_video_open(self, NULL);
        }
    }
    if(zbar->window)
        zbar_window_draw(zbar->window, NULL);
    g_object_unref(zbar);
    return(NULL);
}
Exemplo n.º 21
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
Exemplo n.º 22
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;
}