Writing a C program using Libinsane
The C API is described in subprojects/libinsane/include/libinsane/capi.h
(specifically struct lis_api).
You can find an example program in subprojects/libinsane/examples. For instance, subprojects/libinsane/examples/lis_scan.c .
static void lets_scan(struct bmp *out, const char *dev_id)
{
#define CHECK_ERR(call) do { \
err = call; \
if (LIS_IS_ERROR(err)) { \
fprintf( \
stderr, "%s(L%d): ERROR: %X, %s\n", \
__FILE__, __LINE__, \
err, lis_strerror(err) \
); \
fflush(stderr); \
goto end; \
} \
} while(0)
char img_buffer[32*1024];
size_t bufsize;
size_t obtained = 0;
printf(
"Will use API %s\n", impl->
base_name);
if (dev_id != NULL) {
printf("Will use device %s\n", dev_id);
} else {
));
if (dev_infos[0] == NULL) {
fprintf(stderr, "No scan device found\n");
return;
}
printf("Will use device %s %s (%s ; %s)\n",
dev_infos[0]->vendor, dev_infos[0]->model,
dev_infos[0]->type,
dev_infos[0]->dev_id);
dev_id = dev_infos[0]->
dev_id;
}
CHECK_ERR(impl->
get_device(impl, dev_id, &device));
printf("Will use source '%s'\n", sources[0]->name);
printf("Setting resolution to 300\n");
printf("Setting mode to Color\n");
CHECK_ERR(sources[0]->scan_start(sources[0], &scan_session));
memset(img_buffer, 0, sizeof(img_buffer));
scan_session, ¶meters
));
printf(
"Scan will be: %d px x %d px (%ld bytes)\n",
parameters.width, parameters.height,
(long)parameters.image_size
);
write_header(out, ¶meters);
bufsize = sizeof(img_buffer);
scan_session, img_buffer, &bufsize
);
CHECK_ERR(err);
assert(bufsize == 0);
sleep(1);
continue;
}
obtained += bufsize;
printf(
"\r%ld KB / %ld KB",
(long)(obtained / 1024),
(long)(parameters.image_size / 1024)
);
fflush(stdout);
write_pixels(out, img_buffer, bufsize);
}
}
printf("\nAll done !\n");
end:
if (device != NULL) {
}
if (impl != NULL) {
}
#undef CHECK_ERR
}
Compiling a C program using Libinsane
Libinsane is designed to be used with GCC or Clang.
Please keep in mind that Libinsane is under LGPL. If your program is not under LGPL or GPL, you must compile against Libinsane dynamically, not statically. Users must remain free to modify Libinsane.
If Libinsane has been installed system-wide, you can use pkg-config to get the correct compilation flags:
pkg-config --cflags --libs libinsane
For example:
gcc -Wall -Werror `pkg-config --cflags --libs libinsane` -o test test.c