Image To String Delphi XE7 Android and Ios - android

Im programming mobil app with WebSevices at Delphi Xe7 FireMonkey.
I have a webmethod on webservice. That webmethod is post Base64 string to my database.
Delphi Side is create this Base64 string from Bitmap.
Im use this algorithm
Uses ....EncdDecd;
function Tfrm_yenikayit.BitmapToBase64(Bitmap: TBitmap): string;
var
Input : TMemoryStream;
Output: TStringStream;
begin
Input := TMemoryStream.Create;
try
Bitmap.SaveToStream(Input);
Input.Position := 0;
Output := TStringStream.Create('');
try
EncdDecd.EncodeStream(Input, Output);
Result := Output.DataString;
finally
Output.Free;
end;
finally
Input.Free;
end;
end;
But A Photo that size's 1920x1280 is giving approximately 3 million charecter to result.
How to i making image to short string and fastest than this algorithm?

I found solution that algortihym
Function BitmapToBase64(Bitmap:Tbitmap):string;
var
BS: TBitmapSurface;
AStream: TMemoryStream;
AStringStream : TStringStream;
AResult : AnsiString;
begin
BS := TBitmapSurface.Create;
BS.Assign(Bitmap);
BS.SetSize(600,400);///Solution this
AStream := TMemoryStream.Create;
try
TBitmapCodecManager.SaveToStream(AStream, BS, '.jpg');
AStringStream := TStringStream.Create(EncodeBase64(AStream, AStream.Size));
Result:=AStringStream.DataString;
finally
AStream.Free;
AStringStream.Free;
BS.Free;
end;
end;
I found solution BS.SetSize(600,400) that its not important me quality for me

Related

how to configure tls on Delphi TRestClient component

Helo,
I typed the code as follows and this code works well.
By using an https connection I hope that the Packet Data received cannot be read by applications such as Wireshark or the Packet Capture application on Android.
how do you configure the client side?
this my code
procedure TForm1.Button1Click(Sender: TObject);
var
MyCompletionHandler: TCompletionHandler;
MyErrorCompletionHandler: TCompletionHandlerWithError;
begin
ShowLoadingIndicator(Self, True);
Memo1.Lines.Clear;
RESTClient1.BaseURL := 'https://reqres.in/';
RESTClient1.RaiseExceptionOn500 := False;
RESTClient1.SecureProtocols := [THTTPSecureProtocol.TLS12];
RESTRequest1.ClearBody;
RESTRequest1.Resource := 'api/users';
MyCompletionHandler := procedure
var i: Integer;
tJson: TJSONValue;
begin
Label1.Text := 'Complete!';
Memo1.Lines.Append('Header: ');
for I := 0 to RESTResponse1.Headers.Count-1 do
Memo1.Lines.Append(RESTResponse1.Headers.Strings[I]);
Memo1.Lines.Append('');
Memo1.Lines.Append('Body:');
tJson := TJSONObject.ParseJSONValue(RESTResponse1.Content);
try
memo1.Lines.Append(REST.Json.TJson.Format(tJson));
finally
FreeAndNil(tJson);
end;
HideLoadingIndicator(Self);
end;
MyErrorCompletionHandler := procedure(AObject: TObject)
begin
Label1.Text := 'Error!';
HideLoadingIndicator(Self);
end;
RESTRequest1.ExecuteAsync(MyCompletionHandler, True, True, MyErrorCompletionHandler);
end;
result packet capture using
app
and this simple apps made with firemonkey:
Simple Apps

Wordpress xml-rpc client using idhttp fail with EIdConnClosedGracefully on android

