LoginSignup
29
29

More than 5 years have passed since last update.

RaspberryPiで遊んでみたーその2 ScanSnap S1500 をRaspberryPiに繋いでスキャンしたものをDropboxにアップロードする

Last updated at Posted at 2015-08-29

???「なんで学校からのお手紙、すぐどっかいってまうん?」

というわけで、学校からのお手紙をScanSnapで読み込んで保存していたりしたわけですが、いちいちパソコンを起動したりが意外とめんどくさくて、取り込まなくなった・・・ら、やっぱりお手紙がどっかいってまうんですよね。

色々構想を練ったり、テストをしたりしているうちに行き着いたのが、下の参考サイトにあるような

ScanSnapにRaspberryPiを繋ぐ→スマホのWebブラウザを操作盤にしてスキャン→自動的にドロップボックスにアップロード

という方法です

参考サイト: d.sunnyone.org
ScanSnap fi-5110EOXをDebian/Ubuntuで使う
SheevaPlugを使ってScanSnap fi-5110EOXをネットワーク対応スキャナにする

ではいってみよーー


RaspiScan用のユーザを作る

$ sudo adduser raspiscan
$ sudo vi /etc/sudoers

スキャナはroot権限を必要とするため、/etc/sudoersに以下の一行を追加

/etc/sudoers

raspiscan ALL=(ALL) NOPASSWD: ALL   ←追記
$ su - raspiscan

saneとimagemagickをインストールする

$ sudo apt-get install sane
$ sudo apt-get install imagemagick

コマンドラインからスキャンする

まずはコマンドラインから

$ sudo /usr/bin/scanimage --batch --source "ADF Front" --mode Gray --resolution=190 --y-resolution=190 --format=tiff

カレントディレクトリに out1.tif ができていれば成功。

次はシェルスクリプトで

raspiscan.sh
#!/bin/sh

TMPDIR=/home/raspiscan/tmp

/bin/mkdir -p $TMPDIR
cd $TMPDIR

/usr/bin/scanimage --batch --source "ADF Front" --mode Gray --resolution=190 --y-resolution=190 --format=tiff
$ sudo ./raspiscan.sh

~/tmp に out1.tif ができていれば成功

ImageMagickでPDFに変換する

先程作った out1.tif をPDFに変換してみる
まずはコマンドラインから

$ sudo convert -compress JPEG -quality 85 -adjoin `ls -1 *.tif | sort -t t -k 2 -n` out.pdf

カレントディレクトリに out1.tif ができていれば成功。

次はシェルスクリプトで。先程の raspiscan.sh に以下を追記する。

raspiscan.sh
#以下を追記
convert -compress JPEG -quality 85 -adjoin `ls -1 *.tif | sort -t t -k 2 -n` out.pdf

一度 ~/tmp 以下を消してからスクリプトを実行する

$ sudo rm -rf tmp
$ sudo ./raspiscan.sh

ちゃんと ~/tmp/out.pdf が作られていたら成功

Dropboxにファイルをアップロードする

参考サイトほぼそのままなので、詳しくはそちらを参照してください

参考サイト: Raspberry PiでDropboxにファイルをアップロードしてみた

Dorpbox-Uploaderを入手する

/tmp ディレクトリにgitからcloneしてくる

$ cd /tmp
$ git clone https://github.com/andreafabrizi/Dropbox-Uploader/
$ sudo mv /tmp/Dropbox-Uploader/dropbox_uploader.sh /usr/local/bin/
$ cd /usr/local/bin/
$ chmod 755 dropbox_uploader.sh

アクティベーション

  • https://www.dropbox.com/developers/appsにアクセス
  • 「Create app」ボタンを押す
  • 「Dorpbox API app」を選択
  • 「Files and datastorers」を選択
  • 「Yes」を選択(アプリ専用フォルダのみアクセス)
  • アプリの名前を入力 例:raspiscan
  • 「Create app」を押すと「App key」「App secret」などが表示される
$ dropbox_uploader.sh

This is the first time you run this script.

 1) Open the following URL in your Browser, and log in using your account: https://www.dropbox.com/developers/apps
 2) Click on "Create App", then select "Dropbox API app"
 3) Select "Files and datastores"
 4) Now go on with the configuration, choosing the app permissions and access restrictions to your DropBox folder
 5) Enter the "App Name" that you prefer (e.g. MyUploader84512395614070)

 Now, click on the "Create App" button.

 When your new App is successfully created, please type the
 App Key, App Secret and the Permission type shown in the confirmation page: 

 # App key: [上で表示された「App key」を入力]
 # App secret: [上で表示された「App secret」を入力]

 Permission type:
 App folder [a]: If you choose that the app only needs access to files it creates
 Full Dropbox [f]: If you choose that the app needs access to files already on Dropbox

 # Permission type [a/f]: a

 > App key is ******, App secret is ****** and Access level is App Folder. Looks ok? [y/n]: y

 > Token request... OK

 Please open the following URL in your browser, and allow Dropbox Uploader
 to access your DropBox folder:

 --> https://www.dropbox.com/1/oauth/authorize?oauth_token=******

Press enter when done...

この状態で上のURLにアクセスして、RaspiScanフォルダへのアクセスを許可したら、Enterを押す。

「Setup completed!」と表示されたら成功。

アップロードテスト

まずはコマンドラインから

$ dropbox_uploader.sh upload /home/raspiscan/tmp/out.pdf test.pdf

> Uploading "/home/raspiscan/tmp/out.pdf" to "/test.pdf"... DONEと表示されたら成功。
実際にDropboxを見てみると、 test.pdf がアップロードされている。

次はスクリプトから

raspiscan.sh
#以下を追記
dropbox_uploader.sh upload /home/raspiscan/tmp/out.pdf test1.pdf

rm -rf tmp
$ sudo rm -rf tmp
$ sudo ./raspiscan.sh

test1.pdf がDropboxにアップロードされていれば成功

2015/11/8追記
このまま使用すると、日本語ファイル名が文字化けしてしまうので、以下のサイトを参考に、dropbox_uploader.shを少し書き換えた
CLI環境のLinuxでDropboxを使う - Dropbox-Uploader

書き換えたところは、327行目

/usr/local/bin/dropbox_uploader.sh
327             #* ) $PRINTF $PRINTF_OPT '%%%02x' "'$c"    この行をコメントアウトして
328             *) o=$($PRINTF "$c" | od -A n -t x1 | tr -d '\n' | tr ' ' '%') ;;    #この行を追加

これで日本語のファイルも文字化けせずにアップロードできる

$ dropbox_uploader.sh upload test.txt てすと.txt
 > Uploading "/home/raspiscan/test.txt" to "/てすと.txt"... DONE

次は操作盤を作るためのWebサーバを立てるよ
RaspberryPiで遊んでみたーその3 ScanSnapをブラウザから操作するための準備をする

29
29
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
29
29