emoji 表情图片解决方法

1:先弄到图片 上传到服务器上 



 

2.下载 unicode 和 表情 映射关系xml 
   emoji4unicode.xml  在下面的rar 中 

3.加入这个一个 转换类 
Java代码   收藏代码
  1. package com.fanbaobao.util;  
  2.   
  3. import java.io.InputStream;  
  4. import java.util.ArrayList;  
  5. import java.util.HashMap;  
  6. import java.util.List;  
  7. import java.util.Map;  
  8.   
  9. import javax.xml.namespace.QName;  
  10. import javax.xml.stream.XMLEventReader;  
  11. import javax.xml.stream.XMLInputFactory;  
  12. import javax.xml.stream.XMLStreamException;  
  13. import javax.xml.stream.events.Attribute;  
  14. import javax.xml.stream.events.StartElement;  
  15. import javax.xml.stream.events.XMLEvent;  
  16.   
  17. public class EmojiConverter {  
  18.   
  19.     private EmojiConverter() {  
  20.     }  
  21.   
  22.     enum Type {  
  23.         UNICODE, SOFTBANK  
  24.     };  
  25.   
  26.     private Map<List<Integer>, String> convertMap;  
  27.   
  28.     public static class Builder {  
  29.   
  30.         private Type from;  
  31.         private Type to;  
  32.   
  33.         public Builder from(Type type) {  
  34.             this.from = type;  
  35.             return this;  
  36.         }  
  37.   
  38.         public Builder to(Type type) {  
  39.             this.to = type;  
  40.             return this;  
  41.         }  
  42.   
  43.         public EmojiConverter build() {  
  44.             EmojiConverter converter = new EmojiConverter();  
  45.             readMap(converter);  
  46.             return converter;  
  47.         }  
  48.   
  49.         private static final String TRIM_PATTERN = "[^0-9A-F]*";  
  50.   
  51.         public void readMap(EmojiConverter converter) {  
  52.             Map<List<Integer>, String> result = new HashMap<List<Integer>, String>();  
  53.             converter.convertMap = result;  
  54.   
  55.             XMLEventReader reader = null;  
  56.             try {  
  57.   
  58.                 XMLInputFactory factory = XMLInputFactory.newInstance();  
  59.   
  60.                 InputStream stream = EmojiConverter.class.getClassLoader()  
  61.                         .getResourceAsStream("emoji4unicode.xml");  
  62.                 reader = factory.createXMLEventReader(stream);  
  63.   
  64.                 while (reader.hasNext()) {  
  65.                     XMLEvent event = reader.nextEvent();  
  66.   
  67.                     if (event.isStartElement()) {  
  68.                         StartElement element = (StartElement) event;  
  69.                         if (element.getName().getLocalPart().equals("e")) {  
  70.   
  71.                             Attribute fromAttr = element  
  72.                                     .getAttributeByName(new QName(from.toString().toLowerCase()));  
  73.                             Attribute toAttr = element  
  74.                                     .getAttributeByName(new QName(to.toString()  
  75.                                             .toLowerCase()));  
  76.                             if (fromAttr == null) {  
  77.                                 continue;  
  78.                             }  
  79.                             List<Integer> fromCodePoints = new ArrayList<Integer>();  
  80.                             String from = fromAttr.getValue();  
  81.                             if (from.length() > 6) {  
  82.                                 String[] froms = from.split("\\+");  
  83.                                 for (String part : froms) {  
  84.                                     fromCodePoints.add(Integer.parseInt(  
  85.                                             part.replaceAll(TRIM_PATTERN, ""),  
  86.                                             16));  
  87.                                 }  
  88.                             } else {  
  89.                                 fromCodePoints.add(Integer.parseInt(  
  90.                                         from.replaceAll(TRIM_PATTERN, ""), 16));  
  91.                             }  
  92.                             if (toAttr == null) {  
  93.                                 result.put(fromCodePoints, null);  
  94.                             } else {  
  95.                                 String to = toAttr.getValue();  
  96.                                 StringBuilder toBuilder = new StringBuilder();  
  97.                                 if (to.length() > 6) {  
  98.                                     String[] tos = to.split("\\+");  
  99.                                     for (String part : tos) {  
  100.                                         toBuilder.append(Character  
  101.                                                 .toChars(Integer.parseInt(part  
  102.                                                         .replaceAll(  
  103.                                                                 TRIM_PATTERN,  
  104.                                                                 ""), 16)));  
  105.                                     }  
  106.                                 } else {  
  107.                                     toBuilder.append(Character.toChars(Integer  
  108.                                             .parseInt(to.replaceAll(  
  109.                                                     TRIM_PATTERN, ""), 16)));  
  110.                                 }  
  111.                                 result.put(fromCodePoints, toBuilder.toString());  
  112.                             }  
  113.   
  114.                         }  
  115.                     }  
  116.                 }  
  117.   
  118.                 reader.close();  
  119.             } catch (Exception e) {  
  120.                 e.printStackTrace();  
  121.   
  122.             } finally {  
  123.                 if (reader != null) {  
  124.                     try {  
  125.                         reader.close();  
  126.                     } catch (XMLStreamException e) {  
  127.   
  128.                     }  
  129.                 }  
  130.   
  131.             }  
  132.   
  133.         }  
  134.   
  135.     }  
  136.   
  137.     public String convert(int width,int height,String imgpath,String input) {  
  138.         StringBuilder result = new StringBuilder();  
  139.         int[]codePoints = toCodePointArray(input);  
  140.         for(int i = 0; i < codePoints.length; i++){  
  141.             List<Integer> key2 = null;  
  142.             if(i + 1 < codePoints.length){  
  143.                 key2 = new ArrayList<Integer>();  
  144.                 key2.add(codePoints[i]);  
  145.                 key2.add(codePoints[i + 1]);  
  146.                   
  147.                 if(convertMap.containsKey(key2) || 65039==codePoints[i + 1]){  //处理 iphone5 xxxx-fe0f.png  
  148.                     String aa=Integer.toHexString(codePoints[i])+"-"+Integer.toHexString(codePoints[i+1]);  
  149.                     String value = convertMap.get(key2);  
  150.                     if(value != null || 65039==codePoints[i + 1]){  
  151.                         result.append("<img width=\""+width+"px\" height=\""+height+"px\" style=\"vertical-align: bottom;\" src=\""+imgpath+"");  
  152.                         result.append(aa);  
  153.                         result.append(".png\"/>");  
  154.                     }  
  155.                     i++;  
  156.                     continue;  
  157.                 }  
  158.             }  
  159.               
  160.             List<Integer> key1 = new ArrayList<Integer>();  
  161.             key1.add(codePoints[i]);  
  162.             if(convertMap.containsKey(key1)){  
  163.                 String aa=Integer.toHexString(codePoints[i]);  
  164.                 String value = convertMap.get(key1);  
  165.                 if(value != null){  
  166.                     result.append("<img width=\""+width+"px\" height=\""+height+"px\" style=\"vertical-align: bottom;\" src=\""+imgpath+"");  
  167.                     result.append(aa);  
  168.                     result.append(".png\"/>");  
  169.                     //System.out.println("key:"+key1);  
  170.                     //System.out.println("Map:"+value);  
  171.                 }  
  172.                 continue;  
  173.             }  
  174.             if(128529==codePoints[i]){  //处理空格  
  175.                 result.append(" ");  
  176.                 continue;  
  177.             }  
  178.             result.append(Character.toChars(codePoints[i]));  
  179.               
  180.         }  
  181.         return result.toString();  
  182.     }  
  183.    
  184.     int[] toCodePointArray(String str) {  
  185.         char[] ach = str.toCharArray();  
  186.         int len = ach.length;  
  187.         int[] acp = new int[Character.codePointCount(ach, 0, len)];  
  188.         int j = 0;  
  189.   
  190.         for (int i = 0, cp; i < len; i += Character.charCount(cp)) {  
  191.             cp = Character.codePointAt(ach, i);  
  192.             acp[j++] = cp;  
  193.         }  
  194.         return acp;  
  195.     }  
  196.   
  197. }  


