Mar 10, 2011

Using the ADF of the Brother MFC-7420, MFC-7820N and MFC-7360N via the Scan-To keys in Ubuntu

In a previous post I talked about enabling the scan-to functionality under Ubuntu Linux, but the script presented there was only able to scan one page. Here now the solution for using the ADF (automatic document feeder) for scanning a larger amount of pages and getting a nice pdf out of it.

I recommend having one function of the Scan-To-Key using some version of the script posted in this article so that you can scan over the flatbed and another one for explicitly using the ADF. This script here is simplified for German A4 and plain conversion without any too fancy stuff and was inspired by this source.

Where to save the scripts and how to change the scripts executed for each of the 4 functions of the Scan-To-Key of the MFC-7420 and MFC-7820N is described here. It should work for the DCP-7025 as well.

Update 2013-01-20: All scripts can be used without adaption with a MFC-7360N. The only change is, that the new scan-to-key tool has moved all its files to /opt/brother/scanner

Prerequisites: you need to have the package sane-utils and libtiff4 installed.
sudo apt-get install sane-utils libtiff4
This is the script for copy & paste
#! /bin/sh
set +o noclobber
#
# $1 = scanner device
# $2 = friendly name
#

device=$1
outputdir="$HOME/Desktop/brscan"
tmptemplate="brscan-XXXXXX" 

# template for creating the temporary files
outtemplate1="brscan_XXXX"
resolution=400 

# 100|150|200|300|400|600|1200|2400|4800|9600
mode="Black" 

# Black & White|Gray[Error Diffusion]
# |True Gray|24bit Color
# Create outputdir if it does not exist
mkdir -p $outputdir
# Create temp-directory within the outputdir
tmp_dir=`mktemp -d $outputdir/$tmptemplate`
output_file=`mktemp $outputdir/$outtemplate1.pdf`
touch $output_file
# From Brother, don't know what it is for
if [ "`which usleep`" != '' ];then
usleep 10000
else
sleep 0.01
fi
#
echo "scan from $2($device) to $tmp_dir"
# ================================================
# use scanadf:
scanadf -d "$device" -o "$tmp_dir/tmp-image%02d" \

-e -y 298 --resolution 400 --mode "$mode"
image=`mktemp $tmp_dir/tif-XXXXX`
# echo "created temporary tif file $image"
# tiffcp does not support B&W jpegs
if [ "$mode" = "Black" \

  -o "$mode" = "Gray[Error Diffusion]" ]
then
comp="zip"
else
comp="jpeg"
fi
#echo "Converting pnm to tiff in folder $tmp_dir"
for spass in $tmp_dir/tmp-image*
do pnmtotiff "$spass" > "$spass.tiff"
rm $spass # and deleting pnm-file
done
#echo "Put together tiff-files"
tiffcp -s -r 32 -c $comp $tmp_dir/*.tiff $image
#echo "Create pdf"
tiff2pdf -x$resolution -y$resolution \

   -o"$output_file" "$image"
#
# echo "Cleanup"
rm $image
chmod 600 "$output_file"
rm $tmp_dir/*
rmdir $tmp_dir
Update:
I adjusted the script with the inspiration mentioned above to one, which graphically asks for scanning the back sides of the pile and sorting it correctly into a pdf, you will need to have the packages libtiff-tools and zenity installed:
#! /bin/sh
set +o noclobber
# ======================================================
# This script is intended to scan many pages
# over the ADF of a Brother MFC-7420 or MFC-7820N
# and save it in one pdf file.
# Manual turn of the pages and scanning it via a second
# run of the scanner is possible during the scan, since
# a graphical Pop-Up will show up in order to ask if the
# back sides should be scanned as well.
# ======================================================
# $1 = scanner device
# $2 = friendly name
#
device=$1
open_filemanager="true" # "true" | "false"
reverse_order="true" # "true" | "false"
# normally you grab the pile of paper, turn it around and
# put it in the ADF again, so that the order of the backsides
# is reversed, this "true" makes the script taking care of it.
outputdir="$HOME/brscan"
tmptemplate="brscan-XXXXXX"
# template for creating the temporary files
output_file="$outputdir/brscan_`date +%F_%H-%M-%S`.pdf" # output file with timestamp
touch $output_file # create output file as pdf
resolution=400
# 100|150|200|300|400|600|1200|2400|4800|9600
mode="Black"
# Black & White|Gray[Error Diffusion]
# |True Gray|24bit Color
# Create outputdir if it does not exist
mkdir -p $outputdir
# Create temp-directory within the outputdir
tmp_dir=`mktemp -d "$outputdir/$tmptemplate"`
# tiffcp does not support B&W jpegs
if [ "$mode" = "Black" \
-o "$mode" = "Gray[Error Diffusion]" ]
then
comp="zip"
else
comp="jpeg"
fi
# From Brother, don't know what it is for
if [ "`which usleep`" != '' ];then
usleep 10000
else
sleep 0.01
fi
#
# ================================================
# Here is the actual scan command
# Scan the odd sides with even index starting at 0
# ================================================
scanimage --device-name "$device" --resolution $resolution --format=tiff \
--mode "$mode" -y 297.00 --batch="$tmp_dir/tmp-image%02d" \
--batch-start=0 --batch-increment=2

#
# get maximal index of pictures scanned so far
for image in $tmp_dir/tmp-image*
do
echo "$image"
currentrun=`basename $image | sed -e "s/.*tmp-image\([0-9]\+\)/\1/"`
# strip zeros, for not to confuse the shell
currentrun=`echo $currentrun | sed -e"s/^0*//"`
if [ -z $currentrun ]
then
currentrun=0
else
currentrun=$(($currentrun))
fi
if [ -z $lo_run ] ; then lo_run=$currentrun ; fi
if [ -z $hi_run ] ; then hi_run=$currentrun ; fi
if [ $lo_run -gt $currentrun ] ;
then
lo_run=$currentrun
fi
if [ $hi_run -lt $currentrun ]
then
hi_run=$currentrun
fi
echo "Current run = $currentrun"
done
if zenity --question --text "Rueckseiten auch scannen?"; then
zenity --warning --text "Bitte Seiten aus Einzelblatteinzugablage entnehmen,\nim Bund mit den Rueckseiten nach oben wieder in den Einzelblatteinzug einlegen.\nDann OK klicken."
# ==============================================
# set the second scan to be in correct order
# for easy compilation of pdf
# ==============================================
if [ $reverse_order="true" ]; then
batchstart=`expr $hi_run + 1`
batchincrement=-2

