Android通讯录开发之通讯录联系人搜索功能最新实现,写给程序员的Flutter详细教程

/** First and last Chinese character with known Pinyin according to zh collation */

private static final String FIRST_PINYIN_UNIHAN = “\u963F”;

private static final String LAST_PINYIN_UNIHAN = “\u9FFF”;

private static final Collator COLLATOR = Collator.getInstance(Locale.CHINA);

private static HanziToPinyin sInstance;

private final boolean mHasChinaCollator;

public static class Token {

/**

  • Separator between target string for each source char

*/

public static final String SEPARATOR = " ";

public static final int LATIN = 1;

public static final int PINYIN = 2;

public static final int UNKNOWN = 3;

public Token() {

}

public Token(int type, String source, String target) {

this.type = type;

this.source = source;

this.target = target;

}

/**

  • Type of this token, ASCII, PINYIN or UNKNOWN.

*/

public int type;

/**

  • Original string before translation.

*/

public String source;

/**

  • Translated string of source. For Han, target is corresponding Pinyin. Otherwise target is

  • original string in source.

*/

public String target;

}

protected HanziToPinyin(boolean hasChinaCollator) {

mHasChinaCollator = hasChinaCollator;

}

public static HanziToPinyin getInstance() {

synchronized (HanziToPinyin.class) {

if (sInstance != null) {

return sInstance;

}

// Check if zh_CN collation data is available

final Locale locale[] = Collator.getAvailableLocales();

for (int i = 0; i < locale.length; i++) {

if (locale[i].equals(Locale.CHINA)) {

// Do self validation just once.

if (DEBUG) {

Log.d(TAG, "Self validation. Result: " + doSelfValidation());

}

sInstance = new HanziToPinyin(true);

return sInstance;

}

}

Log.w(TAG, “There is no Chinese collator, HanziToPinyin is disabled”);

sInstance = new HanziToPinyin(false);

return sInstance;

}

}

/**

  • Validate if our internal table has some wrong value.

  • @return true when the table looks correct.

*/

