免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
XMPP實(shí)現(xiàn)群聊截圖(spark+openfire)

spark默認(rèn)的單聊截圖模式是利用文件來來進(jìn)行傳遞,調(diào)用SparkTransferManager.getInstance().sendFile(img.getTmpFile(), getParticipantJID());

調(diào)用    final OutgoingFileTransfer transfer = transferManager
                .createOutgoingFileTransfer(fullJID);

通過    transfer.sendFile(file, "Sending file");來進(jìn)行發(fā)送。

 

spark的群聊(臨時(shí)會(huì)議基礎(chǔ)上進(jìn)行改造)卻不能使用這種模式來進(jìn)行文件傳遞,缺少了文件傳遞的JID。由此,想出一種簡單的方式來通過xmpp來進(jìn)行傳遞。

思路很簡單:截圖后的圖片保存到本地,插入到聊天顯示框,將圖片image轉(zhuǎn)為byte數(shù)組,再轉(zhuǎn)為hex存儲(chǔ)到String中(自定義標(biāo)簽,如<img>來將轉(zhuǎn)碼后的內(nèi)容保存,方便接受時(shí)候截取),利用Message傳遞時(shí)setBody(“轉(zhuǎn)碼后的字符串”)。

在群聊接收消息的GroupChatRoom的handleMessagePacket方法進(jìn)行修改,創(chuàng)建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");//本地創(chuàng)建截圖  
  14.                     File f = new File(rootPath.getAbsolutePath(), tmpPath);  
  15.                   
  16.                     if(f.exists()){  
  17.                         try {  
  18.                             imageByte=image2String(f);//得到轉(zhuǎn)碼后的字符串  
  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.     }  

 轉(zhuǎn)碼的具體實(shí)現(xiàn):

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數(shù)組來生成圖片,實(shí)現(xiàn)群聊截圖功能。


本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊舉報(bào)。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
string類的使用方法
C#抓取網(wǎng)頁數(shù)據(jù)分析
用Servlvet實(shí)現(xiàn)文件上傳的功能
java根據(jù)地址從百度API獲取經(jīng)緯度
java字符串?dāng)?shù)學(xué)公式運(yùn)算
工具類生成mybatis的Mapper類和xml文件以及實(shí)體
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服