[英]google_ml_kit - how to force download of text recognition module at compile?
我是 Flutter 的新手,试图编写一个简单的模块来选择图像并将其传递给 google_ml_kit 以仅用于设备上的 OCR 文本识别。 我已将 Android min SDK 版本设置为 21 并更新了 build.gradle 以使用 FileNotFoundException。 图像选取工作正常,但是当我将选取的图像文件传递给 ml_kit 函数时,出现一组错误,似乎表明 OCR 模块丢失并需要下载。 有没有办法强制在编译时发生这种情况?
import 'dart:async';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:image_picker/image_picker.dart';
import 'package:google_ml_kit/google_ml_kit.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return const MaterialApp(
title: 'Image Selector with OCR',
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key ?key}) : super(key: key);
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
File? txtImage;
String? imageString;
Future getImage()async{
try {
final image = await ImagePicker().pickImage(source: ImageSource.gallery);
if (image == null) return;
final imageFile = File(image.path);
final ocrText = await readText(imageFile);
setState(()=>this.imageString = ocrText);
setState(()=>this.txtImage = imageFile);
} on PlatformException catch(e){print('error'+e.message.toString());}
}
Future readText(_file)async{
try {
final inputImage = InputImage.fromFile(_file);
final textDetector = GoogleMlKit.vision.textDetector();
final RecognisedText recognisedText = await textDetector.processImage(
inputImage);
print(recognisedText);
return (recognisedText);
} on PlatformException catch(e){print(e.message.toString());}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Reading Assistant'),
),
body: Center(child: Column(mainAxisAlignment: MainAxisAlignment.center,
children: [
txtImage != null ? Row(mainAxisAlignment: MainAxisAlignment.spaceEvenly,children: [
Image.file(txtImage!),Text('OCR:')]) : Text('select an image'),
],
)),
floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
floatingActionButton: Stack(
fit: StackFit.expand,
children: [
Positioned(
right: 20,
bottom: 10,
child: FloatingActionButton(
heroTag: 'pick',
onPressed: () {getImage();},
child: Icon(
Icons.photo_album_outlined,
size: 40,
),
),
),
Positioned(
bottom: 10,
right: 90,
child: FloatingActionButton(
heroTag: 'camera',
onPressed: () {/* TODO implement camera later */},
child: Icon(
Icons.camera,
size: 40,
),
),
),
],
),
);;
}
}
W/DynamiteModule(8758):找不到 com.google.android.gms.vision.ocr 的本地模块描述符类。 I/DynamiteModule(8758):考虑本地模块 com.google.android.gms.vision.ocr:0 和远程模块 com.google.android.gms.vision.ocr:0 E/Vision (8758):加载模块 com 时出错.google.android.gms.vision.ocr 可选模块 true:gv:未找到可接受的模块。 本地版本为0,远程版本为0。 I/Vision ( 8758): Request download for engine ocr D/skia (8758): Shader compiler error D/skia (8758): ----------- ------------- D/skia(8758):错误:D/skia(8758):D/skia(8758):着色器编译错误D/skia(8758):---- -------------------- D/skia ( 8758): Errors: D/skia ( 8758): D/skia ( 8758): Shader compiler error D/skia ( 8758):------------------------ D/skia(8758):错误:D/skia(8758):D/skia(8758):着色器编译错误 D/skia (8758): ------------------------ D/skia (8758): Errors: D/skia (8758): d / TransportRuntime.SQLiteEventStore(8758):优先= VERY_LOW,名称= FIREBASE_ML_SDK存储事件目的地CCT d / TransportRuntime.JobInfoScheduler(8758):上传上下文TransportContext(CCT,VERY_LOW,MSRodHRwczovL2ZpcmViYXNlbG9nZ2luZy5nb29nbGVhcGlzLmNvbS92MGNjL2xvZy9iYXRjaD9mb3JtYXQ9anNvbl9wcm90bzNc)已经被调度。 回到... d / TransportRuntime.SQLiteEventStore(8758):/存储优先级= VERY_LOW,名称= FIREBASE_ML_SDK事件目的地CCT d TransportRuntime.JobInfoScheduler(8758):上传上下文TransportContext(CCT,VERY_LOW,MSRodHRwczovL2ZpcmViYXNlbG9nZ2luZy5nb29nbGVhcGlzLmNvbS92MGNjL2xvZy9iYXRjaD9mb3JtYXQ9anNvbl9wcm90bzNc)已经被调度。 返回... I/flutter (8758): com.google.mlkit.common.MlKitException: 等待文本识别模块下载。 请稍等。
正如您在此链接https://developers.google.com/ml-kit/migration/android#new 中看到的那样,只有测试版文本识别可以运行,无需下载任何东西。 所以我鼓励你将 google_ml_kit 包放在你的 flutter 项目的本地,并将文本识别实现替换为
实现 'com.google.mlkit:text-recognition:16.0.0-beta1'
那么该错误将不会出现在 toast 消息中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.