private static boolean doSelfValidation() {

char lastChar = UNIHANS[0];

String lastString = Character.toString(lastChar);

for (char c : UNIHANS) {

if (lastChar == c) {

continue;

}

final String curString = Character.toString©;

int cmp = COLLATOR.compare(lastString, curString);

if (cmp >= 0) {

Log.e(TAG, "Internal error in Unihan table. " + "The last string “” + lastString

  • “” is greater than current string “” + curString + “”.");

return false;

}

lastString = curString;

}

return true;

}

private Token getToken(char character) {

Token token = new Token();

final String letter = Character.toString(character);

token.source = letter;

int offset = -1;

int cmp;

if (character < 256) {

token.type = Token.LATIN;

token.target = letter;

return token;

} else {

cmp = COLLATOR.compare(letter, FIRST_PINYIN_UNIHAN);

if (cmp < 0) {

token.type = Token.UNKNOWN;

token.target = letter;

return token;

} else if (cmp == 0) {

token.type = Token.PINYIN;

offset = 0;

} else {

cmp = COLLATOR.compare(letter, LAST_PINYIN_UNIHAN);

if (cmp > 0) {

token.type = Token.UNKNOWN;

token.target = letter;

return token;

} else if (cmp == 0) {

token.type = Token.PINYIN;

offset = UNIHANS.length - 1;

}

}

}

token.type = Token.PINYIN;

if (offset < 0) {

int begin = 0;

int end = UNIHANS.length - 1;

while (begin <= end) {

offset = (begin + end) / 2;

final String unihan = Character.toString(UNIHANS[offset]);

cmp = COLLATOR.compare(letter, unihan);

if (cmp == 0) {

break;

} else if (cmp > 0) {

begin = offset + 1;

} else {

end = offset - 1;

}

}

}

if (cmp < 0) {

offset–;

}

StringBuilder pinyin = new StringBuilder();

for (int j = 0; j < PINYINS[offset].length && PINYINS[offset][j] != 0; j++) {

pinyin.append((char) PINYINS[offset][j]);

}

token.target = pinyin.toString();

if (TextUtils.isEmpty(token.target)) {

token.type = Token.UNKNOWN;

token.target = token.source;

}

return token;

}

/**

  • Convert the input to a array of tokens. The sequence of ASCII or Unknown characters without

  • space will be put into a Token, One Hanzi character which has pinyin will be treated as a

  • Token. If these is no China collator, the empty token array is returned.

*/

public ArrayList get(final String input) {

ArrayList tokens = new ArrayList();

if (!mHasChinaCollator || TextUtils.isEmpty(input)) {

// return empty tokens.

return tokens;

}

final int inputLength = input.length();

final StringBuilder sb = new StringBuilder();

int tokenType = Token.LATIN;

// Go through the input, create a new token when

// a. Token type changed

// b. Get the Pinyin of current charater.

// c. current character is space.

for (int i = 0; i < inputLength; i++) {

final char character = input.charAt(i);

if (character == ’ ') {

if (sb.length() > 0) {

addToken(sb, tokens, tokenType);

}

} else if (character < 256) {

if (tokenType != Token.LATIN && sb.length() > 0) {

addToken(sb, tokens, tokenType);

}

tokenType = Token.LATIN;

sb.append(character);

} else {

Token t = getToken(character);

if (t.type == Token.PINYIN) {

if (sb.length() > 0) {

addToken(sb, tokens, tokenType);

}

tokens.add(t);

tokenType = Token.PINYIN;

} else {

if (tokenType != t.type && sb.length() > 0) {

addToken(sb, tokens, tokenType);

}

tokenType = t.type;

sb.append(character);

}

}

}

if (sb.length() > 0) {

addToken(sb, tokens, tokenType);

}

return tokens;

}

private void addToken(

final StringBuilder sb, final ArrayList tokens, final int tokenType) {

String str = sb.toString();

tokens.add(new Token(tokenType, str, str));

sb.setLength(0);

}

}

这个工具类用于将汉字转换为拼音,通过一个Token对象来存储转换过后的拼音和转换之前的字符串。

下面定义一个方法获取输入字符串获取全拼

package com.suntek.mobilemeeting.utils;

import java.util.Array

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用Flutter的camera插件来实现Android Studio中调用手机模拟器摄像头实现拍照功能。以下是实现步骤: 1. 在pubspec.yaml文件中添加camera插件依赖: ``` dependencies: camera: ^0.9.4+5 ``` 2. 在Flutter项目中引入camera插件: ``` import 'package:camera/camera.dart'; ``` 3. 初始化摄像头: ``` List<CameraDescription> cameras; Future<void> initCamera() async { cameras = await availableCameras(); // 选择第一个摄像头 final firstCamera = cameras.first; final camera = CameraController( firstCamera, ResolutionPreset.medium, ); await camera.initialize(); setState(() { _camera = camera; }); } ``` 4. 在Flutter页面中显示摄像头预览: ``` @override void initState() { super.initState(); initCamera(); } @override void dispose() { _camera.dispose(); super.dispose(); } @override Widget build(BuildContext context) { return Scaffold( body: _camera == null ? Container() : AspectRatio( aspectRatio: _camera.value.aspectRatio, child: CameraPreview(_camera), ), ); } ``` 5. 实现拍照功能: ``` void takePicture() async { final picturePath = join( (await getTemporaryDirectory()).path, '${DateTime.now()}.png', ); await _camera.takePicture(picturePath); } ``` 6. 在Flutter页面中添加一个按钮,调用takePicture()方法实现拍照: ``` FloatingActionButton( onPressed: takePicture, child: Icon(Icons.camera_alt), ), ``` 完成上述步骤后,您就可以在Android Studio中调用手机模拟器摄像头实现拍照功能了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值