-
Notifications
You must be signed in to change notification settings - Fork 0
/
scan.sh
executable file
·134 lines (127 loc) · 4.11 KB
/
scan.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
#!/bin/bash
#DEVICE_IP="172.16.2.199"
DEVICE_IP="192.168.0.90"
if nmcli dev wifi | grep -q -e ' Aesthetics ' -e ' Tonnyho ' -e ' Henry ' -e ' BIDA ' -e ' Tonda ';
then
# B
DEVICE="airscan:e0:HP Color LaserJet MFP M283fdw (78EA67)"
else
# U
DEVICE="net:${DEVICE_IP}:pixma"
fi
RESOLUTION=300
MODE="Color"
OUTFILE="scan.pdf"
VERBOSE=""
COMPRESS="On"
SCRIPT="${0}"
function help() {
echo "$SCRIPT [-d DEVICE] [-ip IPADDRESS] [-r RESOLUTION] [-m MODE] [-v] [-h] [OUTFILE]"
echo " -d DEVICE ... scaner device. The default is ${DEVICE}"
echo " -ip IPADDRESS ... IP address of the scaner. The default is ${DEVICE_IP}"
echo " -r RESOLUTION ... Resolution of the scanner in DPI. The possible values are 150/300/600. The default is ${RESOLUTION} dpi"
echo " -m MODE ... Color mode. Possible values are Color/Gray/Lineart. the default is ${MODE}"
echo " -C ... Switch off compression of the output PDF file"
echo " -v ... be verbose"
echo " -h ... print help message and exits"
echo " OUTFILE ... The output file. The default is ${OUTFILE}"
}
# Parse cmdline
while [[ $# > 0 ]]; do
case ${1} in
"-d")
[[ $# > 1 ]] || { echo "ERROR: Missing DEVICE parameter"; exit 1; }
DEVICE="${2}"
shift
;;
"-ip")
[[ $# > 1 ]] || { echo "ERROR: Missing IPADDRESS parameter"; exit 1; }
DEVICE_IP="${2}"
DEVICE="net:${DEVICE_IP}:pixma"
shift
;;
"-r")
echo $#
[[ $# > 1 ]] || { echo "ERROR: Missing RESOLUTION parameter"; exit 1; }
case ${2} in
150)
RESOLUTION=150
;;
300)
RESOLUTION=300
;;
600)
RESOLUTION=600
;;
*)
echo "ERROR: Invalid resolution >>${2}<<"
help
exit 1
;;
esac
shift
;;
"-m")
[[ $# > 1 ]] || { echo "ERROR: Missing MODE parameter"; exit 1; }
case ${2} in
Color)
MODE=Color
;;
Gray)
MODE=Gray
;;
Lineart)
MODE=Lineart
;;
*)
echo "ERROR: Invalid mode >>${2}<<"
help
exit 1
;;
esac
shift
;;
"-C")
COMPRESS=""
;;
-v|--verbose)
VERBOSE="-v"
;;
-h|--help)
help
exit 0
;;
-*)
echo "ERROR: Invalid parameter >>${1}<<"
help
exit 1
;;
*)
OUTFILE="${1}"
;;
esac
shift
done
# Check dependencies
type -t gm &> /dev/null || { echo "ERROR: GraphicsMagick is not installed."; exit 1; }
type -t scanimage &> /dev/null || { echo "ERROR: sane-backends is not installed."; exit 1; }
[[ -n "${COMPRESS}" ]] && { type -t gs &> /dev/null || { echo "ERROR: GostScript (gs) is not installed."; exit 1; } ; }
if [[ -n "${VERBOSE}" ]]; then
echo DEVICE_IP: ${DEVICE_IP}
echo DEVICE: ${DEVICE}
echo RESOLUTION: ${RESOLUTION}
echo MODE: ${MODE}
echo OUTFILE: ${OUTFILE}
echo COMPRESSION: ${COMPRESS:-No}
fi
# Run the scanning
scanimage ${VERBOSE} -d "${DEVICE}" --resolution "${RESOLUTION}" --mode "${MODE}" | gm convert - "${OUTFILE}"
if [[ -n "${COMPRESS}" ]]; then
[[ -n "${VERBOSE}" ]] && { echo "Compressing the scanned image"; }
TMPPDF="$(mktemp /tmp/scan.XXXXXX)".pdf
gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/default -dNOPAUSE -dQUIET -dBATCH \
-dDetectDuplicateImages -dCompressFonts=true -r"${RESOLUTION}" -sPAPERSIZE=a4 -dPDFFitPage \
-dFIXEDMEDIA -sOutputFile="${TMPPDF}" "${OUTFILE}"
# [[ -n "${VERBOSE}" ]] && echo "Renaming" "${OUTFILE}" "${TMPPDF}"
mv "${TMPPDF}" "${OUTFILE}"
fi