This is a read-only archive. Find the latest Linux articles, documentation, and answers at the new Linux.com!

Linux.com

Feature

How to configure a scanner's buttons on Linux

By Manolis Tzanidakis on January 02, 2007 (8:00:00 AM)

Share    Print    Comments   

While not perfect, support for scanners on Linux is constantly improving; just check the list of devices supported by SANE (Scanner Access Now Easy -- the scanning suite for Linux and other Unix-like systems). One thing that's still missing though is a way to make a scanner's buttons work on Linux. That's what this tutorial is about.

I run Ubuntu 6.10 (Egdy Eft) on my desktop system, but all required programs are available on most Linux distributions and, possibly, BSD variants. The actual programs you will need for this setup are:

scanimage, the scanning command of the SANE package. In Ubuntu/Debian this command is included in the sane-utils package.

convert, part of the ImageMagick image manipulation suite. We will use this program to convert the scanned pictures to different formats. On Ubuntu/Debian install the imagemagick package. This article is a nice introduction to the powerful ImageMagick suite.

scanbuttond is the daemon program that does all the dirty work of running commands when a scanner button is pressed. For more information check its home page. On Ubuntu and Debian (testing and unstable only) install the scanbuttond package.

Optionally, you can also install the zenity package (installed by default on Ubuntu) to print nice, graphical error messages when something goes wrong.

Before continuing you need to make sure that your scanner operates correctly under Linux. A previous tutorial, How to share a scanner on your network, contains information on how to set up your scanner on Linux. Read the first part of that article and make sure that running scanimage -L as your regular, non-root user lists your scanner. This command on my system returns:

