-
Notifications
You must be signed in to change notification settings - Fork 33
/
HGM.FMX.Image.pas
465 lines (427 loc) · 11.5 KB
/
HGM.FMX.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
unit HGM.FMX.Image;
interface
uses
System.Classes, System.Types, System.SysUtils, FMX.Forms, FMX.Graphics,
FMX.Objects, System.Threading, System.Generics.Collections,
System.Net.HttpClient;
type
TCallbackObject = record
Owner: TComponent;
Bitmap: TBitmap;
Url: string;
Task: ITask;
OnDone: TProc<Boolean>;
procedure Done(const Success: Boolean);
end;
TObjectOwner = class(TComponent)
procedure Notification(AComponent: TComponent; Operation: TOperation); override;
end;
TBitmapHelper = class helper for TBitmap
private
class var
Pool: TThreadPool;
FCallbackList: TThreadList<TCallbackObject>;
FObjectOwner: TComponent;
FClient: THTTPClient;
FCachePath: string;
class function UrlToCacheName(const Url: string): string;
class procedure AddCallback(Callback: TCallbackObject);
class procedure Ready(const Url: string; Stream: TStream);
class function Get(const URL: string): TMemoryStream; static;
class function GetClient: THTTPClient; static;
class procedure SetCachePath(const Value: string); static;
class function FindCached(const Url: string; out Stream: TMemoryStream): Boolean; static;
class procedure AddCache(const Url: string; Stream: TMemoryStream);
class procedure AddCacheFileName(const FileName: string; Stream: TMemoryStream);
class function FindCachedFileName(const FileName: string; out Stream: TMemoryStream): Boolean; static;
public
class procedure RemoveCallback(const AOwner: TComponent);
class procedure CancelAll;
procedure LoadFromUrl(const Url: string; UseCache: Boolean = True);
procedure LoadFromUrlAsync(AOwner: TComponent; const Url: string; Cache: Boolean = True; OnDone: TProc<Boolean> = nil); overload;
// Cache first check
procedure LoadFromUrlAsyncCF(AOwner: TComponent; const Url: string; Cache: Boolean = True; OnDone: TProc<Boolean> = nil); overload;
procedure LoadFromUrlAsyncCF(AOwner: TComponent; const Url, CachedFileName: string; OnDone: TProc<Boolean> = nil); overload;
procedure LoadFromResource(ResName: string); overload;
procedure LoadFromResource(Instanse: NativeUInt; ResName: string); overload;
procedure SaveToStream(Stream: TStream; const Ext: string); overload;
procedure SaveToFile(const AFileName: string; const Ext: string); overload;
class function CreateFromUrl(const Url: string; UseCache: Boolean = True): TBitmap;
class function CreateFromResource(ResName: string; Url: string = ''): TBitmap;
class property Client: THTTPClient read GetClient;
class property CachePath: string read FCachePath write SetCachePath;
end;
implementation
uses
FMX.Surfaces, FMX.Types, FMX.Consts, System.Hash, System.IOUtils;
{ TBitmapHelper }
class procedure TBitmapHelper.AddCallback(Callback: TCallbackObject);
begin
Callback.Owner.FreeNotification(FObjectOwner);
FCallbackList.Add(Callback);
end;
class procedure TBitmapHelper.CancelAll;
begin
var List := FCallbackList.LockList;
try
for var i := List.Count - 1 downto 0 do
List[i].Task.Cancel;
List.Clear;
finally
FCallbackList.UnlockList;
end;
end;
class function TBitmapHelper.CreateFromResource(ResName, Url: string): TBitmap;
begin
Result := TBitmap.Create;
Result.LoadFromResource(ResName);
end;
class function TBitmapHelper.CreateFromUrl(const Url: string; UseCache: Boolean): TBitmap;
begin
Result := TBitmap.Create;
Result.LoadFromUrl(Url, False);
end;
procedure TBitmapHelper.LoadFromResource(ResName: string);
begin
LoadFromResource(HInstance, ResName);
end;
procedure TBitmapHelper.LoadFromResource(Instanse: NativeUInt; ResName: string);
var
Mem: TResourceStream;
begin
Mem := TResourceStream.Create(Instanse, ResName, RT_RCDATA);
try
Mem.Position := 0;
Self.LoadFromStream(Mem);
finally
Mem.Free;
end;
end;
procedure TBitmapHelper.LoadFromUrl(const Url: string; UseCache: Boolean);
var
Mem: TMemoryStream;
begin
Mem := Get(Url);
try
Mem.Position := 0;
Self.LoadFromStream(Mem);
finally
Mem.Free;
end;
end;
class function TBitmapHelper.Get(const URL: string): TMemoryStream;
begin
if URL.IsEmpty then
raise Exception.Create('Empty URL');
Result := TMemoryStream.Create;
try
if (GetClient.Get(URL, Result).StatusCode = 200) and (Result.Size > 0) then
Result.Position := 0
else
begin
Result.Free;
Result := nil;
end;
except
Result.Free;
Result := nil;
end;
end;
class function TBitmapHelper.GetClient: THTTPClient;
begin
if not Assigned(FClient) then
begin
FClient := THTTPClient.Create;
FClient.HandleRedirects := True;
end;
Result := FClient;
end;
class function TBitmapHelper.FindCached(const Url: string; out Stream: TMemoryStream): Boolean;
begin
Result := False;
Stream := nil;
var FileName := TPath.Combine(FCachePath, UrlToCacheName(Url));
if TFile.Exists(FileName) then
try
Stream := TMemoryStream.Create;
Stream.LoadFromFile(FileName);
Result := True;
except
Stream.Free;
Stream := nil;
end;
end;
class function TBitmapHelper.FindCachedFileName(const FileName: string; out Stream: TMemoryStream): Boolean;
begin
Result := False;
Stream := nil;
var FilePath := TPath.Combine(FCachePath, FileName);
if TFile.Exists(FilePath) then
try
Stream := TMemoryStream.Create;
Stream.LoadFromFile(FilePath);
Result := True;
except
Stream.Free;
Stream := nil;
end;
end;
class procedure TBitmapHelper.AddCacheFileName(const FileName: string; Stream: TMemoryStream);
begin
var FilePath := TPath.Combine(FCachePath, FileName);
try
if TFile.Exists(FilePath) then
TFile.Delete(FilePath);
except
Exit;
end;
try
Stream.SaveToFile(FilePath);
except
//
end;
end;
class procedure TBitmapHelper.AddCache(const Url: string; Stream: TMemoryStream);
begin
var FileName := TPath.Combine(FCachePath, UrlToCacheName(Url));
try
if TFile.Exists(FileName) then
TFile.Delete(FileName);
except
Exit;
end;
try
Stream.SaveToFile(FileName);
except
//
end;
end;
procedure TBitmapHelper.LoadFromUrlAsync(AOwner: TComponent; const Url: string; Cache: Boolean; OnDone: TProc<Boolean>);
begin
if AOwner = nil then
raise Exception.Create('You must specify an owner (responsible) who will ensure that the Bitmap is not destroyed before the owner');
var Callback: TCallbackObject;
Callback.Owner := AOwner;
Callback.Bitmap := Self;
Callback.Url := Url;
Callback.OnDone := OnDone;
Callback.Task := TTask.Run(
procedure
begin
try
var Mem: TMemoryStream;
if not FindCached(Url, Mem) then
begin
Mem := Get(Url);
if Cache and Assigned(Mem) then
AddCache(Url, Mem);
end;
TThread.ForceQueue(nil,
procedure
begin
Ready(Url, Mem);
end);
except
TThread.ForceQueue(nil,
procedure
begin
Ready(Url, nil);
end);
end;
end, Pool);
AddCallback(Callback);
end;
procedure TBitmapHelper.LoadFromUrlAsyncCF(AOwner: TComponent; const Url, CachedFileName: string; OnDone: TProc<Boolean>);
begin
var Stream: TMemoryStream;
if FindCachedFileName(CachedFileName, Stream) then
try
Stream.Position := 0;
try
LoadFromStream(Stream);
if Assigned(OnDone) then
OnDone(True);
Exit;
except
// reload
end;
finally
Stream.Free;
end;
if AOwner = nil then
raise Exception.Create('You must specify an owner (responsible) who will ensure that the Bitmap is not destroyed before the owner');
var Callback: TCallbackObject;
Callback.Owner := AOwner;
Callback.Bitmap := Self;
Callback.Url := Url;
Callback.OnDone := OnDone;
Callback.Task := TTask.Run(
procedure
begin
try
var Mem := Get(Url);
if Assigned(Mem) then
AddCacheFileName(CachedFileName, Mem);
TThread.ForceQueue(nil,
procedure
begin
Ready(Url, Mem);
end);
except
TThread.ForceQueue(nil,
procedure
begin
Ready(Url, nil);
end);
end;
end, Pool);
AddCallback(Callback);
end;
procedure TBitmapHelper.LoadFromUrlAsyncCF(AOwner: TComponent; const Url: string; Cache: Boolean; OnDone: TProc<Boolean>);
begin
if Url.IsEmpty then
begin
if Assigned(OnDone) then
OnDone(False);
Exit;
end;
var Stream: TMemoryStream;
if FindCached(Url, Stream) then
try
Stream.Position := 0;
try
LoadFromStream(Stream);
if Assigned(OnDone) then
OnDone(True);
except
if Assigned(OnDone) then
OnDone(False);
end;
Exit;
finally
Stream.Free;
end;
LoadFromUrlAsync(AOwner, Url, Cache, OnDone);
end;
class procedure TBitmapHelper.Ready(const Url: string; Stream: TStream);
begin
try
var List := FCallbackList.LockList;
try
for var i := List.Count - 1 downto 0 do
begin
var Item := List[i];
if Item.Url <> Url then
Continue;
var Success: Boolean := False;
try
if Assigned(Stream) then
try
Stream.Position := 0;
Item.Bitmap.LoadFromStream(Stream);
Success := True;
except
//
end;
finally
Item.Done(Success);
end;
List.Delete(i);
end;
finally
FCallbackList.UnlockList;
end;
finally
Stream.Free;
end;
end;
class procedure TBitmapHelper.RemoveCallback(const AOwner: TComponent);
begin
var List := FCallbackList.LockList;
try
for var i := List.Count - 1 downto 0 do
if List[i].Owner = AOwner then
begin
List[i].Task.Cancel;
List.Delete(i);
end;
finally
FCallbackList.UnlockList;
end;
end;
procedure TBitmapHelper.SaveToFile(const AFileName, Ext: string);
var
Stream: TFileStream;
begin
Stream := TFileStream.Create(AFileName, fmCreate);
try
SaveToStream(Stream, Ext);
finally
Stream.Free;
end;
end;
procedure TBitmapHelper.SaveToStream(Stream: TStream; const Ext: string);
var
Surf: TBitmapSurface;
begin
TMonitor.Enter(Self);
try
Surf := TBitmapSurface.Create;
try
Surf.Assign(Self);
if not TBitmapCodecManager.SaveToStream(Stream, Surf, Ext) then
raise EBitmapSavingFailed.Create(SBitmapSavingFailed);
finally
Surf.Free;
end;
finally
TMonitor.Exit(Self);
end;
end;
class procedure TBitmapHelper.SetCachePath(const Value: string);
begin
FCachePath := Value;
end;
class function TBitmapHelper.UrlToCacheName(const Url: string): string;
begin
Result := THashMD5.GetHashString(Url);
end;
{ TObjectOwner }
procedure TObjectOwner.Notification(AComponent: TComponent; Operation: TOperation);
begin
inherited;
if Operation <> TOperation.opRemove then
Exit;
var List := TBitmap.FCallbackList.LockList;
try
for var i := List.Count - 1 downto 0 do
if List[i].Owner = AComponent then
begin
List[i].Task.Cancel;
List.Delete(i);
end;
finally
TBitmap.FCallbackList.UnlockList;
end;
end;
{ TCallbackObject }
procedure TCallbackObject.Done(const Success: Boolean);
begin
if Assigned(OnDone) then
try
OnDone(Success);
except
//
end;
end;
initialization
TBitmap.Pool := TThreadPool.Create;
TBitmap.FCallbackList := TThreadList<TCallbackObject>.Create;
TBitmap.FObjectOwner := TObjectOwner.Create(nil);
TBitmap.FClient := nil;
finalization
TBitmap.Pool.Free;
TBitmap.FCallbackList.Free;
TBitmap.FObjectOwner.Free;
TBitmap.FClient.Free;
end.