EmojiUtil


import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class EmojiUtil {
	/**
	 * 将带表情的文本转换为特殊格式的文本
	 * 
	 * @param source
	 * @return
	 */
	public static StringBuffer emojiText2Text(String source) {
		StringBuffer buffer = new StringBuffer();// 转换以后的文本
		int length = source.length();
		int skip;
		for (int i = 0; i < length; i += skip) {
			skip = 0;
			boolean bSoftBankEmoji = false;
			char c = source.charAt(i);
			if (isSoftBankEmoji(c)) {// 是否是SoftBankEmoji表情码
				bSoftBankEmoji = true;
				skip = 1;
			}
			String unicodeHexStr = "";
			if (!bSoftBankEmoji) {
				int unicode = Character.codePointAt(source, i);// 把text中的索引位置开始的那个表情转换成16进制码
				unicodeHexStr = Integer.toHexString(unicode);
				System.out.println(Integer.toHexString(unicode) + "---16");// 这个就是0x1f603,表情的16进制
				skip = Character.charCount(unicode);// 计算出表情站位多少,应该是2
				boolean bunicode = unicode > 0xff;

				if (!bunicode && i + skip < length) {
					int followUnicode = Character.codePointAt(source, i + skip);
					if (followUnicode == 0x20e3) {
						int followSkip = Character.charCount(followUnicode);
						switch (unicode) {
						case 0x0031:
						case 0x0032:
						case 0x0033:
						case 0x0034:
						case 0x0035:
						case 0x0036:
						case 0x0037:
						case 0x0038:
						case 0x0039:
						case 0x0030:
						case 0x0023:
							break;
						default:
							followSkip = 0;
							break;
						}
						skip += followSkip;
					} else {
						int followSkip = Character.charCount(followUnicode);
						switch (unicode) {
						case 0x1f1ef:
							bunicode = followUnicode == 0x1f1f5;
							break;
						case 0x1f1fa:
							bunicode = followUnicode == 0x1f1f8;
							break;
						case 0x1f1eb:
							bunicode = followUnicode == 0x1f1f7;
							break;
						case 0x1f1e9:
							bunicode = followUnicode == 0x1f1ea;
							break;
						case 0x1f1ee:
							bunicode = followUnicode == 0x1f1f9;
							break;
						case 0x1f1ec:
							bunicode = followUnicode == 0x1f1e7;
							break;
						case 0x1f1ea:
							bunicode = followUnicode == 0x1f1f8;
							break;
						case 0x1f1f7:
							bunicode = followUnicode == 0x1f1fa;
							break;
						case 0x1f1e8:
							bunicode = followUnicode == 0x1f1f3;
							break;
						case 0x1f1f0:
							bunicode = followUnicode == 0x1f1f7;
							break;
						default:
							followSkip = 0;
							break;
						}
						skip += followSkip;
					}
				}
			}
			if (skip == 2) {// 是表情
				buffer.append("#0x"
						+ unicodeHexStr + "#");
			} else {
				buffer.append(source.substring(i, i + skip));
			}
			System.out.println(buffer.toString());
		}
		return buffer;
	}

	// 把特殊格式的文本转成带表情的文本
	public static String text2EmojiText(String st) {
		Pattern pattern = Pattern.compile("#0x.+?#", Pattern.CASE_INSENSITIVE);
		Matcher matcher = pattern.matcher(st);
		StringBuffer buffer = new StringBuffer();
		while (matcher.find()) {
			matcher.appendReplacement(buffer, emoji2Unicode(matcher.group()
					.replace("#", "")));
		}
		matcher.appendTail(buffer);

		return buffer.toString();
	}

	private static boolean isSoftBankEmoji(char c) {
		return ((c >> 12) == 0xe);
	}

	// high offset
	public static final int W1 = Integer.valueOf("D7C0", 16);

	// low offset
	public static final int W2 = Integer.valueOf("DC00", 16);

	// 表情转Unicode
	public static String emoji2Unicode(String ed) {
		String doughnutBinary = Integer.toBinaryString(Integer.valueOf(
				ed.substring(2), 16));
		// 拆成高低 10 位表示
		String highSetsBinary = doughnutBinary.substring(0,
				doughnutBinary.length() - 10);
		String lowSetsBinary = doughnutBinary.substring(
				doughnutBinary.length() - 10, doughnutBinary.length());
		System.out.println(highSetsBinary); // 1111100
		System.out.println(lowSetsBinary); // 1100000000

		// 分别与偏移量相加,得到两个编码值
		String highSetsHexFixed = Integer.toHexString(W1
				+ Integer.valueOf(highSetsBinary, 2));
		String lowSetsHexFixed = Integer.toHexString(W2
				+ Integer.valueOf(lowSetsBinary, 2));
		System.out.println(highSetsHexFixed); // d83c
		System.out.println(lowSetsHexFixed); // df00

		// 拼接这两个编码值,还原字符表示
		char highChar = (char) Integer.valueOf(highSetsHexFixed, 16).intValue();
		char lowChar = (char) Integer.valueOf(lowSetsHexFixed, 16).intValue();
		System.out.println(highChar); // ?
		System.out.println(lowChar); // ?
		System.out.println(highChar + "" + lowChar); // 🌀
		// mTxtEmojicon.setText(highChar + "" + lowChar);
		return highChar + "" + lowChar;
	}

	// Unicode 转表情
	public static String unicode2Emoji(String ss) {
		return (Integer
				.toHexString(Integer.valueOf(
						Integer.toBinaryString((Integer.parseInt(
								Integer.toHexString(ss.toCharArray()[0]), 16) - W1))
								+ Integer.toBinaryString((Integer.parseInt(
										Integer.toHexString(ss.toCharArray()[1]),
										16) - W2)), 2))).toLowerCase();
	}
}

使用方法
String text="";//前端输入的内容
text=EmojiUtil.emojiText2Text(text).toString();
将text存入数据库就行了
取出展示时
String text="";//数据库获取的内容
text=EmojiUtil.text2EmojiText(text).toString();
将text传给页面即可

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值