-
Notifications
You must be signed in to change notification settings - Fork 11
/
Image.pas
132 lines (114 loc) · 3.16 KB
/
Image.pas
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
{**********************************************}
{ }
{ Eduardo Rodrigues }
{ 18/09/2019 }
{ }
{**********************************************}
unit Image;
interface
uses
Vcl.Graphics,
FMX.Objects;
type
TGraphicAccess = class(Vcl.Graphics.TGraphic)
end;
function ProcessImage(sData, APad: String): String;
function ImageDFMtoFMX(sData: String): String;
function ImageToHex(Image:FMX.Objects.TImage; LineLen:integer): String;
implementation
uses
System.Classes,
Vcl.ExtCtrls,
System.SysUtils,
Vcl.Imaging.Jpeg,
Vcl.Imaging.GIFImg,
Vcl.Imaging.PngImage;
var
FPad: String;
function ProcessImage(sData, APad: String): String;
begin
FPad := APad;
Result := APad +' MultiResBitmap = <'+
sLineBreak + APad +' item '+
sLineBreak + APad +' PNG = { '+ ImageDFMtoFMX(sData) +'} '+
sLineBreak + APad +' end>';
end;
function ImageDFMtoFMX(sData: String): String;
var
Linput: String;
Loutput: TMemoryStream;
LclsName: ShortString;
Lgraphic: TGraphic;
img2: Vcl.ExtCtrls.TImage;
img1: FMX.Objects.TImage;
stream: TMemoryStream;
begin
// Remove caracteres
sData := StringReplace(sData, '{', EmptyStr, []);
sData := StringReplace(sData, '}', EmptyStr, []);
// Inicializa
Linput := sData;
Loutput := TMemoryStream.Create;
try
// Carrega dados para memoria
Loutput.Size := Length(Linput) div 2;
HexToBin(PChar(Linput), Loutput.Memory^, Loutput.Size);
LclsName := PShortString(Loutput.Memory)^;
// Cria imagem FMX
img1 := FMX.Objects.TImage.Create(nil);
// Cria imagem VCL
Lgraphic := TGraphicClass(FindClass(UTF8ToString(LclsName))).Create;
try
// Carrega dados para imagem VCL
Loutput.Position := 1 + Length(LclsName);
TGraphicAccess(Lgraphic).ReadData(Loutput);
img2 := TImage.Create(nil);
img2.Picture.Assign(Lgraphic);
// Converte de VCL para FMX
stream:= TMemoryStream.Create;
try
img2.Picture.SaveToStream(stream);
stream.Position := 0;
img1.Bitmap.LoadFromStream(stream);
finally
stream.Free;
end;
// Retorna imagem convertida de FMX para texto
Result := ImageToHex(img1, 64);
finally
img1.Free;
Lgraphic.Free;
end;
finally
Loutput.Free;
end;
end;
function ImageToHex(Image:FMX.Objects.TImage; LineLen:integer): String;
var
ms:TMemoryStream;
s:String;
t:Ansistring;
begin
ms := TMemoryStream.Create;
try
image.Bitmap.SaveToStream(ms);
SetLength(t, ms.Size * 2);
BinToHex(ms.Memory^, Pansichar(t), ms.Size);
repeat
s := Copy(String(t), 1, LineLen);
Result := Result + sLineBreak + FPad +' '+ s;
Delete(t, 1, LineLen);
until t = '';
finally
ms.Free;
end;
end;
initialization
System.Classes.RegisterClass(TMetafile);
System.Classes.RegisterClass(TIcon);
System.Classes.RegisterClass(TBitmap);
System.Classes.RegisterClass(TWICImage);
System.Classes.RegisterClass(TJpegImage);
System.Classes.RegisterClass(TGifImage);
System.Classes.RegisterClass(TPngImage);
end.