基于Java編寫emoji表情處理工具類
emoji表情也是使用Unicode編碼的,但UTF8編碼是不支持的。我們?nèi)绻氪鎯moji到數(shù)據(jù)庫,一般有兩種方法,以mysql為例,將數(shù)據(jù)庫編碼從 utf8 改為 utf8mb4,第二種就是做一個轉換,將emoji表情轉換成另一個字符,所以本文主要來和大家講講如何使用Java實現(xiàn)處理emoji表情吧
xml
<!-- https://mvnrepository.com/artifact/com.vdurmont/emoji-java -->
<dependency>
<groupId>com.vdurmont</groupId>
<artifactId>emoji-java</artifactId>
<version>5.1.1</version>
</dependency>
java代碼
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang.StringUtils;
import com.vdurmont.emoji.EmojiParser;
/**
* emoji表情過濾
* @author madaha
*
*/
public class EmojiFilter {
/**
* 判斷是否存在Emoji
* @author madaha
*
* @param codePoint
* @return
*/
private static boolean isEmojiCharacter(char codePoint) {
return (codePoint == 0x0) || (codePoint == 0x9)
|| (codePoint == 0xA) || (codePoint == 0xD)
|| ((codePoint >= 0x20) && (codePoint <= 0xD7FF))
|| ((codePoint >= 0xE000) && (codePoint <= 0xFFFD))
|| ((codePoint >= 0x10000) && (codePoint <= 0x10FFFF));
}
/**
* 過濾emoji 或者 其他非文字類型的字符
* @author madaha
*
* @param source 待過濾字符串
* @return
*/
public static String filterEmoji(String source) {
if (StringUtils.isBlank(source)) {
return source;
}
StringBuilder buf = null;
int len = source.length();
for (int i = 0; i < len; i++) {
char codePoint = source.charAt(i);
if (isEmojiCharacter(codePoint)) {
if (buf == null) {
buf = new StringBuilder(source.length());
}
buf.append(codePoint);
}
}
if (buf == null) {
return source;
} else {
if (buf.length() == len) {
buf = null;
return source;
} else {
return buf.toString();
}
}
}
/**
* @Description 將字符串中的emoji表情轉換成可以在utf-8字符集數(shù)據(jù)庫中保存的格式(表情占4個字節(jié),需要utf8mb4字符集)
* @param str 待轉換字符串
* @return 轉換后字符串
* @throws UnsupportedEncodingException
* exception
*/
public static String emojiConvert1(String str) throws UnsupportedEncodingException {
String patternString = "([\\x{10000}-\\x{10ffff}\ud800-\udfff])";
Pattern pattern = Pattern.compile(patternString);
Matcher matcher = pattern.matcher(str);
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
try {
matcher.appendReplacement(sb, "[[" + URLEncoder.encode(matcher.group(1), "UTF-8") + "]]");
} catch (UnsupportedEncodingException e) {
System.out.println(e);
throw e;
}
}
matcher.appendTail(sb);
// System.out.println("emojiConvert " + str + " to " + sb.toString() + ", len:" + sb.length());
return sb.toString();
}
/**
* @Description 還原utf8數(shù)據(jù)庫中保存的含轉換后emoji表情的字符串
* @param str 轉換前的字符串
* @return 轉換后的字符串
* @throws UnsupportedEncodingException
* exception
*/
public static String emojiRecovery2(String str) throws UnsupportedEncodingException {
String patternString = "\\[\\[(.*?)\\]\\]";
Pattern pattern = Pattern.compile(patternString);
Matcher matcher = pattern.matcher(str);
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
try {
matcher.appendReplacement(sb, URLDecoder.decode(matcher.group(1), "UTF-8"));
} catch (UnsupportedEncodingException e) {
System.out.println(e);
throw e;
}
}
matcher.appendTail(sb);
// System.out.println("emojiRecovery " + str + " to " + sb.toString());
return sb.toString();
}
/**
* emoji表情處理測試方法
* @author madaha
*
* @param args
*/
public static void main(String[] args) {
String str = "emoji表情????輸入測試????";
System.out.println("原始字符為:\n" + str);
System.out.println("to aliases 之后:\n" + EmojiParser.parseToAliases(str));
str = EmojiParser.parseToAliases(str);
System.out.println("還原:\n" + EmojiParser.parseToUnicode(str));
}
}
到此這篇關于基于Java編寫emoji表情處理工具類的文章就介紹到這了,更多相關Java處理emoji表情內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
java如何實現(xiàn)post請求webservice服務端
這篇文章主要介紹了java如何實現(xiàn)post請求webservice服務端,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03
Springboot 整合shiro實現(xiàn)權限控制的方法
這篇文章主要介紹了Springboot 整合shiro實現(xiàn)權限控制的相關知識,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11
SpringMVC使用@Valid注解進行數(shù)據(jù)驗證的方法
本篇文章主要介紹了SpringMVC使用@Valid注解進行數(shù)據(jù)驗證的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02
Spring-boot結合Shrio實現(xiàn)JWT的方法
這篇文章主要介紹了Spring-boot結合Shrio實現(xiàn)JWT的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-05-05
SpringBoot @value注解動態(tài)刷新問題小結
@Value注解 所對應的數(shù)據(jù)源來自項目的 Environment 中,我們可以將數(shù)據(jù)庫或其他文件中的數(shù)據(jù),加載到項目的 Environment 中,然后 @Value注解 就可以動態(tài)獲取到配置信息了,這篇文章主要介紹了SpringBoot @value注解動態(tài)刷新,需要的朋友可以參考下2023-09-09

