LibInsane 1.0.10
Cross-platform Cross-API Cross-driver Cross-image-scanner Image Scan Library
Loading...
Searching...
No Matches
How to use Libinsane in a C program

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)
enum lis_error err;
struct lis_api *impl = NULL;
struct lis_device_descriptor **dev_infos;
struct lis_item *device = NULL;
struct lis_item **sources;
struct lis_scan_parameters parameters;
struct lis_scan_session *scan_session;
char img_buffer[32*1024];
size_t bufsize;
size_t obtained = 0;
CHECK_ERR(lis_safebet(&impl));
printf("Will use API %s\n", impl->base_name);
if (dev_id != NULL) {
printf("Will use device %s\n", dev_id);
} else {
CHECK_ERR(impl->list_devices(
impl, LIS_DEVICE_LOCATIONS_ANY, &dev_infos
));
if (dev_infos[0] == NULL) {
fprintf(stderr, "No scan device found\n");
return;
}
// let's use the first scan device found, because it looks cool.
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));
CHECK_ERR(device->get_children(device, &sources));
// Normalizers ensure us that there is at least one source,
// so let's use the first one because it looks cool too.
printf("Will use source '%s'\n", sources[0]->name);
// Setting resolution: This one may or may not work, depending on
// the scanner
printf("Setting resolution to 300\n");
lis_set_option(sources[0], OPT_NAME_RESOLUTION, "300");
// Normalizers ensure us that the mode option has the value "Color"
// by default (still put here for the example)
printf("Setting mode to Color\n");
CHECK_ERR(lis_set_option(sources[0], OPT_NAME_MODE, "Color"));
// Normalizers ensure us that by default, the maximum scan area will
// be used
CHECK_ERR(sources[0]->scan_start(sources[0], &scan_session));
memset(img_buffer, 0, sizeof(img_buffer));
while (!scan_session->end_of_feed(scan_session)) {
// scan parameters must be obtained *after* the scan session has been
// started and before getting the content of a new page.
// DO NOT assume that 2 pages in the same feed will have the same size.
CHECK_ERR(scan_session->get_scan_parameters(
scan_session, &parameters
));
printf(
"Scan will be: %d px x %d px (%ld bytes)\n",
parameters.width, parameters.height,
(long)parameters.image_size
);
write_header(out, &parameters);
while (!scan_session->end_of_page(scan_session)) {
bufsize = sizeof(img_buffer);
err = scan_session->scan_read(
scan_session, img_buffer, &bufsize
);
CHECK_ERR(err);
if (err == LIS_WARMING_UP) {
// old scanners need warming time.
// No data has been returned.
assert(bufsize == 0);
sleep(1);
continue;
}
obtained += bufsize;
printf(
"\r%ld KB / %ld KB",
(long)(obtained / 1024),
(long)(parameters.image_size / 1024)
);
fflush(stdout);
// do something with the chunk of the image/page that
// has just been scanned
// here for example we write simply to a BMP file
write_pixels(out, img_buffer, bufsize);
}
// do something with the whole image/page that has just been scanned
}
// do something with all the images/pages that have just been scanned
printf("\nAll done !\n");
end:
if (device != NULL) {
device->close(device);
}
if (impl != NULL) {
impl->cleanup(impl);
}
#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