我正在尝试创建一个上传图片的 Delphi XE5 Android Datasnap 应用程序(常规,而不是 REST)。
请参阅下面的代码。代码有效!Android 手机上的摄像头拍摄的图像被传输到服务器并保存。问题是:
- 保存到服务器的图像被 IrfanView 识别为 .png。将 image001.bmp 重命名为 image001.png 后,它是可见的,但是
- 它始终是 800 x 600
- 照片也会自动保存到 Android 的 library/pictures/camera 文件夹中。此图像是 .jpg,大小为 1600 x 1200。它不是 .bmp 或 .png。
我很困惑。所有这些自动转换发生在哪里?
客户端代码如下所示
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;
服务器上的方法是这样的
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;