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

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

我收到了把所有代码在这里的请求

有五个文件

  • Project1.dpr
  • uFrmMain.pas
  • uFrmMain.dfm
  • uFrmScan.pas
  • uFrmScan.dfm

所以...

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,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,3.15,4.72);
      Twain.source[src].SetIPixelType(tbdRgb);
      Twain.source[src].TransferMode := TTwainTransferMode(0);
      Twain.source[src].SetupFileTransfer('~ticket.bmp',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

Here is a link to components website

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)