spark默认的单聊截图模式是利用文件来来进行传递,调用SparkTransferManager.getInstance().sendFile(img.getTmpFile(), getParticipantJID());
调用 final OutgoingFileTransfer transfer = transferManager
.createOutgoingFileTransfer(fullJID);
通过 transfer.sendFile(file, "Sending file");来进行发送。
spark的群聊(临时会议基础上进行改造)却不能使用这种模式来进行文件传递,缺少了文件传递的JID。由此,想出一种简单的方式来通过xmpp来进行传递。
思路很简单:截图后的图片保存到本地,插入到聊天显示框,将图片image转为byte数组,再转为hex存储到String中(自定义标签,如<img>来将转码后的内容保存,方便接受时候截取),利用Message传递时setBody(“转码后的字符串”)。
在群聊接收消息的GroupChatRoom的handleMessagePacket方法进行修改,创建BufferedImag并利用ImageIo将图片写入到指定文件中,具体代码如下:
public void sendMessage() {
String text = getChatInputEditor().getText();
StringBuffer sb = new StringBuffer();
String imageByte=null;
final StringTokenizer tokenizer = new StringTokenizer(text, " \n \t", true);
while (tokenizer.hasMoreTokens()) {
String textFound = tokenizer.nextToken();
if(textFound.startsWith("Tmp://")) {
String tmpPath = textFound.substring(textFound.indexOf("Tmp://") + 6, textFound.indexOf("#"));
Log.debug("screen shot file " + tmpPath + "created.");
//CHECK IF SEND BY ME, JUST INSERT EXISTED ICON IF TRUE
File rootPath = new File(Spark.getSparkUserHome(), "/tempImages");//本地创建截图
File f = new File(rootPath.getAbsolutePath(), tmpPath);
if(f.exists()){
try {
imageByte=image2String(f);//得到转码后的字符串
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// String s = new String (imageByte);
sb.append("<img>");
sb.append(imageByte);
sb.append("</img>");
}
// RevImage image = new RevImage();
// insertComponent(image);
}
}
sendMessage(text+sb.toString());
}
转码的具体实现:
public static String image2String(File f) throws Exception {
FileInputStream fis = new FileInputStream( f );
byte[] bytes = new byte[fis.available()];
fis.read(bytes);
fis.close();
// 生成字符串
String imgStr = byte2hex( bytes );
return imgStr;
}
private static String byte2hex(byte[] b) {
StringBuffer sb = new StringBuffer();
String stmp = "";
for (int n = 0; n < b.length; n++) {
stmp = Integer.toHexString(b[n] & 0XFF);
if (stmp.length() == 1){
sb.append("0" + stmp);
}else{
sb.append(stmp);
}
}
return sb.toString();
}
收到消息后的处理:
if(message.getBody().contains("Tmp://")&&message.getBody().contains("<img>")&&message.getBody().contains("</img>")){
final StringTokenizer tokenizer = new StringTokenizer(message.getBody(), " \n \t", true);
byte[] imgbyte=null;
ImageIcon icon=null;
File f=null;
while (tokenizer.hasMoreTokens()) {
String textFound = tokenizer.nextToken();
if(textFound.startsWith("Tmp://")) {
String tmpPath = textFound.substring(textFound.indexOf("Tmp://") + 6, textFound.indexOf("#"));
Log.debug("screen shot file " + tmpPath + "created.");
//CHECK IF SEND BY ME, JUST INSERT EXISTED ICON IF TRUE
File rootPath = new File(Spark.getSparkUserHome(), "/tempImages");
f = new File(rootPath.getAbsolutePath(), tmpPath);
if(!f.exists()){
try {
f.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
if(textFound.contains("<img>")&&textFound.contains("</img>")){
imgbyte = hex2byte(textFound);
byte[] bytes =imgbyte;
if (bytes != null && bytes.length > 0) {
icon = new ImageIcon(bytes);
}
Image image =icon.getImage();
BufferedImage bufImg = new BufferedImage(image.getWidth(null), image.getHeight(null),BufferedImage.TYPE_INT_RGB);
Graphics g = bufImg .createGraphics();
g.drawImage(image, 0, 0, null);
g.dispose();
try {
ImageIO.write(bufImg, "PNG", new FileOutputStream(f));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
getTranscriptWindow().insertMessage(from, message,
getColor(from),
getMessageBackground(from, message.getBody()));
解码代码:
private byte[] hex2byte(String textFound) {
int start = textFound.indexOf("<img>")+5;
int end = textFound.indexOf("</img>");
String str = textFound.substring(start, end);
if (str == null)
return null;
str = str.trim();
int len = str.length();
if (len == 0 || len % 2 == 1)
return null;
byte[] b = new byte[len / 2];
try {
for (int i = 0; i < str.length(); i += 2) {
b[i / 2] = (byte) Integer.decode("0X" + str.substring(i, i + 2)).intValue();
}
return b;
} catch (Exception e) {
return null;
}
}
这样,通过byte数组来生成图片,实现群聊截图功能。
- 大小: 62.2 KB
分享到:
相关推荐
XMPP+Openfire4.5.1+Smack4.3.4+MySql,支持手机对手机,手机对PC(Spark)的消息收发
xmpp+android+openfire+asmack即时通讯客户端聊天 上网下载个openfire服务器安装后即可使用
xmpp+android+openfire+asmack视频通信 上网下载个openfire服务器安装后即可使用
Openfire+spark+smack xmpp
2、com.qq.util包中,Const类,修改XMPP_HOST内容(就是openfire服务器名称) 3、本人保证,可以使用。请安心使用,运行成功的时候千万别激动…… 4、目前存在的BUG,不能及时刷新新收到的信息,需要退出聊天界面后...
Android XMPP例子(Openfire+asmack+spark)
在 XmppConnection 文件 改openfire 服务器地址 就ok, 2. 只能2台手机 安卓和安卓之间通讯,不能和电脑spark,ios等 。3.发送语音信息 显示 语音信息 点一下可以播放, 这个文字可以改个图片 或者什么符号代替。 有...
基于xmpp+asmack+openfire的安卓客户端,界面仿QQ,实现好友添加,消息收发(支持文本,动态表情,图片,及位置类型) 原来的版本Android studio3无法编译通过。修改了一些配置和图片资源 原作者博客地址: ...
源码,实现多人在线聊天 私聊,建房分组聊 ,注册,登陆
先说一下为什么要写这篇博客,是因为本人在周末在研究XMPP和OpenFire,从网上下载了个Demo,但跑不起来,花了很长时间,经改造后,跑起来了,写个篇博文也是希望后边学习XMPP和OpenFire的同学下载后直接运行,少走...
基于Smack3.0.4+ Openfire3.10.2开发之Android 客户端之三(用户登入、用户注册与XMPP 服务器交互)。
基于Qt+opencv开发的桌面应用+xmpp+Mqtt即时通讯+图像处理+vtk3D点云显示+WebEngine展示三维页面,适合毕业设计、课程设计、项目开发。项目源码已经过严格测试,可以放心参考并在此基础上延申使用~ 基于Qt+opencv...
基于Smack3.0.4+ Openfire3.10.2开发之Android 客户端之四(消息会话、联系人、设置与XMPP 服务器交互)
android 基于openfire服务器搭建的xmpp即时聊天客户端 android+asmack+openfire+xmpp
openfire+spark+smack工具,针对即时消息模拟以及xmpp协议测试有很大的帮助
Android 基于xmpp协议,smack包,openfire服务端的高仿QQ的即时通讯实现.zip
openfire xmpp spark基本学习 openfire xmpp spark基本学习
Android 基于xmpp协议,smack包,openfire服务端的高仿QQ的即时通讯实现。实现了注册,登录,读取好友列表,搜索好友,添加分组,添加好友,删除好友,修改心情,两个客户端之间的信息发送与接收,监听发送来的好友...
Openfire服务器 + Android客户端 ( Smack 4.19 ) 组成 功能:注册账号、登录和退出系统、获取好友列表、添加和删除好友、收发文本消息、分享位置(基于百度地图SDK) 说明:本源码来自董相志老师所著的《Java 网络...
XMPP协议及其服务器端的Openfire插件开发XMPP背景知识XMPP是一种基于XML的协议,它继承了在XML环境中灵活的发展性。因此,基于XMPP的应用具有超强的可扩展性。绊过扩展以后的XMPP可以通过发送扩展的信息来处理用户的...