You need to enable JavaScript to run this app. Delphi XE7下DelphiTwain组件600dpi扫描HP MFP M125nw断连出残缺图求解
优惠活动
大模型
产品
解决方案
定价
更多

Delphi XE7下DelphiTwain组件600dpi扫描HP MFP M125nw断连出残缺图求解

高分辨率扫描偶发图像不完整问题说明

我遇到了文档扫描相关的异常问题。运行环境为Windows 10 x64系统、Delphi XE7开发环境,使用DelphiTwain组件,搭配的扫描仪型号为HP MFP M125nw。

使用最低分辨率扫描时功能运行完全正常,但切换为600dpi分辨率扫描时,每10次尝试约有4次出现如下故障:组件与扫描仪驱动断开连接(扫描仪仍继续执行扫描流程,但最终获取的图像不完整——图像仅顶部区域显示扫描到的对象内容,后续区域为全黑空白,有时仅能覆盖扫描区域的10%、30%或70%不等)。

以下是最初编写的扫描相关代码:

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

我也尝试过使用原生传输模式,但故障现象完全一致,问题无规律偶发。

请问该如何调整配置或代码,才能实现每次扫描都稳定达到预期效果?


补充说明

应相关要求,在此附上项目的全部相关代码:
项目共包含5个文件:

  • 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, 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

内容的提问来源于stack exchange,提问作者MSSI

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.04 05:15:06