device `plustek:libusb:004:002' is a Epson Perfection 1260/Photo USB flatbed scanner

To start, you need to adjust scanimage's options manually to achieve the best scanning results. Grab a ruler and measure your scanner's actual scanning surface -- X and Y axis -- or find these values in your scanner's manual. Put a nice color picture on your scanner and run the command scanimage --device "plustek:libusb:004:002" --mode Color --resolution 300 --depth 8 -x 215 -y 297 > ~/test.pnm to scan the image and save it as test.pnm in your home directory. Make sure to replace plustek:libusb:004:002 with the device printed by running scanimage -L, and the values of -x and -y with the dimensions of your scanner. 215 and 297 are my scanner's scanning surface dimensions in mm (millimeters). Don't worry if you get these dimensions wrong; scanimage will round them to the correct values automatically. For example, when I entered 220 as the length of the X axis, scanimage returned:

scanimage: rounded value of br-x from 220 to 215

The other options supplied to the scanimage command instruct it to scan a picture in Color mode (--mode) with 8 bits per sample (--depth) and 300dpi resolution (--resolution). These options should be enough for most users. For all available options run scanimage -h | less and check the scanimage man page.

View the scanned picture test.pnm on your favorite image viewing program and adjust scanimage's options to your liking. Write the adjusted command down somewhere since you'll need it later.

Scanbuttond configuration

Scanbuttond has three configuration files -- meta.conf, buttonpressed.sh, and initscanner.sh -- all installed under the /etc/scanbuttond directory on Ubuntu. The first file, meta.conf, defines which scanner backend libraries will be used. You shouldn't need to edit it unless you test a beta version and you're told so by the program's developers.

Some scanners might need some extra initialization steps -- such as firmware uploading or running some commands -- before they can be used by scanbuttond. You can enter these commands into the initscanner.sh file, which is actually a shell script run by scanbuttond during initialization. For my scanner, which is supported by the plustek backend, I had to add scanimage -n to that file. For more information on using initscanner.sh, check the paragraph titled "Scanner initialization" on scanbuttond's README file (in Ubuntu/Debian it's installed as /usr/share/doc/scanbuttond/README.gz) and SANE's documentation for your scanner.

The last file, buttonpressed.sh, is the most interesting. It defines which commands should run when the buttons are pressed. It's a shell script that's run by scanbuttond with the button number as the first argument ($1) and the scanner device as the second ($2). By default the only thing it does is print the button's number when it's pressed. Run scanbuttond as your regular, non-root user and check its logs by running sudo tail -f /var/log/daemon.log on a virtual terminal. If scanbuttond starts without problems you should see something similar to this:

Dec 14 19:37:18 my_hostname scanbuttond: found scanner: vendor="Epson", 
product="Perfection 1260", connection="libusb", sane_name="plustek:libusb:004:002" Dec 14 19:37:18 my_hostname scanbuttond: scanbuttond started

Now press the scanner's buttons while keeping an eye on the log tail command's output to verify that all of your scanner's buttons work. You should see something like:

Dec 14 19:37:37 my_hostname scanbuttond: button 1 has been pressed.
Dec 14 19:37:37 my_hostname scanbuttond: button 1 has been released.

Now it's time for the fun part. Back up the original buttonpressed.sh file by renaming it to buttonpressed.sh.orig and paste the following as a new buttonpressed.sh file:

#!/bin/sh

# daemon's name
DAEMON=scanbuttond

# securely create temporary file to avoid race condition attacks
TMPFILE=`mktemp /tmp/$DAEMON.XXXXXX`

# lock file
LOCKFILE="/tmp/$DAEMON.lock"

# device name
DEVICE="$2"

# destination of the final image file (modify to match your setup)
DESTINATION="/home/manolis/Desktop/scanned_image.jpg"

# remove temporary file on abort
trap 'rm -f $TMPFILE' 0 1 15

# function: create lock file with scanbuttond's PID
mk_lock() {
	pidof $DAEMON > $LOCKFILE
}

# function: remove temporary and lock files
clean_up () {
	test -e $LOCKFILE && rm -f $LOCKFILE
	rm -f $TMPFILE
}

# function: check if lock file exists and print an error message with zenity (if it's installed)
chk_lock() {
	if [ -e $LOCKFILE ]; then
		if [ -x /usr/bin/zenity ]; then
			zenity --display :0.0 --title="Scanbuttond" --error \
			--text="Another scanning operation is currently in progress."
		fi
		exit 1
	fi
}

# function: the actual scan command (modify to match your setup)
scan() {
	scanimage --device-name "$DEVICE" --mode Color --resolution 300 \
			--depth 8 -x 215 -y 297 > $TMPFILE
}

case $1 in
	1)
	   # button 1 (scan): scan the picture & save it as $DESTINATION
	   chk_lock; mk_lock; scan; convert $TMPFILE -quality 85 -quiet $DESTINATION; clean_up
	   ;;
	2)
	   # button 2 (copy): scan the picture, convert it to postscript and print it
	   chk_lock; mk_lock; scan; convert $TMPFILE ps:- | lpr; clean_up
	   ;;
	3)
	   # button 3 (mail): scan the picture, save it as $DESTINATION and send it as an e-mail
	   # attachment with Evolution
	   chk_lock; mk_lock; scan; convert $TMPFILE -quality 85 -quiet $DESTINATION
	   evolution --display=:0.0 "mailto:?attach=$DESTINATION"; clean_up
	   ;;
	4)
	   # button 4 (web): unconfigured
	   echo "button 4 has been pressed on $2"
	   ;;
esac

This script looks more complicated than it actually is; each command is explained in detail in the included comments (lines starting with #). The lines you need to edit are marked with notes that say (modify to match your setup). To make a long story short it scans, copies, and emails (with Evolution) a picture when you press buttons 1, 2, and 3, respectively. The convert program is used to convert the scanned image to the JPEG format when you press buttons 1 and 3, and converts the image to PostScript (PS) when you press button 2. You can find more information about convert's features and options on its man page (man convert).

I left button 4 unconfigured as an exercise for the reader. You could try combining one of the utilities mentioned in this article with a scanner button to automate uploading scanned pictures to your Flickr account.

Make this new buttonpressed.sh file, as well as initscanner.sh, executable with chmod 755 /etc/scanbuttond/{buttonpressed,initscanner}.sh. Stop scanbuttond, if it's already running, with killall -TERM scanbuttond. To start scanbuttond automatically when you log in, add it to your desktop environment's session startup list. For example, on GNOME go to the menu System -> Preferences -> Sessions > Startup Programs.

Conclusion

When fine-tuned, scanbuttond can increase your productivity by automating your basic scanning tasks, or you can use it just to impress your friends at your local LUG. It can also be used for fun -- you could for instance combine it with a USB Missile Launcher and play a prank on your colleagues.

Share    Print    Comments   

Comments

on How to configure a scanner's buttons on Linux

Note: Comments are owned by the poster. We are not responsible for their content.

so where's the parallel port support ?

Posted by: Anonymous Coward on January 03, 2007 10:28 AM
for my HP ( HP says they "support"
linux).. Prima/Colorado parallel port
600P scanner ?

It's still connected to my win98se box
because linux can't use it..

#

Re:so where's the parallel port support ?

Posted by: Anonymous Coward on January 03, 2007 10:52 PM
I was given a lovely shinny new Dell USB scanner/printer combination. Works great in Windows, I can even route the USB through to a VM if I don't want to reboot. As the website says, supported only under Windows. I can't find documentation on what the printer/scanner chips inside actually are so hunting down a potential FOSS support project isn't happening.

#

Nice Article!

Posted by: Anonymous Coward on January 03, 2007 03:42 AM
I like this article. I addresses a complex Linux issue in a simple and straightforward manner.

It is an unfortunate fact of life that scanning is still an problematic experience on Linux. Between a dearth of drivers, missing SANE support and the inability to use hardware controls such as front-panel buttons and difficulty with networking it can leave a bad taste in your mouth.

While there seems to be a fair bit of support for Epson devices and their excellent image quality, many Epson devices suffer problems like clogged print heads that require replacing the entire printer/scanner! Epsom 5400 and 5600 owners know about this all too well.

I was very happy to find my HP Photosmart 2575xi all-in-one. It has the usual USB connection for printing and scanning and though not officially supported by the SANE-Project, you can make it work. The 2575xi also has a built in network interface and scanning can be done over the network with zero configuration. To scan over the network simply access the printer via any web browser, such as Firefox and click scan. Within seconds, your browser has a PDF or Jpeg scan for you. Sweet! But, the 2575 has been discontinued which is a real bummer.

The up side is that it looks like the HP Photosmart C5180 All-in-One offers the same functionality.

#

Hmm

Posted by: Anonymous Coward on January 03, 2007 10:34 AM
I never use scanners.

I have a some buttons on my keyboard, calc, web, etc.

I would like to make it open calculator, web browser (Mozilla Firefox), and console/terminal on these keys I have located over the F1-F12 buttons.

#

Re:Hmm

Posted by: Anonymous Coward on January 03, 2007 12:35 PM
Depending on what type of keyboard you have and what DE/distro you are using, there are several ways to get most, if not all, of your buttons working. A really nice application to look at is keytouch:

<a href="http://keytouch.sourceforge.net/about.php" title="sourceforge.net">http://keytouch.sourceforge.net/about.php</a sourceforge.net>

#

Re:Hmm

Posted by: Anonymous Coward on January 03, 2007 08:05 PM
This isn't the scope of this article I'm afraid... however, here is one:

<a href="http://www.linuxgangster.org/modules.php?name=Content&file=viewarticle&id=17" title="linuxgangster.org">http://www.linuxgangster.org/modules.php?name=Con<nobr>t<wbr></nobr> ent&file=viewarticle&id=17</a linuxgangster.org>

#

Off topic but I gotta ask

Posted by: Anonymous Coward on January 03, 2007 11:18 PM
Anyone have an idea how to setup or at least what chips are inside the Dell AIO 810 USB printer/Scanner.

Also, how can I get the damn USB ports to take static (as in static IP) usb device locations? I've a flashdrive that jumps all over the USB bus and the previously mentioned scanner which I'd love to see becoming usable.

#

Re:Off topic but I gotta ask

Posted by: Anonymous Coward on January 05, 2007 01:15 AM
you can use devices not only by classical filedev names like:

hard drives:<nobr> <wbr></nobr>/dev/hda1

input devices like mouse:<nobr> <wbr></nobr>/dev/input/mouse0 or<nobr> <wbr></nobr>/dev/input/event0 (/dev/input/js0 or<nobr> <wbr></nobr>/dev/input/event0 alsa for joysticks nad other devices usung evdev interface)

In new kernel you have 'devices' in '/dev' also in other places with new scemes for names.
I will give example for my hard drive, but there are also other similiar naming schemes(to understa nd try to search for 'UDEV' and 'HAL'.
You can still acces drives in old way, but now you can acces also them by:
(normally<nobr> <wbr></nobr>/dev/hda)

-'by-id' i.e<nobr> <wbr></nobr>/dev/disk/by-id/ata-SAMSUNG_SP2514N_S08BJ1JL90068<nobr>9<wbr></nobr>
this was for all hdd, but you can also acces partion, i.e. ata-SAMSUNG_SP2514N_S08BJ1JL900689-part1
(its something like<nobr> <wbr></nobr>/dev/hda1)

-'by-label' i.e.<nobr> <wbr></nobr>/dev/disk/by-label/BOOT
I think you understand that every partion has theyr own label(on windows too). There can be problem if more than one partition has identical LABEL

-'by-label' i.e.<nobr> <wbr></nobr>/dev/disk/by-uuid/20671e62-9754-4771-87ca-cdd265c<nobr>a<wbr></nobr> 345b
Every partiotion has their UUID, which is randomly created(during disk creation, or set manually later) unique identifier. Its impossible(not really) to have 2 partitions with identical UUID by accident. Ubuntu use this scheme in fstab, so you can add partion beetwen now existed, numbers of partions will change(so<nobr> <wbr></nobr>/dev/hda3 will be<nobr> <wbr></nobr>/dev/hda4 i.e.), but you will be able to access them still by same UUID which is written on partition.

-"by-id" i.e.<nobr> <wbr></nobr>/dev/disk/by-path/pci-0000:00:11.1-ide-0:0
This show first partition on first disk connected to the IDE controller which is plugged into pci(virtually, its onboard integrated controller) bus with number 0000:00:11.1.

A dont have any scanner now, so a gaved examples with hdd, but i hope it will be similiar with scanners.

#

Re:Off topic but I gotta ask - Cheers also

Posted by: Anonymous Coward on January 05, 2007 11:31 PM
I knew Ubuntu and a few other distro had moved away from the<nobr> <wbr></nobr>/dev/hda1 style naming but not how I might do so with my own prefered distro.

I'll have to digest this a little more but we'll see if Mandriva shows me some love.

#

Re:Off topic but I gotta ask

Posted by: Administrator on January 04, 2007 02:24 PM
Your Dell AIO scanner/printer is, most likely, a Lexmark unit. I hate to break it to you, but most Lexmark printing/scanning hardware is absolute junk.

#

Re:Off topic but I gotta ask - Cheers

Posted by: Anonymous Coward on January 05, 2007 11:27 PM
Cheers, I'll see if there's any joy from trying it as a Lexmark.

I didn't figure it was top of the line based on it being the free inclusion with a new Dell notebook and provides no drivers for non-Windows OS. I'm not one to look a free "here, can you make use of this?" in the mouth though and it covers my limited needs well enough.

#

convert has limitations.

Posted by: Anonymous Coward on January 07, 2007 11:10 PM
I have found that <tt>cjpeg</tt> from the libjpeg package works better at creating a Jpeg from a PPM file than <tt>convert</tt> from ImageMagick.

#

Kscannerbuttons

Posted by: Anonymous Coward on January 08, 2007 07:46 AM
There is another project Kscannerbuttons to readout the status of the scannerbuttons. It consists of a daemon which reads out the buttons via SANE and a nice KDE configuration tool. Please check the projects website <a href="http://jice.free.fr/KScannerButtons/" title="jice.free.fr">http://jice.free.fr/KScannerButtons/</a jice.free.fr>

#

some more ideas and how to fax

Posted by: Administrator on January 25, 2007 07:29 PM

I run scanbuttond at boot time. You should save the following script as<nobr> <wbr></nobr>/etc/init.d/scanbuttond, then run

<tt>update-rc.d scanbuttond defaults 90</tt>

Next time you reboot, scanbuttond will be started automatically. If you want to start or stop it manually, do

<nobr> <wbr></nobr>
<tt>/etc/init.d/scanbuttond start</tt>
or
<nobr> <wbr></nobr>
<tt>/etc/init.d/scanbuttond stop</tt>
respectively. Here is the boot script:
<tt>#!/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/s<nobr>b<wbr></nobr> in:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/bin/scanb<nobr>u<wbr></nobr> ttond
NAME=scanbuttond
DESC="scanner buttons daemon"

test -x $DAEMON || exit 0

set -e

case "$1" in
  start)
    echo -n "Starting $DESC: "
    start-stop-daemon --start --quiet --exec $DAEMON
    echo "$NAME."
   <nobr> <wbr></nobr>;;
  stop)
    echo -n "Stopping $DESC: "
    start-stop-daemon --stop --oknodo --quiet --exec $DAEMON
    echo "$NAME."
   <nobr> <wbr></nobr>;;
  restart|force-reload)
    echo -n "Restarting $DESC: "
    start-stop-daemon --stop --oknodo --quiet --exec $DAEMON
    sleep 1
    start-stop-daemon --start --quiet --exec $DAEMON
    echo "$NAME."
   <nobr> <wbr></nobr>;;
  *)
    N=/etc/init.d/$NAME
    echo "Usage: $N {start|stop|restart|force-reload}" >&2
    exit 1
   <nobr> <wbr></nobr>;;
esac

exit 0</tt>

In<nobr> <wbr></nobr>/etc/scanbuttond/initscanner.sh I have this. I am not sure whether this is needed, but it solved some mysterious problem for me, and apparently it does no harm:

<tt>#!/bin/sh

# This script is executed whenever scanbuttond
# finds new devices.
# If your scanner needs a firmware upload or any other kind
# of special action to be taken on initialization, put
# the appropriate command here.

scanimage -n</tt>

And this is my<nobr> <wbr></nobr>/etc/scanbuttond/buttonpressed.sh. I configured three buttons to respectively fax, photocopy in b&w, and photocopy in colour. The script contains a comment detailing the packages that you need to install in order to have it work. Additionally, you need an installed and working cups system. The script logs what it does and possible errors at<nobr> <wbr></nobr>/var/log/scanbuttond.log. Additional explanations are below.

<tt>#!/bin/sh

# This script is started by scanbuttond whenever a scanner button has been pressed.
# Scanbuttond passes the following parameters to us:
# $1<nobr> <wbr></nobr>... the button number
# $2<nobr> <wbr></nobr>... the scanner's SANE device name, which comes in handy if there are two or
#        more scanners. In this case we can pass the device name to SANE programs
#        like scanimage.

exec >><nobr> <wbr></nobr>/var/log/scanbuttond.log
exec 2>&1

echo
echo -- $0 started on `date`

LOCKFILE="/tmp/$2.lock"    # lock the scanner

FAXPRINTER="-P fax"    # a fax printer
COLPRINTER="-P HP"        # a colour printer
BWPRINTER="-P bn"        # a B&W printer
PRES="--resolution 300 --contrast 15 --brightness 0"
FRES="--resolution 400 --contrast 30 --brightness 0"

USER=me            # access to the X server
XD=:0                # which X server display

case $1 in
    3)
        echo "button 3 has been pressed on $2 (fax)"

        # Fax.
        # Requires efax-gtk, sane-utils, lockfile-progs and netpbm.
        DISPLAY=$XD su $USER -c 'efax-gtk -s' & # start the fax server
        if ! lockfile-create --retry 2 $LOCKFILE; then
          echo "Error: scanning already in progress for $2"
          exit
        fi
        #set -x
        scanimage --verbose --device-name $2 \
            --mode Gray -x 210 -y 297 $FRES |
          pnmtops -width=8.27 -height=11.69 |
          lpr -J $2 $FAXPRINTER
        lockfile-remove $LOCKFILE
       <nobr> <wbr></nobr>;;
    2)
        echo "button 2 has been pressed on $2 (b&w copy)"

        # B^W photocopy.
        # Requires sane-utils, lockfile-progs and netpbm.
        if ! lockfile-create --retry 2 $LOCKFILE; then
          echo "Error: scanning already in progress for $2"
          exit
        fi
        #set -x
        scanimage --verbose --device-name $2 \
            --mode Gray -x 210 -y 297 $PRES |
          pnmtops -width=8.27 -height=11.69 |
          lpr -J $2 $BWPRINTER
        lockfile-remove $LOCKFILE
       <nobr> <wbr></nobr>;;
    1)
        echo "button 1 has been pressed on $2 (color copy)"

        # Color photocopy.
        # Requires sane-utils, lockfile-progs and netpbm.
        if ! lockfile-create --retry 2 $LOCKFILE; then
          echo "Error: scanning already in progress for $2"
          exit
        fi
        #set -x
        scanimage --verbose --device-name $2 \
            --mode Color -x 210 -y 297 $PRES |
          pnmtops -width=8.27 -height=11.69 |
          lpr -J $2 $COLPRINTER
        lockfile-remove $LOCKFILE
       <nobr> <wbr></nobr>;;
    4)
        echo "button 4 has been pressed on $2"
       <nobr> <wbr></nobr>;;
esac</tt>

The B&W and colour phtocopies work by sending the print job to different printers. You must configure your cups with two printers, and in the printer options configure one of them to print in B&W. The only reason to do that is to save colour ink, so don't bother to do that if you don't care about ink saving.

The fax works by using efax-gtk together with cups. First install efax-gtk, configure it for your modem, then create a cups printer: call it <tt>fax</tt>, then choose a network printer of type <tt>HP JetDirect</tt>, host <tt>localhost</tt>, port <tt>9900</tt>. When you print anything to that printer, it goes to the efax-gtk daemon. In the above script, the daemon is called as the user <tt>me</tt>, which you should change to the user that most often is using the X server. If that user is not logged, the daemon will not start, and the print job will stay queued at the printer until the daemon comes up, perhaps because you call it explicitly from a terminal or from the menu.

The daemon by default pops up a window asking for the telephone number when it receives a fax request from the 9900 socket. If you configured your modem correctly, once you give the number everything should go on, and the fax should be sent.

Note for Italian users: in Italy phones do not use a tone. So X3 should be added to the modem initialisation string. This is the string I use, in the Initialization Params box of the efax-gtk settings:

<tt>Z &FE&D2S7=120 X3 &C0 M1L0</tt>

#

This story has been archived. Comments can no longer be posted.



 
Tableless layout Validate XHTML 1.0 Strict Validate CSS Powered by Xaraya