Jump to content
dkprojektai

Best speed solution to send bitmap to server

Recommended Posts

Hello,

 

I need to send tbitmap and identification id to local server with POST command. For this I wanted to use json.

 

Will I get max available speed? Need advice. If I right - I will have two times to do encoding. 

 

If there are other better way - please share your solution. 

 

Thank you.

Share this post


Link to post

Don't use json. I usually send via MultipartFormData, encoded in WebP with 80% quality, as it is practically identical to the original. See attached the original png image of 2292 KB and the WebP with 80% quality resulting in 61 KB.

 

photo.png

photo.webp

  • Like 1

Share this post


Link to post
On 9/6/2022 at 2:43 PM, dkprojektai said:

Can you share an example?

 

Yes! See a very simple sample:
 

uses
  System.Net.HttpClient, System.Net.Mime, Skia, Skia.FMX {or Skia.Vcl};

function SendBitmap(const AUrl: string; ABitmap: TBitmap): string;
var
  LRequest: THTTPClient;
  LFormData: TMultipartFormData;
  LResponse: TStringStream;
begin
  LRequest := THTTPClient.Create;
  LFormData := TMultipartFormData.Create;
  LResponse := TStringStream.Create;
  try
    LFormData.AddBytes('img', ABitmap.ToSkImage.EncodeToStream(LImageStream, TSkEncodedImageFormat.WEBP, 80));
    LRequest.Post(AUrl, LFormData, LResponse);
    Result := LResponse.DataString;
  finally
    LFormData.Free;
    LResponse.Free;
    LRequest.Free;
  end;
end;

 

This is a simple example for you to understand how it works. In practice I use something different. On the client (API consumer), I declare the whole API in a single unit using the Refit library. This removes a lot of complexity throughout the project, as it already magically serializes records and classes, query parameters, headers automatically.

 

The example above using Refit would look like this:

 

This is the unit of your api for the client-side (consumer):

unit MyApiConsumer;

interface

uses
  System.SysUtils, iPub.Rtl.Refit; // https://github.com/viniciusfbb/ipub-refit

type
  [BaseUrl('http://localhost/api/v1')]
  IMyAPI = interface(IipRestApi)
    ['{1D10C463-1567-4DE0-88F2-099CC0442E43}']

    [Post('/image-upload')]
    [ContentType(TBodyKind.MultipartFormData)]
    function TryImageUpload(const [Body] AImg: TBytes; out AResponse: string): Boolean;
  end;

var
  FMyApi: IMyAPI;

implementation

initialization
  FMyApi := GRestService.&For<IMyAPI>;
end.

And in your code:

uses
  MyApiConsumer, Skia, Skia.FMX {or Skia.Vcl};

function SendBitmap(ABitmap: TBitmap): string;
begin
  if not FMyApi.TryImageUpload(ABitmap.ToSkImage.EncodeToStream(LImageStream, TSkEncodedImageFormat.WEBP, 80), Result) then
    Result := 'Failed!';
end;

 

Edited by vfbb

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×