Skip to content

Commit

Permalink
Merge pull request #17 from kn100/add-adf-simplex
Browse files Browse the repository at this point in the history
actually add support for ADF Simplex scanners and potentially ADF Duplex ones too.
  • Loading branch information
kn100 authored Sep 5, 2023
2 parents 10b038d + 5dcfc8e commit d5b0677
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 22 deletions.
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ thing and I can search for the document later.

1. Create a Telegram Bot using the [BotFather](https://telegram.me/BotFather).
2. Make sure your Telegram account has a username set.
2. Use the example Docker Compose file below to get started.
3. Start a chat with your new Telegram bot.
3. Use the example Docker Compose file below to get started.
4. Start a chat with your new Telegram bot.

## Docker Compose

Expand Down Expand Up @@ -60,6 +60,8 @@ services:
- "/final:/final"
```
Telescan will use the Automatic Document Feed capability of your scanner, if it supports it. It is not currently possible to override this behaviour. If your scanner does not feature this capability (ie, it is flatbed only), it'll use that.
## Running without Docker
```bash
TELEGRAM_API_KEY="<some-key>" \
Expand All @@ -79,7 +81,12 @@ lists below.
* HP Deskjet 2545 (tested with v0.1.8)
* HP Envy 5010 (tested with v0.1.8)
* Epson WF-7830 (tested with v0.1.8)
* Epson WF-3720 (tested with v0.1.9)
* Brother MFC-L2710DN (tested with v0.1.8)

## Not working

## TODO
* The ADF simplex setup does not conveniently support scanning backsides. It is possible to insert the documents the other way and continue scanning, however this will result in the pages not being in the correct order. Add support for this interlacing if the user chooses to scan backsides.
* It would probably be nice to ask the user if they want to scan in A4 or Letter - rather than just forcing the superior standard (A4). I live in Canada so this default makes me suffer too.
* The separation between the scanner and the scan session complicates situations where the feature set of the scanner affects how the user scans. I am thinking specifically of the ADF Simplex setup issue described above. In future, after the scanner has concluded scanning all the front sides, Telescan should ask the user if they wish to scan backsides - but should only do this in the ADF Simplex case. This is going to be ugly unless we more tightly couple the scanning hardware to the scan session.
11 changes: 7 additions & 4 deletions scanner/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func Init(srv *dnssd.BrowseEntry, logger *zap.SugaredLogger) *Scanner {
}
}

func (s *Scanner) Scan() ([]byte, error) {
func (s *Scanner) Scan() ([]bytes.Buffer, error) {
if s.State != ScannerStateIdle {
return nil, fmt.Errorf(s.stateMsg())
}
Expand Down Expand Up @@ -76,17 +76,20 @@ func (s *Scanner) Scan() ([]byte, error) {

defer scan.Close()

var f bytes.Buffer
f := []bytes.Buffer{}
pageCount := 0
for scan.ScanPage() {
if _, err := io.Copy(&f, scan.CurrentPage()); err != nil {
f = append(f, bytes.Buffer{})
if _, err := io.Copy(&f[pageCount], scan.CurrentPage()); err != nil {
s.UpdateState(ScannerStateIdle)
return nil, err
}
pageCount++
}

s.UpdateState(ScannerStateIdle)

return f.Bytes(), nil
return f, nil
}

func (s *Scanner) GetState() ScannerState {
Expand Down
11 changes: 7 additions & 4 deletions scansession/scansession.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package scansession

import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"time"
Expand Down Expand Up @@ -33,17 +33,20 @@ func NewScanSession(userName string, chatID int64, tmpDir, finalDir string) *Sca
}
}

func (s *ScanSession) AddImage(imgBytes []byte) {
func (s *ScanSession) AddImages(imgBytes []bytes.Buffer) {
s.ScanLastUpdated = time.Now()
s.filesInScan = append(s.filesInScan, imgBytes)
for _, img := range imgBytes {
s.filesInScan = append(s.filesInScan, img.Bytes())
}
}

func (s *ScanSession) WriteFinal() (string, error) {
// TODO: Interlace the images if the scanner is ADF simplex
filesOnDisk := make([]string, len(s.filesInScan))
for i := 0; i < len(s.filesInScan); i++ {
fileName := fmt.Sprintf("%s-%d.jpg", s.Filename(), i)
filePath := filepath.Join(s.tmpDir, fileName)
err := ioutil.WriteFile(filePath, s.filesInScan[i], 0644)
err := os.WriteFile(filePath, s.filesInScan[i], 0644)
if err != nil {
return "", err
}
Expand Down
32 changes: 20 additions & 12 deletions tg/tg.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func (t *TG) handleScanSession(ss *scansession.ScanSession, u tgbotapi.Update) {
default:
if ss.NumImages() == 0 {
t.sendMsgWithKB(u.Message.Chat.ID,
"Welcome. Insert the first page and press Scan.",
"Welcome. Insert the first page and press Scan. If your scanner has an Automatic Document Feeder (ADF), you can scan multiple pages at once by placing them in the feeder now.",
ss)
} else {
t.sendMsgWithKB(u.Message.Chat.ID,
Expand Down Expand Up @@ -162,27 +162,35 @@ func (t *TG) sendScanToChat(u tgbotapi.Update, fn, fp string) {
t.bot.Send(doc)
}

func (t *TG) handleScanRequest(ss *scansession.ScanSession, u tgbotapi.Update) {
t.sendMsg(u.Message.Chat.ID, "⌛ Scanning page, please wait...")

func (t *TG) scan(ss *scansession.ScanSession, u tgbotapi.Update) int {
scanner, err := t.scm.GetScanner()
if err != nil {
t.logAndReportErrorToUser(u, "Telescan couldn't grab a scanner to scan with", err)
return
return 0
}

bytes, err := scanner.Scan()
b, err := scanner.Scan()
if err != nil {
t.logAndReportErrorToUser(u, "Telescan couldn't use this scanner", err)
return 0
}

ss.AddImages(b)

return len(b)
}

func (t *TG) handleScanRequest(ss *scansession.ScanSession, u tgbotapi.Update) {
t.sendMsg(u.Message.Chat.ID, "⌛ Scanning page(s), please wait...")

numPages := t.scan(ss, u)

if numPages == 0 {
t.sendMsg(u.Message.Chat.ID, "❌ No pages scanned. Please try again.")
return
}

ss.AddImage(bytes)
t.sendMsgWithKB(
u.Message.Chat.ID,
fmt.Sprintf("✅ Scanned page %d.", ss.NumImages()),
ss,
)
t.sendMsgWithKB(u.Message.Chat.ID, fmt.Sprintf("✅ Scanned %d pages.", numPages), ss)
}

func (t *TG) sendMsgWithKB(chatID int64, text string, ss *scansession.ScanSession) error {
Expand Down

0 comments on commit d5b0677

Please sign in to comment.