将图像从Android发送到Datasnap Restful服务器
我有一个Android应用程序,它使用XE2中的RESTFul客户端将数据发送回datasnap服务器 我可以很好地发送标准的基本数据,但应用程序的一部分包括存储用户拍摄的图像 我最初尝试使用TStream,但从未回到服务器上——它似乎挂起了。我目前的想法是将映像的字节[]转换为base64字符串,并在datasnap端重新转换 要在Android端将图像转换为base64字符串,请执行以下操作:将图像从Android发送到Datasnap Restful服务器,android,delphi,datasnap,Android,Delphi,Datasnap,我有一个Android应用程序,它使用XE2中的RESTFul客户端将数据发送回datasnap服务器 我可以很好地发送标准的基本数据,但应用程序的一部分包括存储用户拍摄的图像 我最初尝试使用TStream,但从未回到服务器上——它似乎挂起了。我目前的想法是将映像的字节[]转换为base64字符串,并在datasnap端重新转换 要在Android端将图像转换为base64字符串,请执行以下操作: ByteArrayOutputStream stream = new ByteArrayOutpu
ByteArrayOutputStream stream = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, stream);
String encodedString = Base64.encode(stream.toByteArray)
encodedString
然后作为标准Delphi字符串发送
在服务器端,要解码的代码是
function Base64Decode(const EncodedText: string): TBytes;
var
DecodedStm: TBytesStream;
Decoder: TIdDecoderMIME;
begin
Decoder := TIdDecoderMIME.Create(nil);
try
DecodedStm := TBytesStream.Create;
try
Decoder.DecodeBegin(DecodedStm);
Decoder.Decode(EncodedText);
Decoder.DecodeEnd;
Result := DecodedStm.Bytes;
SetLength(Result, DecodedStm.Size); // add this line
finally
DecodedStm.Free;
end;
finally
Decoder.Free;
end;
end;
然后
这会在loadFromStream
方法中产生错误,基本上jpeg已损坏。我猜要么编码(不太可能),要么转换成delphi字符串,然后解码成字节[](可能),都有问题
因此,这是一种冗长的方式,询问是否有人对如何将图像从Android应用程序发送到Delphi XE2中的DataSnap服务器有任何建议?我正在加载JPEG图像,但我在开始时设置了指针,并配置了图像:
stream.Seek(0,soFromBeginning);
image.PixelFormat := jf24Bit;
image.Scale := jsFullSize;
image.GrayScale := False;
image.Performance := jpBestQuality;
image.ProgressiveDisplay := True;
image.ProgressiveEncoding := True;
image.LoadFromStream(stream);
If stream.size > 0 then
// OK
else
// not OK
我还将尝试解码一个ANSIString,以检查它是否与Unicode更改相关
uses
DBXJSONCommon,
function TServerImageMethods.ConvertJPEGToJSon(pFilePath: string): TJSONArray;
var
AFileStream: TFileStream;
begin
AFileStream := TFileStream.Create(pFilePath, fmOpenRead);
Result := TDBXJSONTools.StreamToJSON(AFileStream, 0, AFileStream.Size);
end;
我通过以下方式转换回TStream:
AFileStream := TDBXJSONTools.JSONToStream(JSONArray);
注:您可以使用ZLIB压缩流以获得最佳性能。我仍然遇到JPEG被破坏的问题。我怀疑服务器端的解码不是100%正确。我使用的是TidMimeDecoder,但我认为它没有正确解码。顺便说一句,我可以验证,出于多种原因,从Android设备使用TStream发送数据不起作用。我已经和Embarcadero联系了一位QC,希望能在更新中得到修复
AFileStream := TDBXJSONTools.JSONToStream(JSONArray);