Basic facts:
The application was created as a multiplatform default empty application.
Wordpress is on a NonSSL connection.
On windows the code (below) works flawlessly.
On Android the data is being received by the
server flawlessly, However the application is being shut down.
Debug shows the exception IdConnClosedGracefully
The "try except" does not intercept the exception.
XE8, indy 10
Questions:
Am I doing something wrong?
Why is the exception not being intercepted.
What can I do to prevent the shutdown?
the code:
procedure TForm8.Button1Click(Sender: TObject);
var
LPostURL : String;
LXMLStream : TMemoryStream;
LHttp: TIdHttp;
begin
TTask.Run(procedure
var
LPostURL : String;
LXMLStream : TMemoryStream;
LResStream : TMemoryStream;
LHttp: TIdHttp;
begin
LHttp := TIdHTTP.Create;
LHttp.HTTPOptions := LHttp.HTTPOptions - [hoForceEncodeParams];
LHttp.HandleRedirects:=true;
buildpost;
xml.Active:=true;
LPostURL := 'http://benkyo.tk/xmlrpc.php';//edit1.Text;
LXMLStream := TMemoryStream.Create;
XML.SaveToStream(LXMLStream);
try
try
memo1.lines.text:=LHttp.post(LPostURL, LXMLStream);
except
on E:exception do
memo1.Lines.Text:=('IdHTTP1.Post error'+ E.Message);
end;
finally
LHttp.Free;
LXMLStream.Free;
end;
end);
end;

