【问题标题】:Send Image from Android to Datasnap Restful Server将图像从 Android 发送到 Datasnap Restful Server
【发布时间】:2012-07-19 05:04:28
【问题描述】:

我有一个 Android 应用,它使用 XE2 中的 RESTFul 客户端将数据发送回数据快照服务器。

我可以很好地发送标准基本数据,但部分应用程序包括存储用户拍摄的图像。

我最初尝试使用 TStream,但从未返回到服务器 - 它似乎只是挂起。我目前的想法是将图像的byte[]转换为base64字符串并在datasnap结束时重新转换。

为了在 Android 端将图像转换为 base64 字符串,我执行以下操作:

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;

然后

var
    Bytes : TBytes;
  image : TJPEGImage;
  stream : TBytesStream;
begin
  Bytes := Base64Decode(Photo);
  stream := TBytesStream.Create(Bytes);
  image := TJPegImage.Create;
  image.LoadFromStream(stream);

这会在loadFromStream 方法中产生错误,基本上jpeg 已损坏。我猜要么编码有问题(不太可能),要么转换为delphi字符串然后解码为字节[](可能)。

所以这是一个冗长的方式来询问是否有人对如何将图像从 Android 应用程序发送到 Delphi XE2 中的 DataSnap 服务器有任何建议?

【问题讨论】:

  • 顺便说一下,我可以验证由于多种原因使用 TStream 从 Android 设备发送数据不起作用。我已经向 Embarcadero 提出了 QC,因此希望可以在更新中得到修复

标签: android delphi datasnap


【解决方案1】:
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);

PS.:您可以使用 ZLIB 压缩流以获得最佳性能。

【讨论】:

    【解决方案2】:

    我正在加载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 更改有关。

    【讨论】:

    • 我仍然遇到 JPEG 损坏的问题。我怀疑服务器端的解码不是 100% 正确的。我正在使用 TidMimeDecoder,但认为它没有正确解码。
    猜你喜欢
    • 2020-11-08
    • 1970-01-01
    • 1970-01-01
    • 2020-04-29
    • 2019-03-26
    • 2014-06-29
    • 2016-08-02
    • 2015-10-23
    • 2014-03-10
    相关资源
    最近更新 更多