简体   繁体   English

如何将图像从Java应用程序发送到Android应用程序?

[英]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应用程序上获取原始字节流。

  1. Using getPixels function 使用getPixels函数
  2. Using copyPixelsToBuffer function 使用copyPixelsToBuffer函数

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.

相关问题 如何通过彩信从android应用发送图片? - How can I send a picture from android application via mms? 如何在Android应用程序中显示图像 - How can I display image in Android Application 如何将数据从Android发送到服务器上的Java应用程序 - How to send data from android to java application on server 如何将数据从Android应用程序发送到Java Server(Apache tomcat)? - How to send data from Android application to Java Server(Apache tomcat)? 如何将简单的消息从网页(HTML / Javascript)发送到Java应用程序? - How can I send simple messages from a webpage (HTML/Javascript) to a Java application? 如何使用HTTP POST从Java应用程序发送XML并在服务器上使用PHP打印它? - How can I send XML from a Java application and print it with PHP on the server, using HTTP POST? 如何登录Facebook并获取用户信息以从Android应用程序发送我的远程数据库 - How can I login with facebook and get user info to send my remote db from android application 如何将通知从Parse发送到Java应用程序 - How do I send a notification from Parse to a Java application 通过蓝牙将对象从Java应用程序发送到Android应用程序? - Send an object from a java application to an Android application via Bluetooth? 如何从一个 android 应用程序向其他 android 应用程序发送通知? - How to send notification from one android application to other android application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM