本博文主要陈述,当项目中引入微信公众号聊天业务时,在进行聊天时,用户表情的转化,

分为:微信表情转化为项目中聊天表情的格式和项目表情转化成微信格式表情。

  • 定义表情正则表达式
private static String regex = "/::\\)|/::~|/::B|/::\\||/:8-\\)|/::<|/::\\$|/::X|/::Z|/::'\\(|/::-\\||/::@|/::P|/::D|/::O|/::\\(|/::\\+|/:--b|/::Q|/::T|/:,@P|/:,@-D|/::d|/:,@o|/::g|/:\\|-\\)|/::!|/::L|/::>|/::,@|/:,@f|/::-S|/:\\?|/:,@x|/:,@@|/::8|/:,@!|/:!!!|/:xx|/:bye|/:wipe|/:dig|/:handclap|/:&-\\(|/:B-\\)|/:<@|/:@>|/::-O|/:>-\\||/:P-\\(|/::'\\||/:X-\\)|/::\\*|/:@x|/:8\\*|/:pd|/:<W>|/:beer|/:basketb|/:oo|/:coffee|/:eat|/:pig|/:rose|/:fade|/:showlove|/:heart|/:break|/:cake|/:li|/:bome|/:kn|/:footb|/:ladybug|/:shit|/:moon|/:sun|/:gift|/:hug|/:strong|/:weak|/:share|/:v|/:@\\)|/:jj|/:@@|/:bad|/:lvu|/:no|/:ok|/:love|/:<L>|/:jump|/:shake|/:<O>|/:circle|/:kotow|/:turn|/:skip|/:oY|/:#-0|/:hiphot|/:kiss|/:<&|/:&>";
  • 声明静态表情集合
