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

发布于 2024-11-19 08:30:19 字数 1424 浏览 1 评论 0原文

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

  • 以 300dpi 保存(扫描仪可以)
  • 以 TIFF 格式保存

经过深入研究,我发现了 2 个提示:

http://www.delphipraxis.net/132787-farbstich-nach-bitmap-operation.html

http://synopse.info/fossil/wiki?name=GDI%2B

这是我的最终代码:

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 扩展名)。

我缺少什么?

I'm using Delphitwain (delphitwain.sourceforge.net) to add scan functionality to my app. Scanning is working fine and I can save bmp and jpeg files. Now I need to:

  • save in 300dpi (the scanner is capable)
  • save in TIFF format

After digging around, I found 2 tips:

http://www.delphipraxis.net/132787-farbstich-nach-bitmap-operation.html

http://synopse.info/fossil/wiki?name=GDI%2B

Here is my final code:

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;

Result: the image still is 96dpi and were saved as BMP (even with TIF extension).

What am I missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

猫瑾少女 2024-11-26 08:30:19

GDI+库能够保存tiff图片。

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

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

  • 要么你没有安装相应的TIFF编码器;要么你没有安装相应的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 参数,似乎是必需的

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

The GDI+ library is able to save tiff pictures.

The SynGdiPlus unit use {557CF405-1A04-11D3-9A73-0000F81EF32E} for its TIFF encoder.

From the TSynPicture.SaveAs code, I see two possibilities:

  • Either you don't have the corresponding TIFF encoder installed;
  • Either there is some missing parameter expected by the TIFF encoder.

Try this version:

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

It will add the EncoderCompression parameter for TIFF pictures, which seems to be required.

I've updated the source code repository version to include this correction.

若言繁花未落 2024-11-26 08:30:19

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

ImageEn 有效。

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

Saving TIFF files is hard in Delphi. I don't know any free/open source modules that do this.

ImageEn works.

In the past I have used saving as bmp and converting to tiff with irfanview through createprocess with the commandline
'i_view32.exe c:\temp\scanned.bmp /bpp=1 /convert=c:\temp\scanned.tif'

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文