当我触发pickimage功能时,它会弹出图库视图以选择图像。当我选择图像时,应用程序返回到应用程序视图。但 pickimage 函数不会返回,并且永远挂起。这是我的代码:
import 'package:flutter/material.dart';
import 'dart:async';
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:google_ml_kit/google_ml_kit.dart';
import 'package:image_picker/image_picker.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Text Scanning Demo'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
late String result;
File? _image;
InputImage? inputImage;
final ImagePicker picker = ImagePicker();
Future pickImageFromGallery() async {
print("starting get image");
final XFile? pickedFile = await picker.pickImage(source: ImageSource.gallery);
//final pickedFile = await picker.pickImage(source: ImageSource.gallery);
print("getting image.....");
setState(() {
if (pickedFile != null) {
print("file not null");
_image = File(pickedFile.path);
inputImage = InputImage.fromFilePath(pickedFile.path);
imageToText(inputImage);
} else {
print('No image selected.');
}
});
}
Future captureImageFromCamera() async {
final pickedFile = await picker.pickImage(source: ImageSource.camera);
setState(() {
if (pickedFile != null) {
_image = File(pickedFile.path);
inputImage = InputImage.fromFilePath(pickedFile.path);
imageToText(inputImage);
} else {
print('No image selected.');
}
});
}
Future imageToText(inputImage) async {
print("starting");
result = '';
final textDetector = GoogleMlKit.vision.textDetector();
print("loaded textDetector");
final RecognisedText recognisedText = await textDetector.processImage(inputImage);
print("loaded recognisedText");
setState(() {
String text = recognisedText.text;
for (TextBlock block in recognisedText.blocks) {
//each block of text/section of text
final String text = block.text;
print("block of text: ");
print(text);
for (TextLine line in block.lines) {
//each line within a text block
for (TextElement element in line.elements) {
//each word within a line
result += element.text + " ";
}
}
}
result += "\n\n";
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
width: 100,
child: TextField(
textAlign: TextAlign.center,
style: TextStyle(color: Colors.black),
),
),
Container(
height: 70,
width: 150,
child: TextButton(
style: TextButton.styleFrom(
primary: Colors.blue,
),
onPressed: () {
pickImageFromGallery();
},
child: Text('Pick Image'),
))
],
),
),
// This trailing comma makes auto-formatting nicer for build methods.
);
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的 pubspec.yaml
name: text_scanning
description: A new Flutter project.
publish_to: 'none' # Remove this line if you wish to publish to pub.dev
version: 1.0.0+1
environment:
sdk: ">=2.14.0-0 <3.0.0"
dependencies:
flutter:
sdk: flutter
font_awesome_flutter: ^9.0.0
splash_screen_view: ^3.0.0
cupertino_icons: ^1.0.2
google_ml_kit: ^0.7.3
image_picker: ^0.8.1+1
dev_dependencies:
flutter_test:
sdk: flutter
flutter_lints: ^1.0.0
flutter:
uses-material-design: true
Run Code Online (Sandbox Code Playgroud)
这是 Podfile
# Uncomment this line to define a global platform for your project
platform :ios, '10.0'
# CocoaPods analytics sends network stats synchronously affecting flutter build latency.
ENV['COCOAPODS_DISABLE_STATS'] = 'true'
project 'Runner', {
'Debug' => :debug,
'Profile' => :release,
'Release' => :release,
}
def flutter_root
generated_xcode_build_settings_path = File.expand_path(File.join('..', 'Flutter', 'Generated.xcconfig'), __FILE__)
unless File.exist?(generated_xcode_build_settings_path)
raise "#{generated_xcode_build_settings_path} must exist. If you're running pod install manually, make sure flutter pub get is executed first"
end
File.foreach(generated_xcode_build_settings_path) do |line|
matches = line.match(/FLUTTER_ROOT\=(.*)/)
return matches[1].strip if matches
end
raise "FLUTTER_ROOT not found in #{generated_xcode_build_settings_path}. Try deleting Generated.xcconfig, then run flutter pub get"
end
require File.expand_path(File.join('packages', 'flutter_tools', 'bin', 'podhelper'), flutter_root)
flutter_ios_podfile_setup
target 'Runner' do
use_frameworks!
use_modular_headers!
flutter_install_all_ios_pods File.dirname(File.realpath(__FILE__))
pod 'GoogleMLKit/TextRecognition'
pod 'Firebase'
end
post_install do |installer|
# add these lines:
installer.pods_project.build_configurations.each do |config|
config.build_settings["EXCLUDED_ARCHS[sdk=*]"] = "armv7"
config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = $iOSVersion
end
installer.pods_project.targets.each do |target|
flutter_additional_ios_build_settings(target)
# add these lines:
target.build_configurations.each do |config|
if Gem::Version.new($iOSVersion) > Gem::Version.new(config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'])
config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = $iOSVersion
end
end
end
end
Run Code Online (Sandbox Code Playgroud)
这是我来自 xcode 的信息的屏幕截图:[注意画廊和相机的隐私][1]
我在这里束手无策,我遵循了每个文档,它甚至没有产生任何错误,只是挂在那里。[1]: https: //i.stack.imgur.com/XC6aP.png
您是否在基于 ARM 的 Mac (M1 / M1 Pro) 上的 iOS 模拟器上运行此代码?如果是这样,那么问题可能源于 iOS 模拟器中的当前错误,该错误似乎仅在使用 Rosetta 翻译运行时才会重现(请在此处阅读更多信息和此处)。
在您的情况下,该pubspec.yaml文件声明google_ml_kit为依赖项,这会强制应用程序使用 Rosetta 运行,从而破坏插件image_picker。
Apple 可能会在未来某个时间修复此问题,但与此同时,您可以通过在物理 iOS 设备上运行应用程序或使用iOS 版本 < 14 的 iOS 模拟器来解决此问题。
为此:
然后:
| 归档时间: |
|
| 查看次数: |
1792 次 |
| 最近记录: |