如何使用Delphi扫描300dpi图像并以TIFF格式保存?

如何使用Delphi扫描300dpi图像并以TIFF格式保存?,delphi,gdi+,twain,Delphi,Gdi+,Twain,我正在使用Delphitwain(Delphitwain.sourceforge.net)向我的应用程序添加扫描功能。扫描工作正常,我可以保存bmp和jpeg文件。现在我需要: 以300dpi保存(扫描仪可以) 以TIFF格式保存 在四处挖掘之后,我发现了两个提示: 这是我的最终代码: procedure TForm1.GoAcquireClick(Sender: TObject); begin Counter := 0; Twain.SourceManagerLoaded :=

我正在使用Delphitwain(Delphitwain.sourceforge.net)向我的应用程序添加扫描功能。扫描工作正常,我可以保存bmp和jpeg文件。现在我需要:

  • 以300dpi保存(扫描仪可以)
  • 以TIFF格式保存
在四处挖掘之后,我发现了两个提示:

这是我的最终代码:

procedure TForm1.GoAcquireClick(Sender: TObject);
begin
  Counter := 0;
  Twain.SourceManagerLoaded := TRUE;
  Twain.LoadSourceManager;
  Twain.TransferMode := ttmMemory;

  with Twain.Source[ 0 ] do
  begin
    Loaded := TRUE;
    SetIXResolution(300);
    SetIYResolution(300);
    SetIBitDepth(1);
    EnableSource(true, true);
    while Enabled do Application.ProcessMessages;
  end;
end;

procedure TForm1.TwainTwainAcquire(Sender: TObject; const Index: Integer;
   Image: TBitmap; var Cancel: Boolean);
var
   TiffHolder: TSynPicture;
begin
  Inc( Counter );
  Current := Counter;
  ImageHolder.Picture.Assign( Image );
  ImageHolder.Picture.Bitmap.Monochrome := true;
  ImageHolder.Picture.Bitmap.Pixelformat := pf1Bit;

  SynGDIPlus.SaveAs(ImageHolder.Picture, format('c:\temp\teste%d.tif',[ Counter ]), gptTIF );
end;
结果:图像仍为96dpi,并保存为BMP(即使使用TIF扩展)


我遗漏了什么?

在Delphi中保存TIFF文件很难。我不知道有任何免费/开源模块可以做到这一点

工作

过去,我使用命令行通过createprocess将保存为bmp并使用irfanview转换为tiff
“i_view32.exe c:\temp\scanned.bmp/bpp=1/convert=c:\temp\scanned.tif”

GDI+库能够保存tiff图片

SynGdiPlus单元使用{557CF405-1A04-11D3-9A73-0000F81EF32E}作为其TIFF编码器

TSynPicture.SaveAs
代码中,我看到了两种可能性:

  • 或者您没有安装相应的TIFF编码器
  • TIFF编码器可能缺少一些预期参数
请尝试以下版本:

type
  /// the optional TIFF compression levels
  // - use e.g. ord(evCompressionCCITT4) to save a TIFF picture as CCITT4
  TGDIPPEncoderValue = (
    evColorTypeCMYK,
    evColorTypeYCCK,
    evCompressionLZW,
    evCompressionCCITT3,
    evCompressionCCITT4,
    evCompressionRle,
    evCompressionNone,
    (...)
    evFrameDimensionPage);

const
  EncoderCompression: TGUID = '{e09d739d-ccd4-44ee-8eba-3fbf8be4fc58}';

function TSynPicture.SaveAs(Stream: TStream; Format: TGDIPPictureType;
  CompressionQuality: integer): TGdipStatus;
var fStream: IStream;
    Len,Dummy: Int64;
    tmp: pointer;
    Params: TEncoderParameters;
    PParams: pointer;
    MS: TMemoryStream absolute Stream;
begin
  if not Gdip.Exists or (Stream=nil) or (fImage=0) then begin
    result := stInvalidParameter;
    exit;
  end;
  Params.Count := 1;
  Params.Parameter[0].Type_ := EncoderParameterValueTypeLong;
  Params.Parameter[0].NumberOfValues := 1;
  Params.Parameter[0].Value := @CompressionQuality;
  PParams := nil;
  case Format of
  gptJPG: if CompressionQuality>=0 then begin
    Params.Parameter[0].Guid := EncoderQuality;
    PParams := @Params;
  end;
  gptTIF: begin
    if not (TGDIPPEncoderValue(CompressionQuality) in [
        evCompressionLZW, evCompressionCCITT3, evCompressionCCITT4,
        evCompressionRle, evCompressionNone]) then
      // default tiff compression is LZW
      CompressionQuality := ord(evCompressionLZW);
    Params.Parameter[0].Guid := EncoderCompression;
    PParams := @Params;
  end;
  end;
  CreateStreamOnHGlobal(0, true, fStream);
  (...)
它将为TIFF图片添加
EncoderCompression
参数,该参数将


我已经更新了源代码存储库版本以包含此更正。

好问题。请发布您的Delphi版本。我建议您将主题更改为如何将DPI从96更改为300。相关:(不是重复的问题,因为这个新问题归结为如何使用这些相同的Twain组件更改扫描的DPI和文件格式,可能是属性设置问题)感谢您的提示Warren。我会保留“扫描”这个词而不是“更改分辨率”,因为我不知道采集的图像是否已经是300dpi,或者是在我对图像参数进行操作后更改的。Delphi 7。但是,如果新版本解决了我的问题,我就有新版本的许可证:)Mike Liksche的GraphicsEx(VirtualTreeView的知名度)可以读取和写入TIFF文件。我会试试看——如果你读得更好,你会看到它使用GDI+库的开源包装来保存tiff图片。他还说,这些文件在内部以BMP文件的形式出现,尽管它们有.tif扩展名。包装有问题并不意味着它无法保存tiff。GDI+能够保存TIFF图片。在Delphi中,这是最简单的方法。目前这不是最简单的方法,因为它不起作用,而我提供了两种工作解决方案,它们都有自己的缺点。我将继续尝试使用开放/自由软件,然后再尝试使用监禁软件的更激进的解决方案。谢谢!Tiff一代工作得很好。但它并没有以300dpi的速度保存。但我认为这与你们的图书馆无关。错误应该在Twain配置中。