UWP convert Image to string - image

I am trying to send an Image object via bluetooth using the
BluetoothRfcommChat sample
my idea is to convert the image to string before sending and converting it back when received.
my question is how to convert an Image to String
string message;
//here should go the conversion
//message=myimg;
writer.WriteUInt32((uint)message.Length);
writer.WriteString(message);
ConversationListBox.Items.Add(myimg);
await writer.StoreAsync();
or which will be the "Right" way to do this

You should not do it because tring are null terminated. Any 0 in data indicates end of string.
You should send it as raw bytes.
To send image, music or any other file (object) over Bluetooth special protocol was developed. It is called OBEX and ObjectPushProfile is designed to send such things. This is what the right way to send files.

Related

how to send email attachments using power automate to webapi

I am new to power automate. I need to read my email attachments and send each attachment to webapi as base64. I used below expression to convert email attachment to base64. but flow shows error as "Correct to include a valid reference to 'Get_Attachment_(V3)' for the input parameter(s) of action 'HTTP'.
what should be correct expression to convert attachment to base64.
expression: base64(body('Get_Attachment_(V3)')?['contentBytes'])
error:
A couple of things.
Firstly, in your JSON body, you just need to put quotes around the value of the content property.
Secondly, make sure you have a Get Attachment (V#) step prior to the HTTP action.
This step actually retrieves the contents of the attachment.

Why is message content JSON encoded in pubsub?

I'm currently working on implementing a Pub/Sub pattern for a project I'm working on and I was wondering why the content was JSON encoded string when the rest of the message was in plain JSON.
I couldn't find any documentation on this topic, generic documentation on the Pub/Sub pattern is not really abundant, or I didn't use the good search terms.
If content was not encoded separately like that, then the server would need to be aware of the contents of the message in order to deserialize. With this approach, it can simply treat the content as a string and forward that encoded string along to subscribers.

Send base64 string as image to the client

I managed to save an image as a base64 encoded string in my database, but I was wondering how tu serve the base64 string in a way that it is interpreted as an image on the client side.
i made a WS that returns the string like this :
return ok(myBase64String).as("image/jpeg")
but the image cannot be displayed in the browser.
if I decode the string and then send the byte array the image is displayed, now what i dont understand is why do I have to decode my already encoded image in order to display it in the client??
byte [] test = Base64.decodeBase64(event.getPhoto());
return ok(test).as("image/jpeg");
this works but why do I have to decode my base 64 string??
anyone have an idea?
thanks!!
If you want to use Base64, you need to use the data uri scheme:
<img src="data:image/png;<base64>" />
Otherwise you have to pass in a path to a binary representation. That's what the Assets controller does, serving a file as binary.

Unable to open image after base64 decode at server-side

I am developing an cross-platform mobile app using Titanium Appcelerator. This app is based on Sakai, in this application i have to send image to the server.
Client side i am encoding the image with base64 encoding technique using Titanium API.
//Client-Side JavaScript Code
var selectedImageB64 = Ti.Utils.base64encode(selectedImage).toString();
Ti.Utils.base64encode API
and now I am sending this string to server and there I am decoding it,
//Server-Side Java Code
byte[] photoData = Base64.decode(selectedImageB64);
byte[] content = photoData;
Base64 API
now the decoded data (content) is passed to the appropriate method to save the image into the database. Till here everything is working well. Image is successfully stored in the database. The size of the original image and the image stored in the database are of equal, so that I thought this encoding and decoding process is done successfully. But when I am trying to open the image in the database the image viewer displays an error message saying "Windows Photo Viewer can't open this picture because either Photo Viewer doesn't support this file format, or you don't have the latest updates
to Photo Viewer.". So what I have to do now. What exactly is the problem?
Regards..
please try the following code:
var selectedImage=image.getImage();
var selectedImageB64=Ti.Utils.base64encode(selectedImage).getText()
works for me.
For mobile side :
var base64String = Ti.Utils.base64encode(imageView.toImage()).getText()
send base64String to server.
For Server Side :
String tempPic = (String)jsonMap.get("base64String");
byte pic[] = Base64.decodeBase64(tempPic.getBytes());
Now, Play with pic[] byte array. This code work for me.
A couple things to check:
1) Save the bytes from the server to the file system instead just to eliminate a variable (namely the db)
2) Actually print out the numeric value of say the first 10 bytes on the server side, and do the same on the client side. This is to make sure the base64 encode/decode functions are implemented correctly (or they are following the same standard).
3) I don't think you need to the toString after the base64encode, you might to a Ti.Api.Info on the object before and after to the toString
4) I would like to know more about what selectedImage object is, if its a blob object in titanium it may not be the image directly, but rather a wrapper around the image (So you maybe encoding the wrong data).
I found the solution for this, actually when sending the base64 encoded data from client '+' symbols in the encoded data are being replaced by a space. So i tried replacing space with '+' sign on server side. That solves this issue.

Extracting data from net ldap response containing image

In my application I sync users image uploaded as photo field in the LDAP, I am using NET::LDAP for the same.
the object returned for the image field is of type Net::BER::BerIdentifiedArray, I can convert it to Net::BER.
My question is how do I extract the type of image(jpeg/gif/bmp etc) while creating a image file from the binary response given by the LDAP.
You'll need something to decode the binary data into an image. RMagick is a Ruby wrapper for ImageMagick that should do the trick. Specifically, Image.from_blob will read image data from a string in memory.
From there, this answer shows image.format will tell you the format.

Resources