3.调用 servlet 
Java代码   收藏代码
  1. public class ShareServlet extends HttpServlet {  
  2.       
  3.     private static EmojiConverter converter;  
  4.     private static String imgpath="http://xx.xxxx.com/opt/siteimg/mika/emoji/unicode/";  
  5.     @Override  
  6.     public void init() throws ServletException {  
  7.         converter = new EmojiConverter.Builder()  
  8.         .from(Type.UNICODE)  
  9.         .to(Type.SOFTBANK)  
  10.         .build();  
  11.     }  
  12.   
  13. public void doPost(HttpServletRequest request, HttpServletResponse response)  
  14.             throws ServletException, IOException {  
  15.           
  16.         FbbServiceClient service=null;  
  17.         try {  
  18.             String userid=request.getParameter("userid");  
  19.             String bid=request.getParameter("bid");  
  20.             if(userid!=null && bid!=null && userid.length()>0 && bid.length()>0){  
  21.                 service=new FbbServiceClient();  
  22.                 SnapService.Client client=service.open();  
  23.                 SnapItem item=client.getItemByIdS(Long.valueOf(userid),Long.valueOf(bid));  
  24.                 //进行转换  
  25.                 if(item.getUserName()!=null && item.getUserName().length()>0)  
  26.                     item.setUserName(converter.convert(20,20,imgpath,item.getUserName()));  
  27.                 if(item.getItemName()!=null && item.getItemName().length()>0)  
  28.                     item.setItemName(converter.convert(20,20,imgpath,item.getItemName()));  
  29.                 request.setAttribute("SnapItem", item);  
  30.                 request.getRequestDispatcher("/wxshare.jsp").forward(request, response);  
  31.             }  
  32.         } catch (Exception e) {  
  33.             e.printStackTrace();  
  34.             logger.error("errorcode ::: " + e.getMessage(), e);  
  35.         }finally{  
  36.             if(service!=null)  
  37.                 service.close();  
  38.         }  
  39.     }  
  40.   
  41. }  


4.写成一个ELfunction 在jsp EL 表达式中 使用 
Java代码   收藏代码
  1. package com.fanbaobao.util;  
  2.   
  3. import com.fanbaobao.util.EmojiConverter.Type;  
  4.   
  5. public class EmojiFunction {  
  6.     private static EmojiConverter converter;  
  7.     private static String imgpath="http://xx.xxxx.com/opt/siteimg/mika/emoji/unicode/";  
  8.     static{  
  9.         converter = new EmojiConverter.Builder()  
  10.         .from(Type.UNICODE)  
  11.         .to(Type.SOFTBANK)  
  12.         .build();  
  13.     }  
  14.   
  15.     public static String emoji(String arg){  
  16.         return emojiFun(null,null,arg);  
  17.     }  
  18.       
  19.     public static String emojiFun(Integer width,Integer height,String arg){  
  20.         if(width==null){  
  21.             width=20;  
  22.         }  
  23.         if(height==null){  
  24.             height=20;  
  25.         }  
  26.         return converter.convert(width.intValue(),height.intValue(),imgpath,arg);  
  27.     }  
  28. }  


5;编写 tld文件 
Xml代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <taglib version="2.0" xmlns="http://java.sun.com/xml/ns/j2ee"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-jsptaglibrary_2_0.xsd">  
  5.     <tlib-version>1.0</tlib-version>  
  6.     <short-name>mk</short-name>  
  7.     <uri>/mikadata-tags</uri>  
  8.       
  9.     <function>    
  10.         <name>emoji</name>  
  11.         <function-class>com.fanbaobao.util.EmojiFunction</function-class>    
  12.         <function-signature>   
  13.             java.lang.String emoji(java.lang.String)  
  14.         </function-signature>    
  15.     </function>  
  16.       
  17.     <function>    
  18.         <name>emojiFun</name>  
  19.         <function-class>com.fanbaobao.util.EmojiFunction</function-class>    
  20.         <function-signature>   
  21.             java.lang.String emojiFun(java.lang.Integer,java.lang.Integer,java.lang.String)  
  22.         </function-signature>    
  23.     </function>  
  24. </taglib>  


6:jsp中使用 
Html代码   收藏代码
  1. <%@ taglib uri="/mikadata-tags" prefix="mk"%>  
  2.   
  3. <td>${mk:emoji(dataObject.comment)}</td>  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值