How to send image from Java app to Android app?

I have a client server application. Server is pure Java, client is an Android application. They communicate over TCP using ServerSocket.

I want to create an image on the server, edit it (for example, draw lines on it), and then send it to the client to display it there as a bitmap on the screen.

My first approach was to import the android.graphics.Bitmap library into the server in order to use it there, but, of course, since you cannot use the Android libraries outside of the Android environment, this will not work.

Now my approach is to use BufferedImage on the server, but the problem is that I could not find out how to serialize this so that the client could then recover the Bitmap. I tried using ImageIO.write, but then what do I need to do on the client side?

Does anyone have an idea on how to solve this problem, or do you have a better approach? I appreciate your help.

+4
source share
2 answers

Any PNG or JPG image can be decoded in Android using the BitmapFactory class. Get the InputStream for the image, then use BitmapFactory.decodeStream(inputStream) . It does not need to be serialized on purpose ... just send bytes for the image.

+1
source

There are two ways to get the raw byte stream in an Android client application.

  • Using getPixels Function
  • Using the copyPixelsToBuffer Function

getPixels is simple and gives you basic raw bytes. copyPixelsToBuffer copies to the buffer.

Once you have the raw bytes, send them to your server. Here, on the server side of Java, you can use a library such as ImageJ to convert the original byte stream to an image, process it, convert back and send back to Android.

When your client application receives raw_stream back, convert it again to android.graphics.Bitmap using the setPixels or copyPixelsFromBuffer functions.

0
source

Source: https://habr.com/ru/post/1384755/


All Articles