- 人气:
- 放大
- 缩小
- 二维码
- 赞赏
delphi 从摄像头获取照片并转换为特定的格式
首先,我们要使用 TTakePhotoFromCamera 这个 Action 来获取一幅照片,在其 OnDidFinishTaking 事件中,可以得到一个 TBitmap 的图片。这块我们跳过代码。
然后,我们要将这个位图保存为我们希望的格式,如JPEG。默认调用它的 SaveToStream 它会保存为 PNG 格式,我们现在提供一段保存为 JPEG 格式的代码:
Delphi/Pascal
procedure SaveImage(ABitmap:TBitmap;AStream:TStream)
var
Surf:TBitmapSurface;
AParams:TBitmapCodecSaveParams;
begin
Surf:=TBitmapSurface.Create;
try
Surf.Assign(ABitmap);
AParams.Quality:=60;//JPEG 画质,60% 在Photoshop中是中等画质
if not TBitmapCodecManager.SaveToStream(AStream,Surf,'.jpg',@AParams) then
raise EBitmapSavingFailed.Create('无法保存图片为JPG格式');
finally
Surf.DisposeOf;
end;
end;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
procedure SaveImage(ABitmap:TBitmap;AStream:TStream)
var
Surf:TBitmapSurface;
AParams:TBitmapCodecSaveParams;
begin
Surf:=TBitmapSurface.Create;
try
Surf.Assign(ABitmap);
AParams.Quality:=60;//JPEG 画质,60% 在Photoshop中是中等画质
if not TBitmapCodecManager.SaveToStream(AStream,Surf,'.jpg',@AParams) then
raise EBitmapSavingFailed.Create('无法保存图片为JPG格式');
finally
Surf.DisposeOf;
end;
end;
这样图片就转换为 JPEG 格式,保存到流中了。其中的60可以根据需要调整成你需要的品质,越大画质越好,也就越占地方,自行权衡。
来源:http://blog.qdac.cc/?p=4203