-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscantofile-0.2.4-1.sh
executable file
·153 lines (138 loc) · 4.58 KB
/
scantofile-0.2.4-1.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#! /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
#
#
# Settings
#
device=$1 #see with scanimage -L
open_filemanager="true" # open file manager to display created pdf.
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/brother-scans"
tmptemplate="brscan-XXXXXX"
output_file="$outputdir/brscan_`date +%F_%H-%M-%S`.pdf"
#scan options
resolution=1200 #see scanimage --help (1200 is max on MFC-7360N)
mode="24bit Color" #see scanimage --help
#
# Start Script
#
mkdir -p $outputdir
touch $output_file
tmp_dir=`mktemp -d "$outputdir/$tmptemplate"`
# tiffcp does not support B&W jpegs
if [ "$mode" = "Black" ]; then
comp="zip"
else
comp="jpeg"
fi
# From Brother, sleeping 1 sec here
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" --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" --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
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