Atozed Forums

Full Version: compress the following image data
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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 

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 ?
(05-15-2020, 02:57 AM)Madammar Wrote: [ -> ]i have Jpeg image that loaded into memory stream and later on i use TIdEncoderMIME To encode the stream to a base64 string 
...
but the output base64 string length is 4000 which is too big. is there any way to compress this string to get smaller length ?

Base64 adds overhead. The length of the string will be 1.33x times higher than the data being encoded. Which means your compressed JPG is 3000 bytes before base64 encoding is applied. If that is too high, you will just have to get it down lower. For instance, by increasing the CompressionQuality.

Why are you using base64 in the first place? Why can't you use the JPG binary instead?

(05-15-2020, 02:57 AM)Madammar Wrote: [ -> ]i already used zlib to compress the StringImageData

Since JPG is already compressed, compressing it again won't really do much. Not to mention the fact that you are compressing the base64 string, not the binary that was encoded to base64. Since the JPG is smaller than the base64, you should just send the binary JPG data instead.

(05-15-2020, 02:57 AM)Madammar Wrote: [ -> ]what is the correct way to compress the output data string ?

To not compress it at all. Seriously, it is just a waste of time and effort to compress/decompress base64. Instead, solve the issue with the JPG being too large to begin with.