Example #1
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);
}
Example #2
0
static int
image_set_int (zbarImage *self,
               PyObject *value,
               void *closure)
{
    unsigned int tmp, val = PyInt_AsSsize_t(value);
    if(val == -1 && PyErr_Occurred()) {
        PyErr_SetString(PyExc_TypeError, "expecting an integer");
        return(-1);
    }
    switch((int)closure) {
    case 0:
        tmp = zbar_image_get_height(self->zimg);
        zbar_image_set_size(self->zimg, val, tmp);
        break;
    case 1:
        tmp = zbar_image_get_width(self->zimg);
        zbar_image_set_size(self->zimg, tmp, val);
        break;
    case 2:
        zbar_image_set_sequence(self->zimg, val);
    default:
        assert(0);
    }
    return(0);
}
Example #3
0
static PyObject*
image_get_size (zbarImage *self,
                void *closure)
{
    unsigned int width = zbar_image_get_width(self->zimg);
    unsigned int height = zbar_image_get_height(self->zimg);
    return(PyTuple_Pack(2, PyInt_FromLong(width), PyInt_FromLong(height)));
}
Example #4
0
int test_image_bars (zbar_image_t *img)
{
    allocated_images++;
    unsigned w = zbar_image_get_width(img);
    unsigned h = zbar_image_get_height(img);

    const format_def_t *fmt;
    for(fmt = formats; fmt->format; fmt++)
        if(fmt->format == zbar_image_get_format(img))
            break;
    if(!fmt->format)
        return(-1);

    unsigned long planelen = w * h;
    unsigned long datalen = planelen * fmt->bpp / 8;
    uint8_t *data = malloc(datalen);
    zbar_image_set_data(img, data, datalen, test_cleanup_handler);
    fprintf(stderr, "create %.4s(%08" PRIx32 ") image data %lx bytes @%p\n",
            (char*)&fmt->format, fmt->format, datalen, data);

    uint8_t *p = data;
    switch(fmt->type) {
    case GRAY:
    case YUVP: /* planar YUV */
    case YVUP:
        p = fill_bars_y(p, w, h);
        if(fmt->type != GRAY) {
            w = (w + fmt->xdiv - 1) / fmt->xdiv;
            h = (h + fmt->ydiv - 1) / fmt->ydiv;
        }
        else
            break;

        if(fmt->format == fourcc('N','V','1','2') ||
                fmt->format == fourcc('N','V','2','1'))
            p = fill_bars_nv(p, w, h, fmt->type);
        else if(fmt->type == YUVP ||
                fmt->type == YVUP) {
            p = fill_bars_uv(p, w, h, (fmt->type == YUVP) ? Cb : Cr);
            p = fill_bars_uv(p, w, h, (fmt->type == YUVP) ? Cr : Cb);
        }
        break;

    case YUYV: /* packed YUV */
    case YVYU:
    case UYVY:
        p = fill_bars_yuv(p, w, h, fmt->type);
        break;

    default: /* RGB */
        p = fill_bars_rgb(p, w, h, fmt->type, fmt->bpp);
        break;
    }

    assert(p == data + datalen);
    return(0);
}
Example #5
0
int
zbarImage_validate (zbarImage *img)
{
    if(!zbar_image_get_width(img->zimg) ||
       !zbar_image_get_height(img->zimg) ||
       !zbar_image_get_data(img->zimg) ||
       !zbar_image_get_data_length(img->zimg)) {
        PyErr_Format(PyExc_ValueError, "image size and data must be defined");
        return(-1);
    }
    return(0);
}
Example #6
0
static PyObject*
image_get_int (zbarImage *self,
               void *closure)
{
    unsigned int val = -1;
    switch((int)closure) {
    case 0:
        val = zbar_image_get_width(self->zimg); break;
    case 1:
        val = zbar_image_get_height(self->zimg); break;
    case 2:
        val = zbar_image_get_sequence(self->zimg); break;
    default:
        assert(0);
    }
    return(PyInt_FromLong(val));
}
Example #7
0
int zbar_process_image (zbar_processor_t *proc,
                        zbar_image_t *img)
{
    proc_enter(proc);
    _zbar_mutex_unlock(&proc->mutex);

    int rc = 0;
    if(img && proc->window)
        rc = _zbar_processor_set_size(proc,
                                      zbar_image_get_width(img),
                                      zbar_image_get_height(img));
    if(!rc) {
        zbar_image_scanner_enable_cache(proc->scanner, 0);
        rc = _zbar_process_image(proc, img);
        if(proc->streaming)
            zbar_image_scanner_enable_cache(proc->scanner, 1);
    }

    _zbar_mutex_lock(&proc->mutex);
    proc_leave(proc);
    return(rc);
}
Example #8
0
JNIEXPORT jint JNICALL
Java_net_sourceforge_zbar_Image_getWidth (JNIEnv *env,
                                          jobject obj)
{
    return(zbar_image_get_width(GET_PEER(Image, obj)));
}
Example #9
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"));
}
Example #10
0
int zbar_scan_image (zbar_image_scanner_t *iscn,
                     zbar_image_t *img)
{
    recycle_syms(iscn, img);

    /* get grayscale image, convert if necessary */
    img = zbar_image_convert(img, fourcc('Y','8','0','0'));
    if(!img)
        return(-1);

    unsigned w = zbar_image_get_width(img);
    unsigned h = zbar_image_get_height(img);
    const uint8_t *data = zbar_image_get_data(img);

    /* FIXME less arbitrary lead-out default */
    int quiet = w / 32;
    if(quiet < 8)
        quiet = 8;

    int density = CFG(iscn, ZBAR_CFG_Y_DENSITY);
    if(density > 0) {
        const uint8_t *p = data;
        int x = 0, y = 0;

        int border = (((h - 1) % density) + 1) / 2;
        if(border > h / 2)
            border = h / 2;
        movedelta(0, border);

        if(zbar_scanner_new_scan(iscn->scn))
            symbol_handler(iscn, x, y);

        while(y < h) {
            zprintf(32, "img_x+: %03x,%03x @%p\n", x, y, p);
            while(x < w) {
                ASSERT_POS;
                if(zbar_scan_y(iscn->scn, *p))
                    symbol_handler(iscn, x, y);
                movedelta(1, 0);
            }
            quiet_border(iscn, quiet, x, y);

            movedelta(-1, density);
            if(y >= h)
                break;

            zprintf(32, "img_x-: %03x,%03x @%p\n", x, y, p);
            while(x > 0) {
                ASSERT_POS;
                if(zbar_scan_y(iscn->scn, *p))
                    symbol_handler(iscn, x, y);
                movedelta(-1, 0);
            }
            quiet_border(iscn, quiet, x, y);

            movedelta(1, density);
        }
    }

    density = CFG(iscn, ZBAR_CFG_X_DENSITY);
    if(density > 0) {
        const uint8_t *p = data;
        int x = 0, y = 0;

        int border = (((w - 1) % density) + 1) / 2;
        if(border > w / 2)
            border = w / 2;
        movedelta(border, 0);

        while(x < w) {
            zprintf(32, "img_y+: %03x,%03x @%p\n", x, y, p);
            while(y < h) {
                ASSERT_POS;
                if(zbar_scan_y(iscn->scn, *p))
                    symbol_handler(iscn, x, y);
                movedelta(0, 1);
            }
            quiet_border(iscn, quiet, x, y);

            movedelta(density, -1);
            if(x >= w)
                break;

            zprintf(32, "img_y-: %03x,%03x @%p\n", x, y, p);
            while(y >= 0) {
                ASSERT_POS;
                if(zbar_scan_y(iscn->scn, *p))
                    symbol_handler(iscn, x, y);
                movedelta(0, -1);
            }
            quiet_border(iscn, quiet, x, y);

            movedelta(density, 1);
        }
    }

    /* release reference to converted image */
    zbar_image_destroy(img);
    return(iscn->img->nsyms);
}