import 'dart:io'/span>;
import 'package:google_ml_kit/google_ml_kit.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
class ImageNote extends StatefulWidget{
static String id='image-note';
@override
_ImageNoteState createState() => _ImageNoteState()。
}
class _ImageNoteState extends State< ImageNote> {
String result = ""/span>;
late檔案影像。
ImagePicker imagePicker = ImagePicker();
captureFromGallery() async {
XFile? pickedFile =
await imagePicker.pickImage(source: ImageSource.gallery)。
image = File(pickFile!.path);
setState(() {
影像。
//D do the extract text from Image 從影像中提取文本。
textFromImage()。
});
}
captureFromCamera() async {
XFile? pickedFile =
await imagePicker.pickImage(source: ImageSource.camera)。
image = File(pickFile!.path);
setState(() {
影像。
textFromImage()。
});
}
textFromImage() async {
// final FirebaseVisionImage firebaseVisionImage =
//FirebaseVisionImage.fromFile(image);.
final InputImage inputImage = InputImage.fromFile(image)。
final TextDetector textDetector = GoogleMlKit.vision.textDetector()。
final RecognisedText recognisedText = await textDetector.processImage(inputImage)。
// final TextRecognizer recognizer = FirebaseVision.instance.textRecognizer();
// VisionText visionText = await recognizer.processImage(firebaseVisionImage);
result = "";
setState(() {
for (TextBlock block in recognisedText.block) {
for (TextLine line in block.lines) {
for (TextElement element in line.elements) {
result = element.text " ";
}
}
結果 = "
"。
}
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
體。容器(
裝飾。BoxDecoration(
影像。裝飾圖片(
影像。AssetImage('assets/background.png')。
適合。BoxFit.cover)。)
),
孩子。列(
兒童。[
SizedBox(width: 100.0) 。
/Result Container。
容器(
高度。280.0。
width: 250.0,
margin: EdgeInsets.only(top: 70.0)。
padding: EdgeInsets.only(left: 28.0, bottom: 5.0, right: 18.0)。)
孩子。SingleChildScrollView(
孩子。容器(
margin: EdgeInsets.only(top: 50.0)。
孩子。Padding(
padding: EdgeInsets.all(12.0)。
孩子。文本(
結果。
風格。TextStyle(fontSize: 16.0)。
textAlign: TextAlign.justify,
),
),
),
),
裝飾。BoxDecoration(
影像。裝飾圖片()
影像。AssetImage('assets/note.png')。
適合。BoxFit.cover。
),
),
),
容器(
margin: EdgeInsets.only(top: 20.0, right: 140.0)。)
孩子。堆疊(
兒童。[
堆疊(
兒女。[
中心(
孩子。Image.asset(
'assets/output.jpg'。
高度。240.0。
width: 240.0,
),
),
],
),
中心(
孩子。TextButton(
onPressed: () {
captureFromGallery()。
},
onLongPress: () {
captureFromCamera();
},
孩子。容器(
margin: EdgeInsets.only(top: 25.0)。
child: image != null ?
? Image.file(
影像。
width: 140.0,
高度。192.0。
適合。BoxFit.fill。
)
: 容器(
寬度:240.0。
高度。200.0。
孩子。圖示(
Icons.camera_front,
大小。100.0,
color: Colors.gray,
),
),
),
),
),
],
),
),
],
),
),
);
}
}
這是我的代碼,我得到了以下錯誤,我甚至不確定這到底是關于哪張圖片的錯誤:
以下是構建ImageNote(dirty: _ImageNoteState#8b383)時拋出的LateError。
在構建ImageNote(dirty, state: _ImageNoteState#8b383)時拋出了以下LateError。 LateInitializationError: 欄位'image'還沒有被初始化。
我已經找遍了所有的地方......所以,如果有人能幫助我的話!任何幫助都是巨大的!
我已經找遍了所有的地方。
uj5u.com熱心網友回復:
這里是錯誤的地方
class _ImageNoteState extends State< ImageNote> {
String result = ""/span>;
late檔案影像;// Here
ImagePicker imagePicker = ImagePicker()。
發生這種情況是因為你告訴flutter你將在以后初始化檔案,而它沒有檢測到它。
另一種方法是通過添加以下內容使其成為空值。
像這樣
class _ImageNoteState extends State< ImageNote> {
String result = ";
檔案? image;//this is better。
ImagePicker imagePicker = ImagePicker()。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/332744.html
標籤:
