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数组来生成图片,实现群聊截图功能。