KimHJ
Members-
Content Count
39 -
Joined
-
Last visited
Everything posted by KimHJ
-
I have this code where I send an jpg image with TidTCPClient to a TidTCPServer. On the Server side the image aperea in a TImage, but no file is created when I save it. I tried using .jpg as well I try to use the MemoryStream.SaveToFile. I look in many forums and it looks like the do the same thing, what is wrong with this code? Here is the server code. procedure TMainForm.IdTCPServer1Execute(AContext: TIdContext); var JSON: TJSONObject; StringStream: TStringStream; MemoryStream: TMemoryStream; begin MemoryStream := nil; StringStream := nil; JSON := nil; try StringStream := TStringStream.Create('', TEncoding.ASCII); AContext.Connection.IOHandler.LargeStream := True; AContext.Connection.IOHandler.ReadStream(StringStream, SizeOf(StringStream), True); JSON := TJSONObject.ParseJSONValue(StringStream.DataString) as TJSONObject; MemoryStream := TMemoryStream.Create; IdDecoderMIME1.DecodeStream(JSON.GetValue('image_encoded').Value, MemoryStream); TThread.Synchronize(nil, procedure begin Edit2.Text := ''; Edit2.Text := JSON.GetValue('message').Value; Image1.Bitmap.LoadFromStream(MemoryStream); end); finally MemoryStream.Free; StringStream.Free; JSON.Free; end; SaveImageToDisk; end; procedure TMainForm.SaveImageToDisk; var CsFilename: String; begin CsFilename := Edit2.Text + '.png'; Image1.Bitmap.SaveToFile(CsFilename); end;
-
I was just thinking I can just delete the image after I have send it.
-
I just replaced it in the same place in the Client where the MemoryStream.LoadFromFile(FImageFilePath); was. I can save it first and then use LoadFromFile and it works, but I don't want to save them on the device. Image1.Bitmap.SaveToFile(FImageFilePath); MemoryStream.LoadFromFile(FImageFilePath);
-
Remy, I'm sorry I didn't see the sample codes you wrote, before. Maybe we where posting at the same time. On the server side in the first example I get an error: First chance exception at $00007FFCF5F3FA4C. Exception class EIdReadLnMaxLineLengthExceeded with message 'Max line length exceeded.'. Process CSIPictureServer.exe (3744) Here: JsonStr := AContext.Connection.IOHandler.ReadLn; The second example worked, thanks. I have the TImage with a image, why when I save to MemoryStream like this I just get a 19kb black image on the server side? Image1.Bitmap.SaveToStream(MemoryStream); //MemoryStream.LoadFromFile(FImageFilePath); I just get a 19kb black image.
-
Remy, It works as it is, but I changed the code in the Client StringStream := TStringStream.Create(IdEncoderMIME1.Encode(MemoryStream),TEncoding.UTF8); .. IdTCPClient1.Socket.Write(JObj.ToJSON); I change the code in the Server code: StringStream := TStringStream.Create('', TEncoding.UTF8); I tried to use ReadString, but the I get a read error when it tries to parse the json PicStr: String; .. PicStr := AContext.Connection.IOHandler.ReadString(-1); JSON := TJSONObject.ParseJSONValue(PicStr) as TJSONObject; It works fine now and I found that it did save the images, but they was not in the folder that I had in the filename string, but in the project folder where the application was running from. I take a picture with the camera and place it in a TImage. procedure TMainForm.TakePhotoFromCameraAction1DidFinishTaking(Image: TBitmap); begin Image1.Bitmap.Assign(Image); end; Then with the same code that works sending a image from a file works. MemoryStream := TMemoryStream.Create; MemoryStream.Position := 0; MemoryStream.LoadFromFile(FImageFilePath); When I try this I get a blank image. MemoryStream := TMemoryStream.Create; MemoryStream.Position := 0; Image1.Bitmap.SaveToStream(MemoryStream); Thanks for your help.
-
Remy, I do see the image in the TImage on the server application after it is send from the client app. I by accident remove the rest to 0, but still no file are saved. Here is the Client side running on Android. procedure TMainForm.SendPicture; var MemoryStream: TMemoryStream; StringStream: TStringStream; JObj: TJSONObject; begin {$IFDEF MSWINDOWS} FImageFilePath := TPath.Combine(TPath.GetDocumentsPath, EditImage.Text); {$ENDIF} {$IFDEF ANDROID} FImageFilePath := TPath.Combine(TPath.GetSharedDocumentsPath, SetupForm.EditImage.Text); {$ENDIF} if not FileExists(FImageFilePath) then begin ShowMessage('No such file at ' + FImageFilePath + '!'); Exit; end; IdTCPClient1.Host := SetupForm.EditHost.Text; try IdTCPClient1.Port := Integer.Parse(SetupForm.EditPort.Text); except on EConvertError do begin ShowMessage('Wrong port value!'); Exit; end; end; if not IdTCPClient1.Connected then begin try IdTCPClient1.Connect; except on EIdSocketError do begin ShowMessage('Connection error!'); Exit; end; end; end else begin IdTCPClient1.Disconnect; Exit; end; TTask.Run( procedure begin MemoryStream := nil; StringStream := nil; JObj := nil; try MemoryStream := TMemoryStream.Create; MemoryStream.LoadFromFile(FImageFilePath); StringStream := TStringStream.Create(IdEncoderMIME1.Encode(MemoryStream),TEncoding.ASCII); JOBJ := TJSONObject.Create; JOBJ.AddPair('image_encoded', StringStream.DataString); JOBJ.AddPair('message', Edit1.Text); IdTCPClient1.Socket.WriteLn(JObj.ToJSON); TThread.Synchronize(nil, procedure begin ShowMessage('BASE64 Image and message successfully sent to server!'); end); finally IdTCPClient1.Disconnect; JObj.Free; StringStream.Free; MemoryStream.Free; end; end); end; Right no I'm testing the send and save on the server side, but eventually I would like to send and save an image displayed in a TImage on the client side and not a file. The TImage is from the camera.
-
I create a JavaInterface file using java2op, but I have problems when I use it. I have attached the JavaInterface.pas I get this error when it execute printText. First chance exception at $8689BB93. Exception class EJNIFatal with message 'Java type recieptservice/com/recieptservice/PrinterInterface could not be found'. Process ComcaH10.apk (6409) What is missing? Tanks for any help. uses JavaInterfaces; procedure TMainForm.PrintThis; var Myprinter: JPrinterInterface; begin MyPrinter := TJPrinterInterface.Create; MyPrinter.printText(StringToJString('This Is A Test')); MyPrinter.endWork; end; JavaInterfaces.pas
-
Yes, it compiles and it prints. First It didn't print, but then I added the command Nextline(1) first and PrintText after then it printed. Now I just need to create the rest of the app. I'm working on two different apps at the same time as you can see on my posts, not getting enough sleep lately. Again, thanks for all your help.
-
Great, that did it.
-
I'm creating an application for a android device were the user takes a picture and it is store on a local network drive. After taking the picture I tried this , but it keeps saying that the path doesn't exist. I was looking around but I haven't fund any thing similar, the Google AI suggested that I use the Android Api contentResolver, but I'm unable locate that unit. procedure TMainForm.Button2Click(Sender: TObject); var PicPath: String; begin PicPath := '\\SERVER\CSPictures'; try if DirectoryExists(PicPath) then Image1.Bitmap.SaveToFile(PicPath + '\' + Edit1.Text + '.jpg') else ShowMessage('Can not find path ' + PicPath); except on E: Exception do ShowMessage('Unable to save picture to' + PicPath); end; end; Thanks for any help.
-
Ok it works, just had to open the port in the firewall.
-
Sorry, I still have problems when compiling the new JavaInterfaces.pas I get those errors all the way down from JSrPrinter_10 [JavaSignature('com/sr/SrPrinter$10')] JSrPrinter_10 = interface(JRunnable) ['{A13C6C9C-B945-4438-A33F-1B4453FD0555}'] function _Getval$line: Integer; cdecl; //function needs result type. With a red line under the $ sign procedure run; cdecl; property val$line: Integer read _Getval$line; // property 'val' does not exsist in base class. With a red line under val($line), read and $ sign in Getval$text end; TJSrPrinter_10 = class(TJavaGenericImport<JSrPrinter_10Class, JSrPrinter_10>) end; Does it looks like Java2Op created some wrong classes? Is it something i can correct manually? Again, thanks for any help. JavaInterfaces.pas
-
Thanks a lot for your help, I would never had been able to find out this by my self. I was almost going back to Android Studio and make the app there.
-
No, that is the jar file they emailed me in the SDK, with the example. I just posted a bit of the sample, here is the SDK I was emailed. The SDK is 18mb here is my dropbox link. https://www.dropbox.com/scl/fi/0nw4mhyha0qx72m4dbgfn/Java-Printer-SDK.zip?rlkey=tcn49jv2vt4ofmg90xojnzun5&dl=0
-
I have used RestDebugger a lot, but I never tried to call a php script from it. I did find this https://www.linkedin.com/pulse/sending-data-between-embarcadero-delphi-fmx-apps-using-serge-pilko-lo06f/ I will try to if I can get it to work, my first try it timed out.
-
Thanks, It worked I don't get any errors. Now I just have to find out why it's not printing. When I look at the Android example code it looks like they jus send the command printText MainActivity.this.findViewById(R.id.test2).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { try { SrPrinter.getInstance(getApplicationContext()).printText(((EditText) findViewById(R.id.test1)).getText().toString()); } catch (Exception e) { e.printStackTrace(); } } }); Here are the the first part of the SrPrinter.class package com.sr; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.graphics.Bitmap; import android.os.IBinder; import android.os.RemoteException; import java.util.Deque; import java.util.concurrent.LinkedBlockingDeque; import recieptservice.com.recieptservice.PSAMCallback; import recieptservice.com.recieptservice.PrinterInterface; import recieptservice.com.recieptservice.PrinterInterface.Stub; public class SrPrinter { static SrPrinter srPrinter; static Deque<Runnable> deque; static volatile PrinterInterface printerInterface = null; static boolean reconnect = false; static ServiceConnection serviceConnection = new ServiceConnection() { public void onServiceConnected(ComponentName componentName, IBinder iBinder) { SrPrinter.printerInterface = Stub.asInterface(iBinder); synchronized(SrPrinter.deque) { while(SrPrinter.printerInterface != null && !SrPrinter.deque.isEmpty()) { ((Runnable)SrPrinter.deque.poll()).run(); } } } public void onServiceDisconnected(ComponentName componentName) { SrPrinter.printerInterface = null; SrPrinter.reconnect = true; } }; public SrPrinter() { } public static synchronized SrPrinter getInstance(Context context) { Intent intent; if (srPrinter != null) { if (reconnect) { intent = new Intent(); intent.setClassName("recieptservice.com.recieptservice", "recieptservice.com.recieptservice.service.PrinterService"); context.startService(intent); context.bindService(intent, serviceConnection, 1); reconnect = false; } return srPrinter; } else { srPrinter = new SrPrinter(); deque = new LinkedBlockingDeque(); intent = new Intent(); intent.setClassName("recieptservice.com.recieptservice", "recieptservice.com.recieptservice.service.PrinterService"); context.startService(intent); context.bindService(intent, serviceConnection, 1); return srPrinter; } }
-
I have attached it here. printer.jar
-
Now I get exception 6 in the create line. Do I call the wrong class? procedure TMainForm.PrintThis(MyPrint:String); var Myprinter: JPrinterInterface; begin MyPrinter := TJPrinterInterface.Create; MyPrinter.beginWork; MyPrinter.printText(StringToJString('This Is A Test')); MyPrinter.endWork; end; JavaInterfaces.pas
-
Ok, I will have to install PHP on the local computer where i have Apache running, then add it in the LoadModule. I found this script in w3schools.com that i will place in the folder, I change the max size from 50000 to 2000000. <?php $target_dir = "myfolder/"; $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]); $uploadOk = 1; $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION)); // Check if image file is a actual image or fake image if(isset($_POST["submit"])) { $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]); if($check !== false) { echo "File is an image - " . $check["mime"] . "."; $uploadOk = 1; } else { echo "File is not an image."; $uploadOk = 0; } } // Check if file already exists if (file_exists($target_file)) { echo "Sorry, file already exists."; $uploadOk = 0; } // Check file size if ($_FILES["fileToUpload"]["size"] > 2000000) { echo "Sorry, your file is too large."; $uploadOk = 0; } // Allow certain file formats if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" ) { echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed."; $uploadOk = 0; } // Check if $uploadOk is set to 0 by an error if ($uploadOk == 0) { echo "Sorry, your file was not uploaded."; // if everything is ok, try to upload file } else { if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) { echo "The file ". htmlspecialchars( basename( $_FILES["fileToUpload"]["name"])). " has been uploaded."; } else { echo "Sorry, there was an error uploading your file."; } } ?> Now my question how to execute the php script from the Delphi Android app, again when I search I only see Indy examples, I don't know if Indy works on Android. Thanks.
-
I installed Apache and then in the conf/ httpd.conf then i sat the DocumentRoot "C:/myfolder" and <Directory "C:/myfolder" > , AllowOverride All. I can see the Directory from another computer using a web browser and I can open the existing files. My problem is I don't know if I'm using the right way to save the image, I have been searching, all the example is see is mostly programmers that want add other thing to the files or they are using Indy.
-
I was to quick in saying that I had it. Loading an image a display it is working. I'm doing something wrong when I save, if I add a name to the URL like url := 'http://192.168.1.150/test1234.png' I get an error that path is not found. If I create a png file in the folder with that name I get no errors, but the image is not updated. I assume I have to name the Image first and the save it, but I don't see any option in TImage to assign a name to the bitmap, the image comes from the camera. Here is my code. procedure TMainForm.SaveImageToURL(const S: String); var ms : TMemoryStream; httpCli : TNetHTTPClient; resp : IHTTPResponse; url : String; Image: TBitmap; begin httpCli := TNetHTTPClient.Create(nil); try httpCli.UserAgent := 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:44.0) Gecko/20100101 Firefox/44.0'; ms := TMemoryStream.Create(); try url := 'http://192.168.1.151/Test1234.png'; Image := TBitmap.Create; try ms.Seek(0,0); Image := Image1.Bitmap; Image.SaveToStream(ms); finally Image.Free; end; resp := httpCli.Post(url, ms); if resp.StatusCode <> 200 then Showmessage(Format('HTTP Error=%d %s', [resp.StatusCode, resp.StatusText])) finally ms.Free; end; finally httpCli.Free; end; end;
-
I got it to work with Apache. I had to add android:usesCleartextTraffic="true" in the manifest to be able to save and load with http, since the local machine don't have a SSL.
-
Yes, I will create a new java2op using the jar.
-
If I have an Apache running on the computer could I use TNethttpClient? I have another Delphi/Android app that retrieves bitmaps from a Windows Server in the cloud using TNethttpClient, I know how to do that part. I will have to find out how to create a conf file to go to a specific path.
-
I added the jar file, I did not see anything about adding the aidl in Embarcadero's website about jar files. Now I get an exception class 6 in Andoidapi.JNIBridge in class procedure TBridgeHelper.Alloc Could this be because I used the aidl file to create the JavaInterfaces.pas?