static{emotions.put("/::)","0");emotions.put("/::~","1");emotions.put("/::B","2");emotions.put("/::|","3");emotions.put("/:8-)","4");emotions.put("/::<","5");emotions.put("/::$","6");emotions.put("/::X","7");emotions.put("/::Z","8");emotions.put("/::'(","9");emotions.put("/::-|","10");emotions.put("/::@","11");emotions.put("/::P","12");emotions.put("/::D","13");emotions.put("/::O","14");emotions.put("/::(","15");emotions.put("/::+","16");emotions.put("[囧]","17");emotions.put("/::Q","18");emotions.put("/::T","19");emotions.put("/:,@P","20");emotions.put("/:,@-D","21");emotions.put("/::d","22");emotions.put("/:,@o","23");emotions.put("/::g","24");emotions.put("/:|-)","25");emotions.put("/::!","26");emotions.put("/::L","27");emotions.put("/::>","28");emotions.put("/::,@","29");emotions.put("/:,@f","30");emotions.put("/::-S","31");emotions.put("/:?","32");emotions.put("/:,@x","33");emotions.put("/:,@@","34");emotions.put("/::8","35");emotions.put("/:,@!","36");emotions.put("/:!!!","37");emotions.put("/:xx","38");emotions.put("/:bye","39");emotions.put("/:wipe","40");emotions.put("/:dig","41");emotions.put("/:handclap","42");emotions.put("/:&-(","43");emotions.put("/:B-)","44");emotions.put("/:<@","45");emotions.put("/:@>","46");emotions.put("/::-O","47");emotions.put("/:>-|","48");emotions.put("/:P-(","49");emotions.put("/::'|","50");emotions.put("/:X-)","51");emotions.put("/::*","52");emotions.put("/:@x","53");emotions.put("/:8*","54");emotions.put("/:pd","55");emotions.put("/:<W>","56");emotions.put("/:beer","57");emotions.put("/:basketb","58");emotions.put("/:oo","59");emotions.put("/:coffee","60");emotions.put("/:eat","61");emotions.put("/:pig","62");emotions.put("/:rose","63");emotions.put("/:fade","64");emotions.put("/:showlove","65");emotions.put("/:heart","66");emotions.put("/:break","67");emotions.put("/:cake","68");emotions.put("/:li","69");emotions.put("/:bome","70");emotions.put("/:kn","71");emotions.put("/:footb","72");emotions.put("/:ladybug","73");emotions.put("/:shit","74");emotions.put("/:moon","75");emotions.put("/:sun","76");emotions.put("/:gift","77");emotions.put("/:hug","78");emotions.put("/:strong","79");emotions.put("/:weak","80");emotions.put("/:share","81");emotions.put("/:v","82");emotions.put("/:@)","83");emotions.put("/:jj","84");emotions.put("/:@@","85");emotions.put("/:bad","86");emotions.put("/:lvu","87");emotions.put("/:no","88");emotions.put("/:ok","89");emotions.put("/:love","90");emotions.put("/:<L>","91");emotions.put("/:jump","92");emotions.put("/:shake","93");emotions.put("/:<O>","94");emotions.put("/:circle","95");emotions.put("/:kotow","96");emotions.put("/:turn","97");emotions.put("/:skip","98");emotions.put("/:oY","99");emotions.put("/:#-0","100");emotions.put("/:hiphot","101");emotions.put("/:kiss","102");emotions.put("/:<&","103");emotions.put("/:&>","104");emotions.put("[嘿哈]","105");emotions.put("[Facepalm]","106");emotions.put("[奸笑]","107");emotions.put("[机智]","108");emotions.put("[皱眉]","109");emotions.put("[耶]","110");emotions.put("[红包]","111");emotions.put("[鸡]","112");emotions.put("[笑脸]","113");emotions.put("[开心]","114");emotions.put("[哭]","115");emotions.put("[破涕为笑]","116");emotions.put("[晕]","117");emotions.put("[恐惧]","118");emotions.put("[冰激凌]","119");emotions.put("[蛋糕]","120");emotions.put("[苹果]","121");emotions.put("[飞机]","122");emotions.put("[火箭]","123");emotions.put("[自行车]","124");emotions.put("[高铁]","125");emotions.put("[警告]","126");emotions.put("[旗]","127");emotions.put("[O]","128");emotions.put("[X]","129");emotions.put("[版权]","130");emotions.put("[注册商标]","131");emotions.put("[商标]","132");emotions.put("[篮球]","133");emotions.put("[足球]","134");
  • 微信表情内容转成项目聊天表情格式
public static String wechatToUCKeFuface(String content){if(content==null){return null;}logger.info("打印微信表情==========>"+content);StringBuffer sb = new StringBuffer();// 发送内容和pattern 进行匹配Matcher m =  pattern.matcher(content);while(m.find()){//此里面的可以替换为配置String faceStr = m.group();System.out.println("正则表达式匹配后的数据:faceStr =[{}]"+faceStr);// 从定义的静态变量表情中根据 key 查找 valueString face = emotions.get(faceStr);System.out.println("获取表情:face =[{}]"+face);if(face!=null){m.appendReplacement(sb, "<img src=\"/js/kindeditor/plugins/emoticons/images/"+emotions.get(m.group())+".gif\" border=\"0\" alt=\"\" />");System.out.println("打印微信表情替换为配置==========>"+sb+"=============");}else{System.out.println("wechatToUCKeFuface not find:{}"+m.group());//logger.info("打印微信表情替换为配置2==========>"+sb+"=============");}}m.appendTail(sb);System.out.println("打印微信表情end==========>"+sb.toString()+"=============");return sb.toString();}
  • 项目表情转化成微信格式表情
public static String ucKeFufaceTowechat(String content){if(content==null){return null;}StringBuffer sb = new StringBuffer();Matcher m =  patternFaces.matcher(content);while(m.find()){String face = faces.get(m.group(2));if(face!=null){m.appendReplacement(sb, faces.get(m.group(2)));}else{logger.warn("ucKeFufaceTowechat not find:{}",m.group());}}m.appendTail(sb);return sb.toString().replaceAll("<br[ ]{1,}/>|&nbsp;", "");}
  • 全部代码
package com.dol.school.common.utils;import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;/*** 微信公众号和项目中的聊天表情转换* @Author GXY* @Date 22:21 2017/8/5**/
public class WechatUtils {private static Logger logger = LoggerFactory.getLogger(WechatUtils.class);private static String regex = "/::\\)|/::~|/::B|/::\\||/:8-\\)|/::<|/::\\$|/::X|/::Z|/::'\\(|/::-\\||/::@|/::P|/::D|/::O|/::\\(|/::\\+|/:--b|/::Q|/::T|/:,@P|/:,@-D|/::d|/:,@o|/::g|/:\\|-\\)|/::!|/::L|/::>|/::,@|/:,@f|/::-S|/:\\?|/:,@x|/:,@@|/::8|/:,@!|/:!!!|/:xx|/:bye|/:wipe|/:dig|/:handclap|/:&-\\(|/:B-\\)|/:<@|/:@>|/::-O|/:>-\\||/:P-\\(|/::'\\||/:X-\\)|/::\\*|/:@x|/:8\\*|/:pd|/:<W>|/:beer|/:basketb|/:oo|/:coffee|/:eat|/:pig|/:rose|/:fade|/:showlove|/:heart|/:break|/:cake|/:li|/:bome|/:kn|/:footb|/:ladybug|/:shit|/:moon|/:sun|/:gift|/:hug|/:strong|/:weak|/:share|/:v|/:@\\)|/:jj|/:@@|/:bad|/:lvu|/:no|/:ok|/:love|/:<L>|/:jump|/:shake|/:<O>|/:circle|/:kotow|/:turn|/:skip|/:oY|/:#-0|/:hiphot|/:kiss|/:<&|/:&>";
//  private static String regex = "/::\\)|/::~|/::B|/::\\||/:8-\\)|/::<|/::$|/::X|/::Z|/::'\\(|/::-\\||/::@|/::P|/::D|/::O|/::\\(|/::+|/:--b|/::Q|/::T|/:,@P|/:,@-D|/::d|/:,@o|/::g|/:\\|-\\)|/::!|/::L|/::>|/::,@|/:,@f|/::-S|/:?|/:,@x|/:,@@|/::8|/:,@!|/:!!!|/:xx|/:bye|/:wipe|/:dig|/:handclap|/:&-\\(|/:B-\\)|/:<@|/:@>|/::-O|/:>-\\||/:P-\\(|/::'\\||/:X-\\)|/::*|/:@x|/:8*|/:pd|/:<W>|/:beer|/:basketb|/:oo|/:coffee|/:eat|/:pig|/:rose|/:fade|/:showlove|/:heart|/:break|/:cake|/:li|/:bome|/:kn|/:footb|/:ladybug|/:shit|/:moon|/:sun|/:gift|/:hug|/:strong|/:weak|/:share|/:v|/:@\\)|/:jj|/:@@|/:bad|/:lvu|/:no|/:ok|/:love|/:<L>|/:jump|/:shake|/:<O>|/:circle|/:kotow|/:turn|/:skip|/:oY|/:#-0|/:hiphot|/:kiss|/:<&|/:&>";//  regex正则表达式,  Pattern.CASE_INSENSITIVE:启用不区分大小写的匹配。private static Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);private static Pattern patternFaces = Pattern.compile("(<img[\\S\\s]*?src=\"[\\S\\s]*?/emoticons/images/([\\d]{1,}).gif\"[\\s\\S]*?/>)", Pattern.CASE_INSENSITIVE);private static Map<String,String> emotions = new HashMap<>();private static Map<String,String> faces = new HashMap<>();static{emotions.put("/::)","0");emotions.put("/::~","1");emotions.put("/::B","2");emotions.put("/::|","3");emotions.put("/:8-)","4");emotions.put("/::<","5");emotions.put("/::$","6");emotions.put("/::X","7");emotions.put("/::Z","8");emotions.put("/::'(","9");emotions.put("/::-|","10");emotions.put("/::@","11");emotions.put("/::P","12");emotions.put("/::D","13");emotions.put("/::O","14");emotions.put("/::(","15");emotions.put("/::+","16");emotions.put("/:--b","17");emotions.put("/::Q","18");emotions.put("/::T","19");emotions.put("/:,@P","20");emotions.put("/:,@-D","21");emotions.put("/::d","22");emotions.put("/:,@o","23");emotions.put("/::g","24");emotions.put("/:|-)","25");emotions.put("/::!","26");emotions.put("/::L","27");emotions.put("/::>","28");emotions.put("/::,@","29");emotions.put("/:,@f","30");emotions.put("/::-S","31");emotions.put("/:?","32");emotions.put("/:,@x","33");emotions.put("/:,@@","34");emotions.put("/::8","35");emotions.put("/:,@!","36");emotions.put("/:!!!","37");emotions.put("/:xx","38");emotions.put("/:bye","39");emotions.put("/:wipe","40");emotions.put("/:dig","41");emotions.put("/:handclap","42");emotions.put("/:&-(","43");emotions.put("/:B-)","44");emotions.put("/:<@","45");emotions.put("/:@>","46");emotions.put("/::-O","47");emotions.put("/:>-|","48");emotions.put("/:P-(","49");emotions.put("/::'|","50");emotions.put("/:X-)","51");emotions.put("/::*","52");emotions.put("/:@x","53");emotions.put("/:8*","54");emotions.put("/:pd","55");emotions.put("/:<W>","56");emotions.put("/:beer","57");emotions.put("/:basketb","58");emotions.put("/:oo","59");emotions.put("/:coffee","60");emotions.put("/:eat","61");emotions.put("/:pig","62");emotions.put("/:rose","63");emotions.put("/:fade","64");emotions.put("/:showlove","65");emotions.put("/:heart","66");emotions.put("/:break","67");emotions.put("/:cake","68");emotions.put("/:li","69");emotions.put("/:bome","70");emotions.put("/:kn","71");emotions.put("/:footb","72");emotions.put("/:ladybug","73");emotions.put("/:shit","74");emotions.put("/:moon","75");emotions.put("/:sun","76");emotions.put("/:gift","77");emotions.put("/:hug","78");emotions.put("/:strong","79");emotions.put("/:weak","80");emotions.put("/:share","81");emotions.put("/:v","82");emotions.put("/:@)","83");emotions.put("/:jj","84");emotions.put("/:@@","85");emotions.put("/:bad","86");emotions.put("/:lvu","87");emotions.put("/:no","88");emotions.put("/:ok","89");emotions.put("/:love","90");emotions.put("/:<L>","91");emotions.put("/:jump","92");emotions.put("/:shake","93");emotions.put("/:<O>","94");emotions.put("/:circle","95");emotions.put("/:kotow","96");emotions.put("/:turn","97");emotions.put("/:skip","98");emotions.put("/:oY","99");emotions.put("/:#-0","100");emotions.put("/:hiphot","101");emotions.put("/:kiss","102");emotions.put("/:<&","103");emotions.put("/:&>","104");//add by lyl 2019730emotions.put("[嘿哈]","105");emotions.put("[Facepalm]","106");emotions.put("[奸笑]","107");emotions.put("[机智]","108");emotions.put("[皱眉]","109");emotions.put("[耶]","110");emotions.put("[红包]","111");emotions.put("[鸡]","112");emotions.put("[笑脸]","113");emotions.put("[开心]","114");emotions.put("[哭]","115");emotions.put("[破涕为笑]","116");emotions.put("[晕]","117");emotions.put("[恐惧]","118");emotions.put("[冰激凌]","119");emotions.put("[蛋糕]","120");emotions.put("[苹果]","121");emotions.put("[飞机]","122");emotions.put("[火箭]","123");emotions.put("[自行车]","124");emotions.put("[高铁]","125");emotions.put("[警告]","126");emotions.put("[旗]","127");emotions.put("[O]","128");emotions.put("[X]","129");emotions.put("[版权]","130");emotions.put("[注册商标]","131");emotions.put("[商标]","132");emotions.put("[篮球]","133");emotions.put("[足球]","134");for (Map.Entry<String, String> entry : emotions.entrySet()) {faces.put(entry.getValue(), entry.getKey());}}public static String setTagIds(Integer[] tagIds){if(tagIds==null){return null;}StringBuilder sb = new StringBuilder();for (Integer integer : tagIds) {sb.append(integer.toString());sb.append(",");}return sb.toString();}public static Integer[] getTagIds(String tagIds){if(tagIds==null || StringUtils.isBlank(tagIds)){return null;}String[] tags = tagIds.split(",");Integer[] tagarr = new Integer[tags.length];for (int i = 0; i < tagarr.length; i++) {tagarr[i] = NumberUtils.toInt(tags[i]);}return tagarr;}/*** 微信表情内容转成项目聊天表情格式* @param content* @return*/public static String wechatToUCKeFuface(String content){if(content==null){return null;}logger.info("打印微信表情==========>"+content);StringBuffer sb = new StringBuffer();// 发送内容和pattern 进行匹配Matcher m =  pattern.matcher(content);while(m.find()){//此里面的可以替换为配置String faceStr = m.group();System.out.println("正则表达式匹配后的数据:faceStr =[{}]"+faceStr);// 从定义的静态变量表情中根据 key 查找 valueString face = emotions.get(faceStr);System.out.println("获取表情:face =[{}]"+face);if(face!=null){m.appendReplacement(sb, "<img src=\"/js/kindeditor/plugins/emoticons/images/"+emotions.get(m.group())+".gif\" border=\"0\" alt=\"\" />");System.out.println("打印微信表情替换为配置==========>"+sb+"=============");}else{System.out.println("wechatToUCKeFuface not find:{}"+m.group());//logger.info("打印微信表情替换为配置2==========>"+sb+"=============");}}m.appendTail(sb);System.out.println("打印微信表情end==========>"+sb.toString()+"=============");return sb.toString();}/*** 项目表情转化成微信格式表情* @Date 22:27 2017/8/5**/public static String ucKeFufaceTowechat(String content){if(content==null){return null;}StringBuffer sb = new StringBuffer();Matcher m =  patternFaces.matcher(content);while(m.find()){String face = faces.get(m.group(2));if(face!=null){m.appendReplacement(sb, faces.get(m.group(2)));}else{logger.warn("ucKeFufaceTowechat not find:{}",m.group());}}m.appendTail(sb);return sb.toString().replaceAll("<br[ ]{1,}/>|&nbsp;", "");}public static void main(String[] args) {String str = wechatToUCKeFuface("/::)/::Q/::T/:P-(");System.out.println(str);}
}

如需微信表情包请访问如下地址:

微信公众号和web项目聊天表情转化相关推荐

  1. 通过微信公众号访问个人项目

    通过微信公众号访问个人项目 1.首先申请一个微信公众号(注意不是小程序) 2.将微信公众号相关组件放到项目里 3.获取微信公众号的开发者密钥(AppID+AppSecret) 4.修改微信公众号相关组 ...

  2. .NET微信公众号开发系列 - 项目介绍

    由于业务需要,需要开发微信公众号.不过没有查询到比较有用的信息,只能自己摸索前进. 写这个微信公众号开发系列,希望能提供一些帮助. 需要的功能有创建订单.查看订单.还有基本的用户管理. 由于团队人员有 ...

  3. 微信公众号自定义菜单添加emoji表情图标

    微信公众号自定义菜单添加emoji表情图标 第一步:打开微信公众平台接口调试工具,点击前往接口调试工具: 第二步:把这段代码   {"button":[{"sub_but ...

  4. 微信公众号的web开发者工具的自助解绑功能终于有了!

    本篇文章主要讲解微信公众号的web开发者自助解绑的方式,今年一月份突然发现微信团队对这个功能进行了添加. 具体说明:web开发者权限不能通过"公众平台安全助手"进行自助解绑,这个问 ...

  5. 视频教程-微信公众号前后端项目视频教程-其他

    微信公众号前后端项目视频教程 张长志技术全才.擅长领域:区块链.大数据.Java等.10余年软件研发及企业培训经验,曾为多家大型企业提供企业内训如中石化,中国联通,中国移动等知名企业.拥有丰富的企业应 ...

  6. 已嵌入微信公众号内的小图聊天机器人介绍和使用说明

    小图是一个融合常识与专业知识的智能聊天机器人,进入"人工智能遇上知识图谱"公众号,并在聊天窗口中发送消息就可以开始聊天,同时支持多轮多对话,小图能够提供闲聊.生活服务咨询.智能助手 ...

  7. 客服系统对接微信公众号-访客在聊天界面扫码-临时访客绑定公众号OpenID可接收客服回复消息通知...

    访客在线咨询有以下几个问题: 1. 访客打开聊天窗口,一般情况下都是临时访客,只存在于当前浏览器. 2. 浏览器清理缓存,或者换一个浏览器,访客ID会重新生成就会变成一个新访客. 3. 访客咨询后,关 ...

  8. php获取微信公众号用户信息头像,微信公众号的web页面如何获取微信用户名和头像?...

    首先,你的网站入口必须是微信服务号(开通认证.拥有获取用户openid权限:订阅号是不行的). 方法:网页通过微信的Oauth2认证链接.(必须是微信的内置浏览器,通过微信服务号进入) 操作:通过服务 ...

  9. 微信公众号接入web服务器,asp.net Web API 接入微信公众平台验证服务器,接收微信发送的POST请求...

    验证签名,接入微信公众平台:不能直接返回String,直接返回String微信服务器不接受. // GET: api/checkSignature/5 public HttpResponseMessa ...

最新文章

  1. CORS-跨域资源共享 解决跨域问题
  2. 32通过rs485烧写程序_28027芯片sci程序烧写问题请教
  3. 苹果要做第一个吃螃蟹的人!将率先尝试台积电5nm工艺
  4. Vue单文件项目自定义组件入门
  5. [asp.netMVC]通过configSource提高web.config配置灵活性
  6. 优秀代码所具备的5大品质 你的代码呢?
  7. Compare int a and int b without if
  8. 雅可比迭代法和高斯赛德尔迭代法
  9. 测量学5_测量误差理论的基本知识
  10. sd卡重新分区以及格式化
  11. 天蝎项目整机柜服务器技术规范,天蝎整机柜服务器技术规范25.doc
  12. 化妆品护肤DiY的广告界面 简单的jquery 图片无缝滚动
  13. iOS多线程的初步研究(三)-- NSRunLoop
  14. 高德地图自定义点标记大小_自定义高德地图的标记样式和内容
  15. 锂电池电量百分比计算_锂电池容量计算
  16. 2021年德阳2中高考成绩查询,2021年德阳高中录取分数线是多少及高中排名榜
  17. 【MySQL入门指北】第六篇 按条件筛选
  18. matlab处理时间数据绘图
  19. jQuery赋值checked的几种写法:
  20. liunx下通过Canal将MySQL数据同步到Elasticsearch

热门文章

  1. LVGL8制作简易时钟
  2. 博弈论中的MinMax搜索算法
  3. 开始尝试淘宝直通车推广
  4. Excel引用函数-实例1
  5. 《流浪地球》地下城怎么建?三维地学建模探秘地下空间
  6. 金仓数据库 KingbaseES SQL 语言参考手册 (10. 查询和子查询)
  7. 谷歌Chrome 操作系统基于浏览器的OS
  8. win10投影无法正常使用:我们正在确认此功能 解决方法
  9. 2023年会议教学庭审录像机产品分析
  10. 数据结构与算法【Java】05---排序算法总结