3

XE2 の RESTFul クライアントを使用して datasnap サーバーにデータを送信する Android アプリがあります。

標準の基本データの送信には問題なく機能していますが、アプリの一部には、ユーザーが撮影した画像の保存が含まれています。

最初に 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 文字列に変換してから byte[] にデコードする (可能性が高い) のいずれかに問題があると思います。

Android アプリから Delphi XE2 の DataSnap サーバーに画像を送信する方法について何か提案があるかどうかを尋ねるには、これは長い道のりです。

4

2 に答える 2

0

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 の変更に関連しているかどうかを確認します。

于 2012-07-19T14:37:54.767 に答える