乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      XMPP實現(xiàn)群聊截圖(spark+openfire)

       WindySky 2016-02-18

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

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

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

       

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

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

      在群聊接收消息的GroupChatRoom的handleMessagePacket方法進行修改,創(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)碼的具體實現(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ù)組來生成圖片,實現(xiàn)群聊截圖功能。


        本站是提供個人知識管理的網(wǎng)絡存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
        轉(zhuǎn)藏 分享 獻花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多