使用 zbar-0.10 扫描 jpeg图片简单例子
#include <config.h>
#ifdef HAVE_INTTYPES_H
# include <inttypes.h>
#endif
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <zbar.h>
#define fourcc(a, b, c, d) \
((uint32_t)(a) | ((uint32_t)(b) << 8) | \
((uint32_t)(c) << 16) | ((uint32_t)(d) << 24))
static void decode_callback(zbar_image_t *image, const void *userdata)
{
const zbar_symbol_t *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);
printf("decoded type:%s\r\ncontext:%s\n", zbar_get_symbol_name(typ), data);
}
}
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_processor_set_data_handler(proc, decode_callback, NULL);
zbar_image_t *img = zbar_image_create();
zbar_image_set_size(img, 8, 8);
zbar_image_set_format(img, fourcc('J','P','E','G'));
FILE* fd;
char* pic_data;
int size;
struct timeval tv;
gettimeofday(&tv, NULL);
long long timestamp_start = tv.tv_sec*1000 + tv.tv_usec/1000;
printf("timestamp:%lld\n", timestamp_start);
fd = fopen("1.jpeg", "r");
if (fd) {
fseek(fd, 0, SEEK_END);
size = ftell(fd);
fseek(fd, 0, SEEK_SET);
pic_data = malloc(size);
fread(pic_data, 1, size, fd);
fclose(fd);
zbar_image_set_data(img, pic_data, size, NULL);
zbar_process_image(proc, img);
free(pic_data);
zbar_image_free_data(img);
}
long long timestamp_pic_1 = tv.tv_sec*1000 + tv.tv_usec/1000;
printf("timestamp_now:%lld\n", timestamp_pic_1);
printf("need time:%lld\n", timestamp_pic_1-timestamp_start);
fd = fopen("2.jpeg", "r");
if (fd){
fseek(fd, 0, SEEK_END);
size = ftell(fd);
fseek(fd, 0, SEEK_SET);
pic_data = malloc(size);
fread(pic_data, 1, size, fd);
fclose(fd);
zbar_image_set_data(img, pic_data, size, NULL);
zbar_process_image(proc, img);
free(pic_data);
zbar_image_free_data(img);
}
long long timestamp_pic2 = tv.tv_sec*1000 + tv.tv_usec/1000;
printf("timestamp_end:%lld\n", timestamp_pic2);
printf("need time:%lld\n", timestamp_pic2-timestamp_pic_1);
zbar_image_destroy(img);
zbar_processor_destroy(proc);
return(0);
}