I have a problem with document scanning. I use DelphiTwain component over Delphi XE7, everything on Windows 10 x64. Scanner that I use is HP MFP M125nw.
When I scan with the smallest resolution, it works well, but when I scan with 600dpi, 4 of 10 tries failes this way, that component loses connection with driver (scanner continues scanning, but I'm getting incomplete picture - picture shows scanned object from the top, but later it's just black and contains nothing... sometime it scans 30% of the scanning area, sometime 70% sometime 10%).
Here is my code...
I was trying also native transfer mode, but the problem is the same - sometime it works, sometime it does not.
Does anyone know what could I do to get it working as expected each time I scan?
Twain := TDelphiTwain.Create;
if Twain.LoadLibrary then
begin
Twain.SourceManagerLoaded := TRUE;
Twain.Source[src].Loaded := True;
if Twain.Source[src].Loaded = True then
begin
Twain.Source[src].SetIXResolution(600);
Twain.Source[src].SetIYResolution(600);
Twain.Source[src].SetICapUnits(tuInches);
Twain.Source[src].SetImagelayoutFrame(0, 0, (TicketWidth / 10) / 2.54, (TicketHeight / 10) / 2.54);
Twain.Source[src].SetIPixelType(tbdRgb);
Twain.Source[src].TransferMode := TTwainTransferMode(0);
Twain.Source[src].SetupFileTransfer(tf + '~ticket.bmp', TTwainFormat.tfBMP);
Twain.Source[src].EnableSource(False,False);
While Twain.Source[src].Enabled do
begin
Application.ProcessMessages;
end;
Twain.UnloadLibrary;
Twain.Free;
tmr_OCR.Enabled := True;
end else
ShowMessage('Can''t contact scanner');
end else
ShowMessage('Twain is not installed.');
=============================== ===========Edit================
I've got request to put all code here
There are five files:
- Project1.dpr
- uFrmMain.pas
- uFrmMain.dfm
- uFrmScan.pas
- uFrmScan.dfm
so...
Project1.dpr
program Project1;
uses
Vcl.Forms,
uFrmScan in 'uFrmScan.pas' {FrmScan},
uFrmMain in 'uFrmMain.pas' {FrmMain};
{$R *.res}
begin
Application.Initialize;
Application.MainFormOnTaskbar := True;
Application.CreateForm(TFrmMain, FrmMain);
Application.CreateForm(TFrmScan, FrmScan);
Application.Run;
end.
uFrmMain.pas
unit uFrmMain;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, DelphiTwain, DelphiTwain_Vcl, Vcl.ExtCtrls;
type
TFrmMain = class(TForm)
btn_1: TButton;
procedure btn_1Click(Sender: TObject);
private
{ Private declarations }
public
end;
var
FrmMain: TFrmMain;
implementation
{$R *.dfm}
uses uFrmScan;
procedure TFrmMain.btn_1Click(Sender: TObject);
begin
FrmScan := TFrmScan.Create(Self);
FrmScan.ShowModal;
end;
end.
uFrmMain.dfm
object FrmMain: TFrmMain
Left = 0
Top = 0
Caption = 'FrmMain'
ClientHeight = 299
ClientWidth = 424
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object btn_1: TButton
Left = 136
Top = 120
Width = 147
Height = 25
Caption = 'Open scan window'
TabOrder = 0
OnClick = btn_1Click
end
end
uFrmScan.pas
unit uFrmScan;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, DelphiTwain, DelphiTwain_Vcl, Vcl.ExtCtrls;
type
TFrmScan = class(TForm)
btn_1: TButton;
pnl_1: TPanel;
lbl_1: TLabel;
lst_Sources: TListBox;
img_1: TImage;
btn_2: TButton;
procedure FormCreate(Sender: TObject);
procedure btn_1Click(Sender: TObject);
procedure btn_2Click(Sender: TObject);
private
{ Private declarations }
public
procedure Scan(dpi : Integer);
end;
var
FrmScan: TFrmScan;
const TicketWidth : Integer = 8;
TicketHeight : Integer = 12;
implementation
{$R *.dfm}
procedure TFrmScan.btn_1Click(Sender: TObject);
begin
Scan(96)
end;
procedure TFrmScan.btn_2Click(Sender: TObject);
begin
Scan(600)
end;
procedure TFrmScan.FormCreate(Sender: TObject);
var Twain : TDelphiTwain;
i : Integer;
begin
Twain := TDelphiTwain.Create;
if Twain.LoadLibrary then
begin
//Load source manager
Twain.SourceManagerLoaded := TRUE;
lst_Sources.Items.Clear;
for I := 0 to Twain.SourceCount-1 do
lst_Sources.Items.Add(Twain.Source[I].ProductName);
if lst_Sources.Items.Count > 0 then
lst_Sources.ItemIndex := 0;
end else begin
ShowMessage('Twain is not installed.');
end;
Twain.UnloadLibrary;
Twain.Free;
end;
procedure TFrmScan.Scan(dpi: Integer);
var Twain : TDelphiTwain;
src : Integer;
begin
if FileExists('~ticket.bmp') then
DeleteFile('~ticket.bmp');
Twain := TDelphiTwain.Create;
src := lst_Sources.ItemIndex;
if Twain.LoadLibrary then
begin
Twain.SourceManagerLoaded := TRUE;
Twain.Source[src].Loaded := True;
if Twain.Source[src].Loaded = True then
begin
Twain.Source[src].SetIXResolution(dpi);
Twain.Source[src].SetIYResolution(dpi);
Twain.Source[src].SetICapUnits(tuInches);
Twain.Source[src].SetImagelayoutFrame(0, 0, 3.15, 4.72);
Twain.Source[src].SetIPixelType(tbdRgb);
Twain.Source[src].TransferMode := TTwainTransferMode(0);
Twain.Source[src].SetupFileTransfer('~ticket.bmp', TTwainFormat.tfBMP);
Twain.Source[src].EnableSource(False,False);
While Twain.Source[src].Enabled do
begin
Application.ProcessMessages;
end;
Twain.UnloadLibrary;
Twain.Free;
img_1.Picture.LoadFromFile('~ticket.bmp');
end else
ShowMessage('Can''t contact scanner');
end else
ShowMessage('Twain is not installed.');
end;
end.
uFrmScan.dfm
object FrmScan: TFrmScan
Left = 0
Top = 0
Caption = 'FrmScan'
ClientHeight = 299
ClientWidth = 424
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
OnCreate = FormCreate
PixelsPerInch = 96
TextHeight = 13
object img_1: TImage
Left = 210
Top = 3
Width = 208
Height = 262
Proportional = True
Stretch = True
end
object btn_1: TButton
Left = 210
Top = 271
Width = 95
Height = 25
Caption = 'Scan with 96dpi'
TabOrder = 1
OnClick = btn_1Click
end
object pnl_1: TPanel
AlignWithMargins = True
Left = 3
Top = 3
Width = 201
Height = 293
Align = alLeft
BevelOuter = bvLowered
Caption = 'pnl_1'
TabOrder = 0
object lbl_1: TLabel
Left = 1
Top = 1
Width = 199
Height = 13
Align = alTop
Caption = 'Source'
ExplicitWidth = 33
end
object lst_Sources: TListBox
Left = 1
Top = 14
Width = 199
Height = 278
Align = alClient
ItemHeight = 13
TabOrder = 0
end
end
object btn_2: TButton
Left = 311
Top = 271
Width = 105
Height = 25
Caption = 'Scan with 600 dpi'
TabOrder = 2
OnClick = btn_2Click
end
end