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
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)
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)