Freddie Bell
Freddie Bell

Reputation: 2307

Image uploaded from Android photo to a Datasnap server has different image type and dimensions

I am attempting to create a Delphi XE5 Android Datasnap application (regular, not REST) that uploads pics.

See the code below. The code works! The image taken by the camera on the Android phone gets transferred to the server and saved. The problems are:

  1. the image that is saved to the server is recognised by IrfanView as a .png. After renaming image001.bmp to image001.png, it is viewable but
  2. it is consistently 800 x 600
  3. the photo is also automatically saved to the Android in the library/pictures/camera folder. This image is a .jpg, with a size of 1600 x 1200. It is not a .bmp or .png.

I'm confused. Where is all this automatic conversion taking place?

The client code looks like this

procedure TfrmMain.TakePhotoFromCamerAction1FinishTaking(Image: TBitMap)
var
  ImageStream: TMemoryStream;
  Bytes: Integer;
  FileName: String;
begin
  ImageStream := TMemoryStream.Create;
  try
    Image.SaveToStream(ImageStream);
    ImageStream.Position := 0;
    FileName := 'Image001.bmp';
    Bytes := ClientModule1.ServerMethods1Client.UploadImage(FileName, ImageStream);
    if Bytes = -1 then
       raise exception.create('Image transfer failed!')
  finally
    ImageStream.Free
  end;
end;

The method on the server looks like this

function TServerMethods1.UploadImage(FileName: String; Stream: TStream): Integer;
const
  BufSize = $F000;
var
  Mem: TMemoryStream;
  BytesRead: Integer;
  Buffer: PByte;
begin

  frmMain.LogMessage('Upload image ' + FileName);
  try
    Mem := TMemoryStream.Create;
    GetMem(Buffer, BufSize);
    try
      repeat
        BytesRead := Stream.Read(Buffer^, BufSize);
        if BytesRead > 0 then
          Mem.WriteBuffer(Buffer^, BytesRead);
      until BytesRead < BufSize;
      // here, replace with DB update
      // for now, save to file
      Mem.Seek(0, TseekOrigin.soBeginning); // necessary?
      Mem.SaveToFile(ExtractFilePath(Application.ExeName) + FileName);
      // ============================
      Result := 1; // Mem.Size doesn't work...
    finally
      FreeMem(Buffer, BufSize);
      Mem.Free;
    end;
  except
    Result := -1
  end;
end;

Upvotes: 1

Views: 2182

Answers (0)

Related Questions