📜 ⬆️ ⬇️

How to make an office scanner network

Lyrical digression


In one small organization (~ 10 computers) it was necessary to organize a backup of information. To do this, it was decided to install a computer with a large hard drive and Ubuntu inside, and on users' computers - Cobian backup.

An inquisitive reader will ask: “Why do you write this, the author? And what have some kind of scanner? ”
The fact is that in this organization there was only one opportunity to scan the document - ask the Samsung SCX-4200, the happy owner of the MFP, to do it (the name, of course, not important, but it all happened because of this device).
During the installation of Ubuntu on the “server for backup”, the idea arose: “And if you connect this MFP here, leave the keyboard connected, and by pressing certain keys force the scanner to scan, saving the result in a shared folder? After all, then a person will not be distracted from work to scan other people's documents! ”
Of course, first of all, ready-made scripts were searched. This one seemed the most interesting - www.opennet.ru/base/sys/net_scanner.txt.html
However, upon closer inspection, he was not so good, because I have no desire to recompile the program to change the scanner settings, to teach users to convert images and create .pdf-files.
I'll try to write my own, good ...


So, the task is set


1. Connect to the Ubuntu MFP, make the printer shared, configure the scanner.
2. Write a script that will wait for pressing a key on the keyboard
Esc - cancel scanning in progress
1 - scan color mode
2 - scan mode in grayscale
0 - save the scanned file (jpg, if one page was scanned or pdf, if several)
Enter - scan the page.
')

Decision


Point 1 was dropped by itself. Connecting the printer happened unexpectedly simply and did not cause a single question. It was enough to simply connect the USB cable to the computer, and after a few seconds, Ubuntu announced that the printer should print. Sent a test page - really typing!
Now the fun part.
Let's see which scanners we have in the system:
scanimage -L
If the system cannot find this command, it means that you need to install the sane-utils package:
sudo apt-get install sane-utils

The program found a device with the name `xerox_mfp: libusb: 001: 002`
If the scanner is the only one in the system, then the “device name” parameter can be omitted; scanimage will scan the only possible scanner.

Let's try to scan the page:
scanimage -d “xerox_mfp: libusb: 001: 002” --resolution 150 --mode Color --format = tiff> test.tif
The resolution of 150 dpi is chosen because of the desire to reduce the scan time and the size of the output file, but leave the user the ability to print a document with readable text. If you ever need text recognition or scanning photos, then additional options will appear.
So, we have tiff. Either one or several (scanned a multi-page document). It is clear that users in 90% of cases scan documents for sending by e-mail, and tiff is not a compact format. So, you need to convert the result to .jpg or .pdf.

Put the package for editing / converting raster images:
sudo apt-get install imagemagick

Squeeze a single image:
convert -quality 60% test.tif test.jpg

Or all at once:
convert -compress jpeg -quality 60% * .tif all.pdf

Only one moment remained. I am not going to leave a monitor connected to this computer - it means that the user is left without feedback. Well, let's make a computer talking.
Make a list of sounds that should be played in response to pressing a key on the keyboard, take the microphone and record our mini-phrases. They are as short as possible, because the goal of the whole action is not listening to mp3, but scanning.
1. Waiting for the command (waitcommand.mp3)
2. Color scanning (color.mp3)
3. Black and white scanning (bw.mp3)
4. Scan page (scanpage.mp3)
5. Save the result (saveresult.mp3)

Oh yeah, because we have a “clean” system that cannot play mp3 from the console ...
We correct the situation:
sudo apt-get install mpg321
Enjoying:
mpg123 -q waitcommand.mp3

Now it seems everything is ready


Writing a script /mnt/2tb/scan.sh

#!/bin/bash

# ==================================================
#

scannerdevice="xerox_mfp:libusb:001:002" #scanimage -L
workdir="/tmp/scanworkdir"
destdir="/mnt/2tb/Share/1Scanner"
dpi="150"
jpegquality="60%"

# ==================================================
#

sleep 10s

mkdir -p $workdir
rm $workdir/*.* 2>/dev/null
numpages=0
color="Gray"
mpg123 -q waitcommand.mp3

while true
do
#
read -sn 1 Keypress

case "$Keypress" in
$'\e')
#
rm $workdir/*.* 2>/dev/null
numpages=0
color="Gray"
mpg123 -q waitcommand.mp3
;;

$'1')
#
color="Color"
mpg123 -q color.mp3
;;

$'2')
#/
color="Gray"
mpg123 -q bw.mp3
;;

$'0')
#
mpg123 -q saveresult.mp3
filename=`date +%Y%m%d-%H%M%S`
if [ $numpages = 1 ]; then
convert -quality $jpegquality $workdir/1.tif $destdir/$filename.jpg
fi
if [ $numpages \> 1 ]; then
convert -compress jpeg -quality $jpegquality $workdir/*.tif $destdir/$filename.pdf
fi
rm $workdir/*.* 2>/dev/null
numpages=0
color="Gray"
mpg123 -q waitcommand.mp3
;;

$'')
#
mpg123 -q scanpage.mp3
let "numpages=numpages+1"
scanimage -d $scannerdevice --resolution $dpi --mode $color --format=tiff >$workdir/$numpages.tif
mpg123 -q waitcommand.mp3
;;
esac

done


We speak
chmod + x /mnt/2tb/scan.sh

Run


Yes, everything works as expected. Now go to the menu Ubuntu System -> Options -> Running applications, Add, Browse, select the file with the script. Restarting the computer, and ... the scanner starts to scan something continuously, no sounds are played.
Okay, I press Ctrl + C, I read once again what I wrote ...
With sounds, everything is trivial - they are not in the folder from which the script is called. Continuous scanning is apparently due to $ '') inside the case.

I did not begin to deal with this in detail, but simply changed the autorun command to
/ usr / bin / gnome-terminal -e /mnt/2tb/scan.sh --working-directory / mnt / 2tb
Once again I restart the computer - everything works, users are happy.

Source: https://habr.com/ru/post/110905/


All Articles