测试文字编码格式,与设置字符串格式
调用:
juniversalchardet-1.0.3.jar
app里的Build.gradle implementation files('libs\\juniversalchardet-1.0.3.jar')
java调用:
import org.mozilla.universalchardet.UniversalDetector; /** * 测试编码格式,调用方法: * String encoding = GetByteEncode.getEncoding(plateResult); * Log.i(TAG, "字符串编码可能为:" + encoding); * <p> * 字符串格式强转: * String result = new String(plateResult, "gbk"); */ public class GetByteEncode { public static String getEncoding(byte[] bytes) { String DEFAULT_ENCODING = "UTF-8"; UniversalDetector detector = new UniversalDetector(null); detector.handleData(bytes, 0, bytes.length); detector.dataEnd(); String encoding = detector.getDetectedCharset(); detector.reset(); if (encoding == null) { encoding = DEFAULT_ENCODING; } return encoding; } }
。。。