首页 > 解决方案 > 使用 600dpi 扫描时出现 DelphiTwain 问题

问题描述

我在文档扫描时遇到问题。我在 Delphi XE7 上使用 DelphiTwain 组件,一切都在 Windows 10 x64 上。我使用的扫描仪是 HP MFP M125nw。

当我以最小分辨率扫描时,效果很好,但是当我以 600dpi 扫描时,10 次尝试中有 4 次以这种方式失败,该组件失去与驱动程序的连接(扫描仪继续扫描,但我得到的图片不完整 - 图片显示扫描的对象从顶部开始,但后来它只是黑色并且什么都没有......有时它会扫描 30% 的扫描区域,有时是 70%,有时是 10%)。

这是我的代码...

我也在尝试本机传输模式,但问题是一样的——有时有效,有时无效。

有谁知道每次扫描时我能做些什么才能让它按预期工作?

  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.');

===========================================编辑======= =========

我有要求将所有代码放在这里

有五个文件:

所以...

项目1.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

这是组件网站的链接

标签: imagedelphitwain

解决方案


推荐阅读