Entwickler-Ecke

Multimedia / Grafik - Tbitmap als 8 bit grey scale abspeichern


nepleurepas - Mo 15.09.08 19:42
Titel: Tbitmap als 8 bit grey scale abspeichern
Hallo zusammen,
ich möchte in meinem Program die OCR Funktion vom Delphi OCR Reader nutzen. Hierfür müssen die Bitmaps jedoch im 8 bit grey scale Format vorliegen. Ich habe nun die bilder schwarzweiß abgepsichert, d.h. ich habe jeden pixel ab einer bestimmten Dunkelheit komplett schwarz gemacht und alle anderen Pixel komplett weiß. Außerdem habe ich die bilder auf pf8bit gesetzt. Leider liefert der OCR Reader immer noch die Fehlermeldung, dass nur 8 bit gray scale images gelesen werden können.
Kann mir da jemand sagen, wie ich eine wirkliche 8 bit greyscale machen kann? Also wenn ich das bild mit dem Microsoft Photo editor abpseicher, funktioniert das OCR Program...

Wäre für antworten dankbar...

Gruß Stefan


nepleurepas - Do 18.09.08 22:02
Titel: Re: Tbitmap als 8 bit grey scale abspeichern
hier is mal ne teillösung...

Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
procedure TForm1.Button1Click(Sender: TObject);
type
  TLogPal = record
    lpal : TLogPalette;
    colorSpace : Array[0..255of TPaletteEntry; // This allocate room to
                                                 // new palette colors
                                                 // since palPalEntry member of
                                                 // TLogPalette is declared as
                                                 // Array [0..0] of TPaletteEntry
  end;
var
  bmp : TBitmap;
  pal : TLogPal;
  iCount : integer;
begin
  // Loads the image
  Image1.Picture.LoadFromFile('my_picture.bmp');

  // Create a 256 gray-scale palette
  pal.lpal.palVersion:=$300;
  pal.lpal.palNumEntries := 256;
  for iCount := 0 to 255 do
    with pal.lpal.palPalEntry[iCount] do
      begin
        peRed := iCount;
        peGreen := iCount;
        peBlue := iCount;
      end;

  // Create a temporary bitmap
  bmp := TBitmap.Create;
  try
    // Define bitmap as 8 bit color
    bmp.PixelFormat := pf8bit;

    // Define width and height
    bmp.Width := Image1.Picture.Width;
    bmp.Height := Image1.Picture.Height;

    // Create our new grayscale palette
    bmp.Palette := CreatePalette(pal.lpal);


    // Draw the image on bmp surface
    bmp.Canvas.Draw(0,0,Image1.Picture.Graphic);


    // Save the new 8bit file
    bmp.SaveToFile('my_picture-8bit.bmp');

  finally
    // Free temp bitmap
    bmp.Free;
  end;
end;