diff --git a/scanner/card_scanner b/scanner/card_scanner new file mode 100755 index 0000000..111a95e --- /dev/null +++ b/scanner/card_scanner @@ -0,0 +1,59 @@ +#!/bin/bash + +check_error() { + if test $? -ne 0; then + echo "card_scanner: $1 exited with unexpected error" + exit 1 + fi +} + +STORAGE_DIR="$HOME/.local/share/sevenkeys/scanimages/" +mkdir -p "$STORAGE_DIR" + +printf "Database profile: " +read profile + +printf "Storage area name: " +read storageAreaName + +ADD_CARDS=0 +echo "scantap: Beginning scan loop" +while true; do + rng="$(cat /dev/random | tr -cd 'a-f0-9' | head -c 32)" + filename="$STORAGE_DIR/$rng.png" + + scanimage --output-file="$filename" --source "ADF Front" --mode Color --page-width 63mm --page-height 88mm 2>/dev/null + + # scanimage exits with code 7 if no documents are available in scanner + if test $? -eq 7; then + if test $ADD_CARDS -eq 0; then + ADD_CARDS=1 + echo "scantap: No more cards in feeder" >&2 + fi + + # If we have generated a zero-length file, then delete it + if ! test -s "$filename"; then + rm "$filename" + fi + + continue + fi + ADD_CARDS=0 + check_error "scanimage" + + convert -rotate 180 "$filename" "$filename" + check_error "convert" + + cardLocationId="$(./sevenkeys --profile="$profile" store --storagearea="$storageAreaName" --id="00000000-0000-0000-0000-0000000000000")" + check_error "sevenkeys" + + if test "$profile" == "development"; then + databaseName="sevenkeys_development" + else + databaseName="sevenkeys" + fi + + mysql --silent --silent --user=root --password="$(pass show sevenkeys/mysql)" \ + -e "USE $databaseName; INSERT INTO CardScan (CardLocationId, Filename) VALUES ('$cardLocationId', '$rng.png');" + check_error "mysql" +done diff --git a/scanner/crop b/scanner/crop new file mode 100755 index 0000000..8e48c06 --- /dev/null +++ b/scanner/crop @@ -0,0 +1,8 @@ +#!/bin/bash + +crop() { + convert "$1" -crop 1200x120+90+100 "$2" +} + +crop old_border.png name_old.png +crop new_border.png name_new.png diff --git a/scanner/profiler b/scanner/profiler new file mode 100755 index 0000000..8baed32 --- /dev/null +++ b/scanner/profiler @@ -0,0 +1,49 @@ +#!/bin/bash + +CLASSNAME="svnProfiler" +TITLE_FILENAME="/tmp/title.png" +touch "$TITLE_FILENAME" + +get_window_id() { + while true; do + id="$(xdotool search --classname "$CLASSNAME")" + if test "$?" -ne 0; then + continue + fi + + echo "$id" + break + done +} + +printf "Database profile: " +read profile +if test "$profile" == "development"; then + databaseName="sevenkeys_development" +else + databaseName="sevenkeys" +fi + +STORAGE_DIR="$HOME/.local/share/sevenkeys/scanimages/" +cd "$STORAGE_DIR" + +files="$(find *.png)" + +nsxiv -N "$CLASSNAME" $files & +nsxivWindowId="$(get_window_id)" +nsxiv "$TITLE_FILENAME" & + +for file in $files; do + cardLocationId="$(mysql --silent --silent --user=root --password="$(pass show sevenkeys/mysql)" -e "USE $databaseName; SELECT CardLocationId FROM CardScan WHERE Filename = '$file';")" + + # TODO: Detect features of the card automatically + #convert "$file" -crop 1200x120+90+100 "$TITLE_FILENAME" + #title="$(tesseract --psm 9 "$TITLE_FILENAME" stdout)" + #echo "$title" + + cardPrintingId="$(sevenkeys --profile="$profile" search-printings)" + + sevenkeys --profile="$profile" replace --card-location-id="$cardLocationId" --card-printing-id="$cardPrintingId" + + xdotool key --window "$nsxivWindowId" 'n' +done diff --git a/scanner/psm_test b/scanner/psm_test new file mode 100755 index 0000000..22474f0 --- /dev/null +++ b/scanner/psm_test @@ -0,0 +1,9 @@ +#!/bin/bash +for i in $(seq 1 13); do + echo "PSM $i:" >>output + echo "Old card:" >>output + tesseract name_old.png stdout --psm "$i" >>output + echo "New card:" >>output + tesseract name_new.png stdout --psm "$i" >>output + echo >>output +done diff --git a/scanner/test_card_scanner_count b/scanner/test_card_scanner_count new file mode 100755 index 0000000..31e3f38 --- /dev/null +++ b/scanner/test_card_scanner_count @@ -0,0 +1,34 @@ +#!/bin/bash + +if test -z "$1"; then + echo "usage: test_card_scanner_count " >&2 + exit 1 +fi + +STORAGE_DIR="$HOME/.local/share/sevenkeys/testimages/" +mkdir -p "$STORAGE_DIR" + +starting_file_count="$(find $STORAGE_DIR/*.png | wc -l)" + +echo "test_card_scanner_count: Beginning test" +while true; do + rng="$(cat /dev/random | tr -cd 'a-f0-9' | head -c 32)" + filename="$STORAGE_DIR/$rng.png" + + scanimage --output-file="$filename" --source "ADF Front" --mode Color --page-width 63mm --page-height 88mm 2>/dev/null + + # scanimage exits with code 7 if no documents are available in scanner + if test $? -eq 7; then + echo "test_card_scanner_count: No more cards in feeder" >&2 + rm "$filename" + break + fi +done + +ending_file_count="$(find $STORAGE_DIR/*.png | wc -l)" + +if test $(( ending_file_count - starting_file_count )) -ne "$1"; then + echo "FAILED: Start: $starting_file_count, End: $ending_file_count" +else + echo "SUCCESS" +fi diff --git a/scanner/test_scanner_loop b/scanner/test_scanner_loop new file mode 100755 index 0000000..21222ce --- /dev/null +++ b/scanner/test_scanner_loop @@ -0,0 +1,12 @@ +#!/bin/bash + +STORAGE_DIR="$HOME/.local/share/sevenkeys/testimages/" +mkdir -p "$STORAGE_DIR" + +echo "test_card_scanner_count: Beginning test" +while true; do + rng="$(cat /dev/random | tr -cd 'a-f0-9' | head -c 32)" + filename="$STORAGE_DIR/$rng.png" + + scanimage --output-file="$filename" --source "ADF Front" --mode Color --page-width 63mm --page-height 88mm 2>/dev/null +done