else
batchstart=1
batchincrement=2
fi
echo "starting at $batchstart with increment $batchincrement"
# scanning the backsides with odd indices starting at high or 1 depending on order ;-)
# and saving them with odd indices starting at highestnumber+1 with increment
# -2 or just with odd indices starting at 1 if no reverse 

# order is set.
scanimage --device-name "$device" --resolution $resolution --format=tiff\
--mode "$mode" -y 297.00 \
--batch-start=$batchstart --batch-increment=$batchincrement --batch="$tmp_dir/tmp-image%02d"
fi
# ==============================================
# Now all sides are scanned and we continue with
# converting them to a nice pdf file
# ==============================================
image=`mktemp $tmp_dir/tifcollect-XXXXX`
# ==============================================
# tiffcp does not support B&W jpegs
# ==============================================
if [ "$mode" = "Black" \
-o "$mode" = "Gray[Error Diffusion]" ]
then
comp="zip"
else
comp="jpeg"
fi
# all tiffs are merged in one compressed tiff
tiffcp -s -r 32 -c $comp $tmp_dir/tmp-image* $image
# this makes the pdf file, the complicated procedure
# is a workaround for some trouble with tiff2pdf
tiff2pdf -x$resolution -y$resolution \
-o"$output_file" "$image"
# ==============================================
# Deleting temporary files
# ==============================================
rm $image
chmod 600 "$output_file"
rm $tmp_dir/*
rmdir $tmp_dir
# ==============================================
# Starting File Manager if requested
# ==============================================
if $open_filemanager = "true";then
# Use a file browser that is available in the current Linux installation.
# 1st choice: nautilus (gnome default file manager)
# 2nd choice: konqueror (KDE default file manager)
# 3rd choice: dolphin (KDE default simple file manager)
if [ "`which nautilus`" != '' ];then
nautilus $outputdir
else
if [ "`which konqueror`" != '' ];then
konqueror $outputdir
else
if [ "`which d3lphin`" != '' ];then
d3lphin $outputdir
fi
fi
fi
fi
exit 0

6 comments:

Juicer said...

Your earlier post on setting up the scanner to function from the scan key was very thorough for a novice like myself.
This one however isn't as clear. I see copy and paste but where do I paste it? Am I as root and where to paste it and save it as you described so clearly in the first post. The link that sends me to another site for the how to isn't clear either. Thanks for the guidance.

DaWuzzzz said...

Dear Juicer,

although it's a little late for the reply: where the scripts can be used, is described in the referenced article within the post. You have 4 scripts in the folder /usr/local/Brother/sane/script
You just have to decide which one you want to replace by the versions posted here. Hope that helps.

juicer52 said...

Thanks for the reply. Just so happens, I am in the process of now reinstalling my Brother MFC 7860dw for the new Linux Mint 17.
I was able to scan multiple pages using your second script but it does not enable combination of front and back of pages. After I press "OK" I am then presented with the same screen on the scanner as if starting a new scan. It creates a second pdf file of the back sides, separate from the pdf file of the front sides.
Any thoughts?

Johannes Wahl said...

Hello,
thank you for your script. But i it possible to scan in 24bit Color, if I change the mode from "Black" to "24bit Color" ill get a pfd file with blank pages.

Unknown said...

i would like to ask for the colour scan issue. what is the correct parameter for colour scanning? thank you.

DaWuzzzz said...

AFAIK, there is still a bug in libtiff which is being used in the script. I only got coloured scans as single jpg and not as pdf at all.