打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
XMPP实现群聊截图(spark+openfire)
userphoto

2017.10.26 广东

关注

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将图片写入到指定文件中,具体代码如下:

Groupchatroom sendmessage代码  
  1. public void sendMessage() {  
  2.         String text = getChatInputEditor().getText();  
  3.         StringBuffer sb = new StringBuffer();  
  4.         String  imageByte=null;  
  5.            final StringTokenizer tokenizer = new StringTokenizer(text, " \n \t", true);  
  6.             while (tokenizer.hasMoreTokens()) {  
  7.                 String textFound = tokenizer.nextToken();  
  8.                 if(textFound.startsWith("Tmp://")) {  
  9.                     String tmpPath = textFound.substring(textFound.indexOf("Tmp://") + 6, textFound.indexOf("#"));  
  10.                     Log.debug("screen shot file " + tmpPath + "created.");  
  11.   
  12.                     //CHECK IF SEND BY ME, JUST INSERT EXISTED ICON IF TRUE  
  13.                     File rootPath =  new File(Spark.getSparkUserHome(), "/tempImages");//本地创建截图  
  14.                     File f = new File(rootPath.getAbsolutePath(), tmpPath);  
  15.                   
  16.                     if(f.exists()){  
  17.                         try {  
  18.                             imageByte=image2String(f);//得到转码后的字符串  
  19.                         } catch (Exception e) {  
  20.                             // TODO Auto-generated catch block  
  21.                             e.printStackTrace();  
  22.                         }  
  23. //                    String s = new String (imageByte);  
  24.                         sb.append("<img>");  
  25.                         sb.append(imageByte);  
  26.                         sb.append("</img>");  
  27.                     }  
  28. //                  RevImage image = new RevImage();  
  29. //                  insertComponent(image);  
  30.                 }  
  31.             }  
  32.             sendMessage(text+sb.toString());  
  33.           
  34.     }  

 转码的具体实现:

Java代码  
  1. public static  String image2String(File f) throws Exception {  
  2.     FileInputStream fis = new FileInputStream( f );  
  3.     byte[] bytes = new byte[fis.available()];  
  4.     fis.read(bytes);  
  5.     fis.close();  
  6.       
  7.     // 生成字符串  
  8.     String imgStr = byte2hex( bytes );  
  9.     return imgStr;  
  10.   
  11. }  
  12.   
  13. private static String byte2hex(byte[] b) {  
  14.       StringBuffer sb = new StringBuffer();  
  15.        String stmp = "";  
  16.        for (int n = 0; n < b.length; n++) {  
  17.         stmp = Integer.toHexString(b[n] & 0XFF);  
  18.         if (stmp.length() == 1){  
  19.             sb.append("0" + stmp);  
  20.         }else{  
  21.             sb.append(stmp);  
  22.         }  
  23.           
  24.        }  
  25.        return sb.toString();  
  26. }  

 收到消息后的处理:

Java代码  
  1. if(message.getBody().contains("Tmp://")&&message.getBody().contains("<img>")&&message.getBody().contains("</img>")){  
  2.                         final StringTokenizer tokenizer = new StringTokenizer(message.getBody(), " \n \t", true);  
  3.                          byte[] imgbyte=null;  
  4.                            ImageIcon icon=null;  
  5.                            File f=null;  
  6.                         while (tokenizer.hasMoreTokens()) {  
  7.                             String textFound = tokenizer.nextToken();  
  8.                                if(textFound.startsWith("Tmp://")) {  
  9.                                     String tmpPath = textFound.substring(textFound.indexOf("Tmp://") + 6, textFound.indexOf("#"));  
  10.                                     Log.debug("screen shot file " + tmpPath + "created.");  
  11.   
  12.                                     //CHECK IF SEND BY ME, JUST INSERT EXISTED ICON IF TRUE  
  13.                                     File rootPath =  new File(Spark.getSparkUserHome(), "/tempImages");  
  14.                                    f = new File(rootPath.getAbsolutePath(), tmpPath);  
  15.                                     if(!f.exists()){  
  16.                                      try {  
  17.                                         f.createNewFile();  
  18.                                     } catch (IOException e) {  
  19.                                         // TODO Auto-generated catch block  
  20.                                         e.printStackTrace();  
  21.                                     }  
  22.                                     }  
  23.                         }  
  24.                                if(textFound.contains("<img>")&&textFound.contains("</img>")){  
  25.                                     imgbyte = hex2byte(textFound);  
  26.                                     byte[] bytes =imgbyte;  
  27.                                     if (bytes != null && bytes.length > 0) {  
  28.                                        icon = new ImageIcon(bytes);  
  29.                                     }  
  30.                                     Image image =icon.getImage();  
  31.                                     BufferedImage bufImg = new BufferedImage(image.getWidth(null), image.getHeight(null),BufferedImage.TYPE_INT_RGB);    
  32.                                     Graphics g = bufImg .createGraphics();    
  33.                                     g.drawImage(image, 0, 0, null);    
  34.                                     g.dispose();  
  35.   
  36.                                     try {  
  37.                                         ImageIO.write(bufImg, "PNG", new FileOutputStream(f));  
  38.                                     } catch (IOException e) {  
  39.                                         // TODO Auto-generated catch block  
  40.                                         e.printStackTrace();  
  41.                                     }  
  42.                                 }  
  43.                         }  
  44.                         getTranscriptWindow().insertMessage(from, message,  
  45.                                 getColor(from),  
  46.                                 getMessageBackground(from, message.getBody()));  

 解码代码:

Java代码  
  1. private byte[] hex2byte(String textFound) {  
  2.     int start = textFound.indexOf("<img>")+5;  
  3.     int end = textFound.indexOf("</img>");  
  4.     String str = textFound.substring(start, end);  
  5.       if (str == null)  
  6.              return null;  
  7.             str = str.trim();  
  8.             int len = str.length();  
  9.             if (len == 0 || len % 2 == 1)  
  10.              return null;  
  11.             byte[] b = new byte[len / 2];  
  12.             try {  
  13.              for (int i = 0; i < str.length(); i += 2) {  
  14.               b[i / 2] = (byte) Integer.decode("0X" + str.substring(i, i + 2)).intValue();  
  15.              }  
  16.              return b;  
  17.             } catch (Exception e) {  
  18.              return null;  
  19.             }  

 这样,通过byte数组来生成图片,实现群聊截图功能。


本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
string类的使用方法
C#抓取网页数据分析
用Servlvet实现文件上传的功能
java根据地址从百度API获取经纬度
java字符串数学公式运算
工具类生成mybatis的Mapper类和xml文件以及实体
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服