📢 Webサイト閉鎖と移転のお知らせ
このWebサイトは2026年9月に閉鎖いたします。
新しい記事は移転先で追加しております。(旧サイトでは記事を追加しておりません)

40行目: 40行目:
  make j $(nproc)
  make j $(nproc)
  make install
  make install
<br>
==== 使用例 ====
Qtのプロジェクトファイルを設定する。<br>
<syntaxhighlight lang="make">
# QImageクラスを使用する場合は、guiを記述する
QT = core gui
# SANE library.
LIBS += \
    -lsane \
INCLUDEPATH += \
    /usr/include
</syntaxhighlight>
<br>
以下の例では、SANE backendライブラリを使用してスキャナから画像を取り込んでいる。<br>
<syntaxhighlight lang="c++">
#include <QCoreApplication>
#include <QImage>
#include <QFile>
#include <vector>
#include <memory>
#include <sane/sane.h>
#include <sane/saneopts.h>
#include <iostream>
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    // スキャナのハンドルを初期化
    SANE_Int version_code;
    SANE_Status status = sane_init(&version_code, nullptr);
    if (status != SANE_STATUS_GOOD) {
        std::cerr << "SANE init failed: " << sane_strstatus(status) << std::endl;
        return -1;
    }
    // 全てのデバイス情報を取得
    const SANE_Device **device_list;
    status = sane_get_devices(&device_list, SANE_FALSE);
    if (status != SANE_STATUS_GOOD) {
        sane_exit();
        std::cerr << "SANE get devices failed: " << sane_strstatus(status) << std::endl;
        return -1;
    }
    // ここでは、1番目のデバイスを使用する
    if (device_list[0] == nullptr) {
        sane_exit();
        std::cerr << "No SANE devices found." << std::endl;
        return -1;
    }
    // 使用するスキャナを決定
    SANE_Handle scanner;
    status = sane_open(device_list[0]->name, &scanner);
    if (status != SANE_STATUS_GOOD) {
        sane_exit();
        std::cerr << "SANE open failed: " << sane_strstatus(status) << std::endl;
        return -1;
    }
    // スキャナのオプションを設定
    status = sane_start(scanner);
    if (status != SANE_STATUS_GOOD) {
        sane_close(scanner);
        sane_exit();
        std::cerr << "SANE start failed: " << sane_strstatus(status) << std::endl;
        return -1;
    }
    SANE_Parameters params;
    status = sane_get_parameters(scanner, &params);
    if (status != SANE_STATUS_GOOD) {
        sane_close(scanner);
        sane_exit();
        std::cerr << "Failed to get scan parameters: " << sane_strstatus(status) << std::endl;
        return -1;
    }
    // 画像データの保存先
    QFile File("output.ppm");
    if (!File.open(QIODevice::WriteOnly)) {
        std::cerr << "Failed to open file for writing." << std::endl;
        sane_close(scanner);
        sane_exit();
        return -1;
    }
    // スマートポインタを使用する場合
    //std::unique_ptr<unsigned char[]> buffer = std::make_unique<unsigned char[]>(params.bytes_per_line * params.lines);
    // vectorクラスを使用する場合
    std::vector<unsigned char> buffer(params.bytes_per_line * params.lines);
    try {
        // PPMヘッダ情報を書き込む
        QString header = QString("P6\n%1 %2\n255\n").arg(params.pixels_per_line).arg(params.lines);
        File.write(header.toUtf8());
        // 画像をスキャン
        do {
            SANE_Int numRead = 0;
            // スマートポインタを使用する場合
            //status = sane_read(scanner, buffer.get(), params.bytes_per_line * params.lines, &numRead);
            // vectorクラスを使用する場合
            status = sane_read(scanner, buffer.data(), buffer.size(), &numRead);
            if (status != SANE_STATUS_EOF && status != SANE_STATUS_GOOD) {
                std::cerr << "Failed to read data" << std::endl;
                File.close();
                return -1;
            }
            // スマートポインタを使用する場合
            //File.write(reinterpret_cast<const char*>(buffer.get()), numRead);
            // vectorクラスを使用する場合
            File.write(reinterpret_cast<const char*>(buffer.data()), numRead);
        } while (status != SANE_STATUS_EOF);
    }
    catch (const std::exception &e) {
        std::cerr << e.what() << std::endl;
    }
    File.close();
    // PNG形式に変換
    QImage image("output.ppm");
    if (!image.load("output.ppm")) {
        std::cerr << "Could not read PPM image." << std::endl;
    }
    if (!image.save("output.png", "PNG")) {
        std::cerr << "Could not convert PNG image." << std::endl;
    }
    sane_close(scanner);
    sane_exit();
    std::cout << "Scan complete." << std::endl;
    return 0;
}
</syntaxhighlight>
<br><br>
<br><br>