想在android中使用node.js和socket.io发送图像
I am Creating a chat app between two users now I can do Simple text chat with different users using node.js and socket.io. Now problem arises here as I have to send image in chat application and after searching for whole long day I am not able to get perfect node.js in which I can send image in chat app. So I want to know is it possible to send image using node.js. Here is my simple node.js file for sending simple text message from one user to another.
我正在两个用户之间创建一个聊天应用程序现在我可以使用node.js和socket.io与不同的用户进行简单的文本聊天。现在问题出现了,因为我必须在聊天应用程序中发送图像,在搜索了整整一天后,我无法获得完美的node.js,我可以在聊天应用程序中发送图像。所以我想知道是否可以使用node.js发送图像。这是我的简单node.js文件,用于从一个用户向另一个用户发送简单的文本消息。
socket.on('privateMessage', function(data) {
socket.get('name', function (err, name) {
if(!err) {
// get the user from list by its name to get its socket,
// then emit event privateMessage
// again here we want to make you clear
// that every single client connection has its own
// unique SOcket Object, we need to get this Socket object
// to communicate with every other client. The socket variable
// in this scope is the client who wants to send the private
// message but the socket of the receiver is not know.
// Get it from the saved list when connectMe handlers gets called
// by each user.
onLine[data.to].emit('newPrivateMessage',{from:name, msg:data.msg, type:'Private Msg'})
}
});
});
2 个解决方案
#1
2
You can use the Base64 version of your image and send it like this:
您可以使用图像的Base64版本并将其发送如下:
onLine[data.to].emit('newPrivateMessage',{from:name, img:data.img.toString('base64'), type:'Private Msg'})
.. and then on the client side receive it and create an image
..然后在客户端接收它并创建一个图像
socket.on("newPrivateMessage", function(data) {
if (data.img) {
var img = new Image();
img.src = 'data:image/jpeg;base64,' + data.img;
// Do whatever you want with your image.
}
});
UPDATE
The following is a snippet taken from the link I've commented below. As you can see it takes the image from the input
, read
s it and sends to the server. After that you can send the same data from the server to another client.
以下是从我在下面评论过的链接中获取的片段。如您所见,它从输入中获取图像,读取并发送到服务器。之后,您可以将相同的数据从服务器发送到另一个客户端。
For the full example, please read the article.
有关完整示例,请阅读文章。
JavaScript (client)
...
$('#imageFile').on('change', function(e) {
var file = e.originalEvent.target.files[0],
reader = new FileReader();
reader.onload = function(evt) {
var jsonObject = {
'imageData': evt.target.result
}
// send a custom socket message to server
socket.emit('user image', jsonObject);
};
reader.readAsDataURL(file);
});
...
HTML
...
Image file: <input type="file" id="imageFile" /><br/>
...
UPDATE 2
Here is one example I have found:
这是我发现的一个例子:
Java (client)
File file = new File("path/to/the/image");
try {
FileInputStream imageInFile = new FileInputStream(file);
byte imageData[] = new byte[(int) file.length()];
imageInFile.read(imageData);
// Converting Image byte array into Base64 String
String imageDataString = Base64.encodeBase64URLSafeString(imageData);
} catch (...) {
...
}
The above snippet shows how to read the file and encode the data into a base64
string. So then you can send it just like a string (I assume).
上面的代码段显示了如何读取文件并将数据编码为base64字符串。那么你可以像字符串一样发送它(我假设)。
Here is the complete example: How to convert Image to String and String to Image in Java?
这是完整的示例:如何在Java中将Image转换为String和String转换为Image?
Also I have found encodeToString
function of Base64.Encoder
(java.util
package), which you can use.
我也找到了Base64.Encoder(java.util包)的encodeToString函数,你可以使用它。
更多相关文章
- Android Picasso Dropbox:如何将图像加载到gridview中
- 如何将本机应用程序(android)与phonegap应用程序集成
- OpenCV4Android中图像预览旋转90度的问题
- Android图形图像处理之Bitmap和BitmapFactory
- 为什么我的Android应用程序偶尔可以非常快地耗尽电池?
- 使用android的加速度计移动图像
- 如何在android 5.0(L)中运行应用程序活动名称?
- Android应用程序启动过程上
- Java Android套接字连接。错误的IP地址导致应用程序停止响应