ML Kit
是一个由Google开发的跨平台移动SDK(Android和iOS)。它带来了谷歌的端上机器学习能力。
ML Kit所有在端上运行的API允许实时和离线功能。这也意味着这些功能在离线模式下也可用。
ML Kit
可以识别超过100种语言的文本,包括原生拼写及罗马转写系统,比如汉语、俄语、印地语、英语、希腊语等。点击查看支持语言的完整列表
注意
此 API 需要 Android API 级别 21 或更高版本。确保应用的 build file 使用值 21 或更高。minSdkVersion
Google
的Maven
存储库。build.gradlebuildscriptallprojects本文讲的是中文识别,所以只导入了中文依赖包,你可以根据自己所需选择导入的依赖包
// To recognize Chinese script
implementation 'com.google.mlkit:text-recognition-chinese:16.0.0'
因为本文是中文,所以使用的中文脚本库
ChineseTextRecognizerOptions build = new ChineseTextRecognizerOptions.Builder().build();
TextRecognizer recognizer = TextRecognition.getClient(build);
图像来源有:
本文用的是使用Bitmap,如下图
InputImage image = InputImage.fromBitmap(bitmap, 0);
将图像传递给方法:process
recognizer.process(image)
.addOnSuccessListener(new OnSuccessListener<Text>() {
@Override
public void onSuccess(Text visionText) {
// Task completed successfully
// ...
}
})
.addOnFailureListener(
new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
// Task failed with an exception
// ...
}
});
别忘了导入依赖包!!!
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/btn"
android:text="拍照"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/tv_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
package com.example.mlkitapplication;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.mlkit.vision.common.InputImage;
import com.google.mlkit.vision.text.Text;
import com.google.mlkit.vision.text.TextRecognition;
import com.google.mlkit.vision.text.TextRecognizer;
import com.google.mlkit.vision.text.chinese.ChineseTextRecognizerOptions;
public class MainActivity extends AppCompatActivity {
private static final int REQUEST_CAMERA_PERMISSION = 1;
private static final int REQUEST_IMAGE_CAPTURE = 1;
private TextView tv_result;
private ImageView image;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv_result=findViewById(R.id.tv_result);
image=findViewById(R.id.image);
//权限检查
requestCameraPermission();
@SuppressLint({"MissingInflatedId", "LocalSuppress"}) Button btn=findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dispatchTakePictureIntent();//调本地相机
}
});
}
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
private void requestCameraPermission() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.CAMERA},
REQUEST_CAMERA_PERMISSION);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
image.setImageBitmap(imageBitmap);
// 在这里使用imageBitmap进行文字识别
recognizeTextFromBitmap(imageBitmap);
}
}
private void recognizeTextFromBitmap(Bitmap bitmap) {
InputImage image = InputImage.fromBitmap(bitmap, 0);
ChineseTextRecognizerOptions build = new ChineseTextRecognizerOptions.Builder().build();
TextRecognizer recognizer = TextRecognition.getClient(build);
recognizer.process(image)
.addOnSuccessListener(new OnSuccessListener<Text>() {
@Override
public void onSuccess(com.google.mlkit.vision.text.Text text) {
String text1 = text.getText();
tv_result.setText(text1);
recognizer.close();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
// 处理错误
e.printStackTrace();
// 释放资源
recognizer.close();
}
});
}
}