static void gst_zbar_init (GstZBar * zbar) { zbar->cache = DEFAULT_CACHE; zbar->message = DEFAULT_MESSAGE; zbar->scanner = zbar_image_scanner_create (); }
static void gst_skor_sink_init (GstSkorSink * filter) { filter->cache = DEFAULT_CACHE; filter->message = DEFAULT_MESSAGE; filter->attach_frame = DEFAULT_ATTACH_FRAME; filter->data_consumer = NULL; filter->scanner = zbar_image_scanner_create (); }
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; }
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')); }
JNIEXPORT jlong JNICALL Java_net_sourceforge_zbar_ImageScanner_create (JNIEnv *env, jobject obj) { zbar_image_scanner_t *zscn = zbar_image_scanner_create(); if(!zscn) { throw_exc(env, "java/lang/OutOfMemoryError", NULL); return(0); } stats.ImageScanner_create++; return((intptr_t)zscn); }
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; }
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; }
zbar_processor_t *zbar_processor_create (int threaded) { zbar_processor_t *proc = calloc(1, sizeof(zbar_processor_t)); if(!proc) return(NULL); err_init(&proc->err, ZBAR_MOD_PROCESSOR); proc->scanner = zbar_image_scanner_create(); if(!proc->scanner) { free(proc); return(NULL); } proc->threaded = !_zbar_mutex_init(&proc->mutex) && threaded; _zbar_processor_init(proc); return(proc); }
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); }
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)); } }
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; }
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); }