Camron Cade

Artisan web development, mostly using Laravel.

Cloud Scanner with Raspberry Pi & Fujitsu ix500

My end goal here is to have a completely headless Raspberry Pi.

Push the scan button, and the Pi does all the work scanning, processing, and putting the outputted files in a shared network folder.

Here's what I did to get this working.

1. Install Raspbian

Follow any guide - there're everywhere. I bought a kit that included a SD card with NOOBs on it so it was pretty straight forward for me.

I also turned on SSH in the settings menu so I could complete the rest of this from my desktop computer. Much easier.

2. Install and Setup Programs

Start with a basic update

$ sudo apt-get update

Now to make sure we have SANE, which will interface with our scanner.

$ sudo apt-get install sane

Luckily, the Fujitsu ix500 works with SANE out of the box. You can check that your scanner works by running the following - you should see your scanner pop up.

$ sudo scanimage --list-devices

Now to grab the rest of the bunch:

$ sudo apt-get install scanbuttond vim imagemagick bc exactimage pdftk tesseract-ocr tesseract-ocr-eng wget wput libusb-dev build-essential

Note: I'm using scanbd (included in the scannerbuttond package) to respond to button presses on my scanner. I spent a good amount of time racking my brain on why I couldn't get it to work following the instructions on the Wiki.

Turns out when you install it via apt-get, and for the way I wanted to use it, there was no need to configure it via the instructions.

3. Setup your scan script

I need a script that scanbd can run in response to a button press on the scanner.

I used a script from this guide as a starting place and modified it to meet my needs. This includes a bit of tweaking to get a decent-looking output pdf file.

I saved this at ~/scripts/scan.sh.

#!/bin/bash

OUT_DIR=~/scans/Today  
TMP_DIR=`mktemp -d`  
FILE_NAME=scan_`date +%Y-%m-%d-%H%M%S`  
LANGUAGE="eng"                 # the tesseract language - ensure you installed it

echo 'scanning...'  
scanimage --resolution 300 \  
          --batch="$TMP_DIR/scan_%03d.pnm" \
          --format=pnm \
          --mode Gray \
          --source 'ADF Front'
echo "Output saved in $TMP_DIR/scan*.pnm"

cd $TMP_DIR

for i in scan_*.pnm; do  
    echo "${i}"
    convert "${i}" "${i}.tif"
done

# do OCR
echo 'doing OCR...'  
for i in scan_*.tif; do  
    echo "${i}"
    tesseract "$i" "$i" -l $LANGUAGE hocr
    hocr2pdf -i "$i" -s -o "$i.pdf" < "$i.hocr"
done

# create PDF
echo 'creating PDF...'  
pdftk *.tif.pdf cat output "compiled.pdf"

gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/ebook -dNOPAUSE -dQUIET -dBATCH -sOutputFile="$FILE_NAME.pdf" compiled.pdf

cp $FILE_NAME.pdf $OUT_DIR/

rm -rf $TMP_DIR  

Make sure the file is executable:

$ chmod 777 ~/scripts/script.sh

Tell scanbd which file to run. Edit /etc/scanbd/scanbd.conf:

Edit the script= line within the action scan {} block. Set it to point to the script we just saved.

action scan {  
                filter = "^scan.*"
                numerical-trigger {
                        from-value = 1
                        to-value   = 0
                }
                desc   = "Scan to file"
                # script must be an relative path starting from scriptdir (see above),
                # or an absolute pathname.
                # It must contain the path to the action script without arguments
                # Absolute path example: script = "/some/path/foo.script
                script = "/home/pi/scripts/scan.sh"
        }

Quick test

At this point, a quick test to make sure everything's working properly would be smart.

  1. Put a document in the scanner
  2. Run $ scanbd -f.
  3. Wait about 15 seconds.
  4. Press the scan button.

If all has been setup correctly, and your scanner is supported by SANE and scanbd, it should scan and output to the directory you specified in the scan script. Make sure the output directory exists.

4. Setup scanbd to autorun on boot

This part also gave me trouble. None of the ways the Wiki or other online guides suggested setting this up to run worked for me.

What I ended up doing was adding the following the the ~/.profile file.

nohup scanbd -f &  

This runs scanbd on boot, but detaches it from the main shell. If you later ssh into your Pi and want to manage this, here are a few useful commands.

See if the job is running:

$ jobs

Kill the job: (replace # with the id-number shown from the above command)

kill %#  

5. Setup shared network folder

I guess calling this a cloud scanner wasn't entirely accurate because I ended up only needing access to the scans from computers on the same wireless network.

I'm sure you could set this up for out-of-network access using any of the Raspberry Pi cloud programs that are out there.

I won't rewrite what I did to get this setup because it's been done before.

I used a combination of this guide and this guide to setup the shared network folder.


For errors, questions, suggestions, or anything else, leave a comment below - I'm always happy to chat.