how to modify a function so it returns an array of strings [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
can you help me to with this function
function TdmPush.GetDeviceRegistrationId: string;
begin
{$IFDEF ANDROID}
result := gcmn.RegistrationID;
{$ELSE}
result := 'Mobile Test';
{$ENDIF}
end;
function TdmPush.PushMessage(Pushmessage : string):string;
const
sendUrl = 'https://android.googleapis.com/gcm/send';
var
Params: TStringList;
AuthHeader: STring;
idHTTP: TIDHTTP;
SSLIOHandler: TIdSSLIOHandlerSocketOpenSSL;
begin
idHTTP := TIDHTTP.Create(nil);
try
SslIOHandler := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
idHTTP.IOHandler := SSLIOHandler;
idHTTP.HTTPOptions := [];
Params := TStringList.Create;
try
Params.Add('registration_id='+ GetDeviceRegistrationId());
Params.Values['data.message'] := Pushmessage;
idHTTP.Request.Host := sendUrl;
AuthHeader := 'Authorization: key=' + YOUR_API_ID;
idHTTP.Request.CustomHeaders.Add(AuthHeader);
IdHTTP.Request.ContentType := 'application/x-www-form- urlencoded;charset=UTF-8';
result := idHTTP.Post(sendUrl, Params);
finally
Params.Free;
end;
finally
FreeAndNil(idHTTP);
end;
end;
I need the function GetDeviceRegeistrationID to return an array of registration id, so I can modify the Push method.
An example how to assign the text from the items of a TListView into an array of string.
function TdmPush.GetDeviceRegistrationId: TArray<string>;
var
i: Integer;
begin
SetLength(Result, myListView.Items.Count);
...
// Fill the array
for i := 0 to myListView.Items.Count-1 do
Result[i] := myListView.Items[i].Text;
end;
In case of Delphi 2010+ you can use LURD's answer (and you better do - for relaxed types compatibility)
In case of earlier Delphi you have to use another type:
uses Types;
function TdmPush.GetDeviceRegistrationId: tStringDynArray;
///...the rest is the same as with LU RD...
Also to be independent from Delphi RTL you can declare the type yourself
type MyStringsArray = array of string;
function TdmPush.GetDeviceRegistrationId: MyStringsArray ;
///...the rest is the same as with LU RD...
PS. I know that formally Delphi 2009 also has TArray, but usng generics in 2009 is a ticket to hell.
PPS. if you can not know the exact number of strings in the array in advance, then for the sake of scaling heap memory management use special classes:
uses Generics.Collections;
function TdmPush.GetDeviceRegistrationId: TArray<string>;
var ls: TList<string>;
begin
ls := TList<string>.Create;
try
ls.Add('aaaa');
ls.Add('bbb');
ls.Add('cccccc');
....
ls.Add('$%#$#');
Result := ls.ToArray();
finally
ls.Destroy;
end;
end;
To make a lazy filling of a ListView from the 1D vector of strings one cn utilize LiveBinding.
The general idea - http://docwiki.embarcadero.com/RADStudio/XE8/en/Mobile_Tutorial:_Using_LiveBindings_to_Populate_a_ListView_(iOS_and_Android)
Using TStringList as binding data source - http://www.webdelphi.ru/2011/11/firemonkey-ot-prostogo-k-slozhnomu-3-komponenty-fmx-spiski-prodolzhenie/

delphi xe6 No mapping for the Unicode character exists in the target multi-byte code page

The following code works on Win32, anyway it is trowing exception if run on Android or iOS. The exception is : "No mapping for the Unicode character exists in the target multi-byte code page"
function GetURLAsString(aURL: string): string;
var
lHTTP: TIdHTTP;
lStream: TStringstream;
begin
lHTTP := TIdHTTP.Create(nil);
lStream := TStringstream.Create(result);//create('',EEncoding.utf8),not work
try
lHTTP.Get(aURL, lStream);
lStream.Position := 0;
result := lStream.readstring(lStream.Size);//error here
finally
FreeAndNil(lHTTP);
FreeAndNil(lStream);
end;
end;
TStringStream is not a suitable class for this situation. It requires you to specify an encoding in its constructor. If you do not, it uses the OS default encoding instead. On Windows, that default is locale-specific to the user account that is running your app. On mobile, that default is UTF-8 instead.
HTTP can transmit text using any number of charsets. If the data does not match the encoding used by the TStringStream, you are going to run into decoding problems.
TIdHTTP knows the charset of the received data and can decode it into a Delphi string for you, eg:
function GetURLAsString(aURL: string): string;
var
lHTTP: TIdHTTP;
begin
lHTTP := TIdHTTP.Create(nil);
try
Result := lHTTP.Get(aURL);
finally
FreeAndNil(lHTTP);
end;
end;

Taking pictures on Android from Delphi Firemonkey XE5 app

Has anyone been able to take pictures from camera on Android from within the app written in Delphi Firemonkey XE5? How about the video capture?
This is believed to be either a bug in a framework or just something with missing documentation about it.
Can anyone tell why the code bellow doesn't work / retrieve any image from a camera on Android?
Dropped a TCameraComponent on a form, and a TImage component as well, and nothing happens.
procedure TCameraComponentForm.OnCreate(Sender: TObject);
begin
CameraComponent1.Kind := FMX.Media.TCameraKind.ckFrontCamera;
CameraComponent1.FlashMode := FMX.Media.TFlashMode.fmFlashOff;
CameraComponent1.Active := True;
end;
procedure TCameraComponentForm.CameraComponent1SampleBufferReady(
Sender: TObject; const ATime: Int64);
begin
CameraComponent1.SampleBufferToBitmap(Image1.Bitmap, True);
Image1.Width := Image1.Bitmap.Width;
Image1.Height := Image1.Bitmap.Height;
end;
Permissions are set correctly.
This code works fine:
procedure TfrmPrincipal.SampleBufferSync;
begin
cmcPrincipal.SampleBufferToBitmap(imgFoto.Bitmap, true);
end;
procedure TfrmPrincipal.cmcPrincipalSampleBufferReady(Sender: TObject;
const ATime: Int64);
begin
TThread.Synchronize(TThread.CurrentThread, SampleBufferSync);
// CameraComponent1.SampleBufferToBitmap(imgFoto.Bitmap, True);
// imgFoto.Width := imgFoto.Bitmap.Width;
// imgFoto.Height := imgFoto.Bitmap.Height;
end;
procedure TfrmPrincipal.FormShow(Sender: TObject);
begin
cmcPrincipal.Kind := FMX.Media.TCameraKind.ckBackCamera;
try
cmcPrincipal.FlashMode := FMX.Media.TFlashMode.fmFlashOff;
except
end;
cmcPrincipal.Active := True;
end;

Categories

Resources