网上很多文字用到的都很老了,废弃了,我集成之后报很多错,运行不了。
下面是给出自己看最新文档集成的。
1、注册登录Firebase
https://console.firebase.google.com/project/obexclientbase/ml?hl=zh-cn
2、创建Firebase项目绑定 Android应用
3、找到机器学习(查看所有构建功能)
4、进入查看文档
5、收费,选择 独立机器学习套件库
6、集成
项目 build.gradle
plugins {
id 'com.google.gms.google-services' version '4.3.15' apply false
}
app (我用的module) 的 build.gradle
plugins {
// Firebase ML Kit
id 'com.google.gms.google-services'
}
dependencies {
/*implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.8.0'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'*/
// Firebase ML Kit
implementation platform('com.google.firebase:firebase-bom:32.7.0')
// TODO: Add the dependencies for Firebase products you want to use
// When using the BoM, don't specify versions in Firebase dependencies
// https://firebase.google.com/docs/android/setup#available-libraries
// Firebase ML Kit 实现文字识别功能
// To recognize Chinese script
implementation 'com.google.mlkit:text-recognition-chinese:16.0.0'
}
如果你也用的 module ,会报错:No client found ...
这是因为你在Firebase添加的项目包名与你当前module namespace不一样,把namespace改成Firebase那个
package com.project.imagerecognizelib;
import android.graphics.Bitmap;
import android.graphics.Point;
import android.graphics.Rect;
import android.util.Log;
import androidx.annotation.NonNull;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
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 TextRecognizeManager {
private TextRecognizer textRecognizer;
public TextRecognizeManager() {
// When using Chinese script library
textRecognizer = TextRecognition.getClient(new ChineseTextRecognizerOptions.Builder().build());
}
public void recognize(Bitmap bitmap) {
InputImage image = InputImage.fromBitmap(bitmap, 0);
Task<Text> result = textRecognizer.process(image)
.addOnSuccessListener(new OnSuccessListener<Text>() {
@Override
public void onSuccess(Text visionText) {
handleVisionText(visionText);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.d("", "识别失败");
e.printStackTrace();
}
});
}
private void handleVisionText(Text result) {
String resultText = result.getText();
for (Text.TextBlock block : result.getTextBlocks()) {
String blockText = block.getText();
Point[] blockCornerPoints = block.getCornerPoints();
Rect blockFrame = block.getBoundingBox();
for (Text.Line line : block.getLines()) {
String lineText = line.getText();
Log.d("", "识别文本: " + lineText);
/*Point[] lineCornerPoints = line.getCornerPoints();
Rect lineFrame = line.getBoundingBox();
for (Text.Element element : line.getElements()) {
String elementText = element.getText();
Point[] elementCornerPoints = element.getCornerPoints();
Rect elementFrame = element.getBoundingBox();
for (Text.Symbol symbol : element.getSymbols()) {
String symbolText = symbol.getText();
Point[] symbolCornerPoints = symbol.getCornerPoints();
Rect symbolFrame = symbol.getBoundingBox();
}
}*/
}
}
}
}
找个地方测试一下就可以了
private var textRecognizer: TextRecognizeManager? = null
private fun test() {
val bitmap = BitmapFactory.decodeResource(resources, R.drawable.test)
if (textRecognizer == null)
textRecognizer = TextRecognizeManager()
textRecognizer?.recognize(bitmap)
}
网友评论