Mega Search
23.2 Million


Sign Up

Make a donation  
Android memorystream size [Edit]  
News Group: embarcadero.public.delphi.firemonkey

How to check the size of a photo taken from camera in android. I used the below code but the size returning is not correct. For example the size of the photo is 160KB only but the below code return 386KB.  

procedure TForm1.TakePhotoFromCameraAction1DidFinishTaking(Image: TBitmap);
 var
 mStream : TMemoryStream;
 msize : integer;
begin
try
     mStream  := TMemoryStream.Create();
     Image.SaveToStream(mStream);
     Label1.Text := IntToStr(mStream.Size div 1024);

finally
  mStream.Free;
end;
end;

Edited by: Pramod Nair on Jan 21, 2015 1:01 AM

Vote for best question.
Score: 0  # Vote:  0
Date Posted: 21-Jan-2015, at 1:02 AM EST
From: Pramod Nair
 
Re: Android memorystream size [Edit]  
News Group: embarcadero.public.delphi.firemonkey
Thanks a lot Remy. I was excepting the answer from you Sir!

> {quote:title=Remy Lebeau (TeamB) wrote:}{quote}
> Pramod wrote:
> 
> > How to check the size of a photo taken from camera in android. I used
> > the below code but the size returning is not correct. For example the
> > size of the photo is 160KB only but the below code return 386KB.
> 
> The OnDidFinishTaking event does not provide you access to the actual file 
> that is captured by the camera, only access to the image pixel data.  So 
> a direct comparison is not possible, since you don't know where the image 
> was saved or in what format.
> 
> The TBitmap.SaveToStream() method always saves in PNG format only.  The image 
> saved in the OS Gallery may be in a different format, such as TIFF or JPG, 
> depending on the OS's camera settings.  TBitmap.SaveToStream() does not allow 
> you to specify the output format, but the TBitmap.SaveToFile() method does, 
> eg:
> 
> {code}
> Image.SaveToFile('filename.jpg'); // <-- the file extension specifies which 
> format to use
> // you can optionally specify addition parameters, eg:
> // Image.SaveToFile('filename.jpg', 'quality=50');
> {code}
> 
> If you really need the data in a TStream but in a non-PNG format, you will 
> have to skip TBitmap.SaveToStream() and invoke FireMonkey's codec directly, 
> eg:
> 
> {code}
> procedure TForm1.TakePhotoFromCameraAction1DidFinishTaking(Image: TBitmap);
> var
>   mStream : TMemoryStream;
>   msize : integer;
>   codec: TBitmapCodec;
> begin
>   mStream := TMemoryStream.Create;
>   try
>     //Image.SaveToStream(mStream);
>     codec := DefaultBitmapCodecClass.Create;
>     try
>       codec.SaveToStream(mStream, Image, 'jpg');
>       // you can optionally specify addition parameters, eg:
>       // codec.SaveToStream(mStream, Image, 'jpg', 'quality=50');
>     finally
>       codec.Free;
>     end;
>     Label1.Text := IntToStr(mStream.Size div 1024);
>   finally
>     mStream.Free;
>   end;
> end;
> {code}
> 
> -- 
> Remy Lebeau (TeamB)

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 21-Jan-2015, at 6:07 PM EST
From: Pramod Nair
 
Re: Android memorystream size [Edit]  
News Group: embarcadero.public.delphi.firemonkey
Pramod wrote:

> How to check the size of a photo taken from camera in android. I used
> the below code but the size returning is not correct. For example the
> size of the photo is 160KB only but the below code return 386KB.

The OnDidFinishTaking event does not provide you access to the actual file 
that is captured by the camera, only access to the image pixel data.  So 
a direct comparison is not possible, since you don't know where the image 
was saved or in what format.

The TBitmap.SaveToStream() method always saves in PNG format only.  The image 
saved in the OS Gallery may be in a different format, such as TIFF or JPG, 
depending on the OS's camera settings.  TBitmap.SaveToStream() does not allow 
you to specify the output format, but the TBitmap.SaveToFile() method does, 
eg:

{code}
Image.SaveToFile('filename.jpg'); // <-- the file extension specifies which 
format to use
// you can optionally specify addition parameters, eg:
// Image.SaveToFile('filename.jpg', 'quality=50');
{code}

If you really need the data in a TStream but in a non-PNG format, you will 
have to skip TBitmap.SaveToStream() and invoke FireMonkey's codec directly, 
eg:

{code}
procedure TForm1.TakePhotoFromCameraAction1DidFinishTaking(Image: TBitmap);
var
  mStream : TMemoryStream;
  msize : integer;
  codec: TBitmapCodec;
begin
  mStream := TMemoryStream.Create;
  try
    //Image.SaveToStream(mStream);
    codec := DefaultBitmapCodecClass.Create;
    try
      codec.SaveToStream(mStream, Image, 'jpg');
      // you can optionally specify addition parameters, eg:
      // codec.SaveToStream(mStream, Image, 'jpg', 'quality=50');
    finally
      codec.Free;
    end;
    Label1.Text := IntToStr(mStream.Size div 1024);
  finally
    mStream.Free;
  end;
end;
{code}

-- 
Remy Lebeau (TeamB)

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 21-Jan-2015, at 9:57 AM EST
From: Remy Lebeau (TeamB)