05-15-2020, 02:57 AM
i have Jpeg image that loaded into memory stream and later on i use TIdEncoderMIME To encode the stream to a base64 string
with the following code
but the output base64 string length is 4000 which is too big. is there any way to compress this string to get smaller length ?
i already used zlib to compress the StringImageData with the following code
but image got corrupted after decompression what is the correct way to compress the output data string ?
with the following code
Code:
try
JpegStream := TMemoryStream.Create;
try
Jpg := TJPEGImage.Create;
try
Jpg.Performance := jpBestSpeed;
Jpg.ProgressiveEncoding := True;
Jpg.ProgressiveDisplay := True;
Jpg.Assign(btmpcam);
Jpg.CompressionQuality := Qualitycm;
Jpg.Compress;
Jpg.SaveToStream(JpegStream);
finally
Jpg.Free;
end;
JpegStream.Position := 0;
StringImageData := TIdEncoderMIME.EncodeStream(JpegStream);
finally
JpegStream.Free;
end;
but the output base64 string length is 4000 which is too big. is there any way to compress this string to get smaller length ?
i already used zlib to compress the StringImageData with the following code
Code:
interface
uses
Classes, SysUtils, zlib, EncdDecd;
function ZCompressString(aText: string; aCompressionLevel: TCompressionLevel): string;
function ZDecompressString(aText: string): string;
implementation
function ZCompressString(aText: string; aCompressionLevel: TCompressionLevel): string;
var
strInput,
strOutput: TStringStream;
Zipper: TZCompressionStream;
begin
Result:= '';
strInput:= TStringStream.Create(aText);
strOutput:= TStringStream.Create;
try
Zipper:= TZCompressionStream.Create(aCompressionLevel, strOutput);
try
Zipper.CopyFrom(strInput, strInput.Size);
finally
Zipper.Free;
end;
Result:= strOutput.DataString;
finally
strInput.Free;
strOutput.Free;
end;
end;
function ZDecompressString(aText: string): string;
var
strInput,
strOutput: TStringStream;
Unzipper: TZDecompressionStream;
begin
Result:= '';
strInput:= TStringStream.Create(aText);
strOutput:= TStringStream.Create;
try
Unzipper:= TZDecompressionStream.Create(strInput);
try
strOutput.CopyFrom(Unzipper, Unzipper.Size);
finally
Unzipper.Free;
end;
Result:= strOutput.DataString;
finally
strInput.Free;
strOutput.Free;
end;
end;
but image got corrupted after decompression what is the correct way to compress the output data string ?