[英]How can I send an Image from a Java application to an Android application?
I have a client-server application. 我有一个客户端 - 服务器应用程序。 The server is pure Java, the client is an Android application. 服务器是纯Java,客户端是Android应用程序。 They communicate over TCP using a ServerSocket. 它们使用ServerSocket通过TCP进行通信。
I want to create an Image on the server, edit it (eg draw lines on it) and then send it to the client to display it there as a Bitmap on the screen. 我想在服务器上创建一个Image,编辑它(例如在它上画线),然后将它发送到客户端,在屏幕上显示Bitmap。
My first approach was to import the android.graphics.Bitmap library into the server to use it there, but of course since you can't use Android libraries outside of an Android environment this can't work. 我的第一种方法是将android.graphics.Bitmap库导入服务器以在那里使用它,但当然因为你不能在Android环境之外使用Android库,所以这是行不通的。
My approach now is to use a BufferedImage on the server, but the problem is that I couldn't find out how to serialize this in a way that the client can then reconstruct a Bitmap out of it. 我现在的方法是在服务器上使用BufferedImage,但问题是我无法找到如何以客户端可以从中重构Bitmap的方式对其进行序列化。 I tried using ImageIO.write, but then what do I have to do on the client-side? 我尝试使用ImageIO.write,但那么我在客户端需要做什么?
Does anyone have an idea on how to solve this, or has a better approach? 有没有人知道如何解决这个问题,或者有更好的方法? I appreciate your help. 我感谢您的帮助。
Any PNG or JPG image can be decoded in Android using the BitmapFactory
class. 可以使用BitmapFactory
类在Android中解码任何PNG或JPG图像。 Get an InputStream
for the image then use BitmapFactory.decodeStream(inputStream)
. 获取图像的InputStream
然后使用BitmapFactory.decodeStream(inputStream)
。 It doesn't need to be serialized specially...just send the bytes for the image. 它不需要特别序列化......只需发送图像的字节。
There are two ways to get raw stream of bytes on your client Android application. 有两种方法可以在客户端Android应用程序上获取原始字节流。
getPixels is straightforward and gives you basic raw bytes. getPixels非常简单,并为您提供基本的原始字节。 copyPixelsToBuffer copies in a buffer. copyPixelsToBuffer在缓冲区中复制。
Once you have the raw bytes, send them to your server. 获得原始字节后,将它们发送到您的服务器。 Here, on the server side Java, you can use a library such as ImageJ to convert back your raw byte stream into Image, process it, convert it back and send back to Android. 这里,在服务器端Java,您可以使用ImageJ等库将原始字节流转换回Image,处理它,将其转换回并发送回Android。
When your client application receives back the raw_stream, convert it again into android.graphics.Bitmap by using the setPixels or copyPixelsFromBuffer function. 当您的客户端应用程序收到raw_stream时,使用setPixels或copyPixelsFromBuffer函数将其再次转换为android.graphics.Bitmap。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.