在flutter中将图像转换为GIF



我正在制作一个程序,可以拍摄一些照片,然后将它们转换为GIF,然后显示出来我在这里使用了解决方案,但我不能使它工作,我得到一些令人沮丧的错误。

Image.memory( Uint8List.fromList( generateGIF(images)! ), )

错误提示我不能将图像列表放入图像的iterable

ide也在两个Image类之间感到沮丧,因为我使用的是Image Package所以我把包命名为imageGen,我不知道我是否在使用它,写在这个函数

List<int>? generateGIF(Iterable<imageGen.Image> images) {
final imageGen.Animation animation = imageGen.Animation();
for(imageGen.Image image in images) {
animation.addFrame( image );
}
return imageGen.encodeGifAnimation(animation);
}

这是我的代码

import 'dart:typed_data';
import 'package:deaf_helper/logic/gif_generator.dart';
import 'package:flutter/material.dart';
import 'package:image/image.dart' as imageGen ;
import 'package:speech_to_text/speech_recognition_result.dart';
import 'package:speech_to_text/speech_to_text.dart';
import '../constants.dart';
class FaceToFaceTab extends StatefulWidget {
@override
_FaceToFaceTabState createState() => _FaceToFaceTabState();
}
class _FaceToFaceTabState extends State<FaceToFaceTab> {
final SpeechToText _speechToText = SpeechToText();
TextEditingController textEditingController = TextEditingController() ;
List images = [] ;
GifGenerator generator = GifGenerator();
bool start = false;
@override
void initState() {
_initSpeech();
super.initState();
}
void _initSpeech() async {
await _speechToText.initialize();
setState(() {});
}
void _startListening() async {
await _speechToText.listen(
onResult: _onSpeechResult,
localeId: language,
);
setState(() {});
}
void _stopListening() async {
await _speechToText.stop();
setState(() {});
}
void _onSpeechResult(SpeechRecognitionResult result) {
setState(() {
textEditingController.text = result.recognizedWords;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: [
color1,
color2,
],
),
),
child: SafeArea(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
SingleChildScrollView(
child: Container(
padding: EdgeInsets.all(48),
alignment: Alignment.center,
child: start ? Image.memory( Uint8List.fromList( generateGIF(images)! ), ) : isArabic ? Text('اضغط على زر الأرسال') : Text('Press the send button') ,
),
),
Container(
color: Colors.white,
child: Row(
children: [
Expanded(
flex: 1,
child: IconButton(onPressed: () {
setState((){
textEditingController.text = '';
});
},
icon: Icon(Icons.close_outlined, color: color2,),
),),
Expanded(
flex: 11,
child: Padding(
padding: const EdgeInsets.only(left: 8.0, right: 8.0),
child: TextFormField(
textAlign: isArabic? TextAlign.right : TextAlign.left,
controller: textEditingController,
decoration: InputDecoration(
hintText: isArabic ? 'قم بالتسجيل او ابدأ الكتابه' : 'Start recording or start typing',
),
),
),
),
Expanded(
flex: 2,
child: IconButton(
onPressed: () {
start = true;
getSentencePictures(textEditingController.text);
},
icon: Icon(Icons.send,size: 28, color: color2,),
),
),
Expanded(
flex: 2,
child: getMicIconBtn(),
),
],
),
),
],
),
),
),
);
}
Widget getMicIconBtn() {
return Padding(
padding: const EdgeInsets.only(bottom: 4.0),
child: IconButton(
onPressed:
_speechToText.isNotListening ? _startListening : _stopListening,
tooltip: 'Record',
icon: Icon(
_speechToText.isListening ? Icons.mic : Icons.mic_none,
size: 40,
color: color2,
),
),
);
}
List<int>? generateGIF(Iterable<imageGen.Image> images) {
final imageGen.Animation animation = imageGen.Animation();
for(imageGen.Image image in images) {
animation.addFrame( image );
}
return imageGen.encodeGifAnimation(animation);
}
getSentencePictures(String sentence){
for(int x = 0 ; x < sentence.length ; x++){
switch(sentence[x]){
case 'a' :
case 'A' : images.add(AssetImage('assets/sign-lang/a.png'),);
break;
case 'b' :
case 'B' : images.add(AssetImage('assets/sign-lang/b.png'),);
break;
case 'c' :
case 'C' : images.add(AssetImage('assets/sign-lang/c.png'),);
break;
case 'd' :
case 'D' : images.add(AssetImage('assets/sign-lang/d.png'),);
break;
case 'e' :
case 'E' : images.add(AssetImage('assets/sign-lang/e.png'),);
break;
case 'f' :
case 'F' : images.add(AssetImage('assets/sign-lang/f.png'),);
break;
case 'g' :
case 'G' : images.add(AssetImage('assets/sign-lang/g.png'),);
break;
case 'h' :
case 'H' : images.add(AssetImage('assets/sign-lang/h.png'),);
break;
case 'i' :
case 'I' : images.add(AssetImage('assets/sign-lang/i.png'),);
break;
case 'j' :
case 'J' :images.add(AssetImage('assets/sign-lang/j.png'),);
break;
case 'k' :
case 'K' : images.add(AssetImage('assets/sign-lang/k.png'),);
break;
case 'l' :
case 'L' : images.add(AssetImage('assets/sign-lang/l.png'),);
break;
case 'm' :
case 'M' : images.add(AssetImage('assets/sign-lang/m.png'),);
break;
case 'n' :
case 'N' : images.add(AssetImage('assets/sign-lang/n.png'),);
break;
case 'o' :
case 'O' : images.add(AssetImage('assets/sign-lang/o.png'),);
break;
case 'p' :
case 'P' : images.add(AssetImage('assets/sign-lang/p.png'),);
break;
case 'q' :
case 'Q' : images.add(AssetImage('assets/sign-lang/q.png'),);
break;
case 'r' :
case 'R' : images.add(AssetImage('assets/sign-lang/r.png'),);
break;
case 's' :
case 'S' : images.add(AssetImage('assets/sign-lang/s.png'),);
break;
case 't' :
case 'T' : images.add(AssetImage('assets/sign-lang/t.png'),);
break;
case 'u' :
case 'U' : images.add(AssetImage('assets/sign-lang/u.png'),);
break;
case 'v' :
case 'V' : images.add(AssetImage('assets/sign-lang/v.png'),);
break;
case 'w' :
case 'W' : images.add(AssetImage('assets/sign-lang/w.png'),);
break;
case 'x' :
case 'X' : images.add(AssetImage('assets/sign-lang/x.png'),);
break;
case 'y' :
case 'Y' : images.add(AssetImage('assets/sign-lang/y.png'),);
break;
case 'z' :
case 'Z' : images.add(AssetImage('assets/sign-lang/z.png'),);
break;
case '0' : images.add(AssetImage('assets/sign-lang/zero.png'),);
break;
case '1' : images.add(AssetImage('assets/sign-lang/one.png'),);
break;
case '2' : images.add(AssetImage('assets/sign-lang/two.png'),);
break;
case '3' : images.add(AssetImage('assets/sign-lang/three.png'),);
break;
case '4' : images.add(AssetImage('assets/sign-lang/four.png'),);
break;
case '5' : images.add(AssetImage('assets/sign-lang/five.png'),);
break;
case '6' : images.add(AssetImage('assets/sign-lang/six.png'),);
break;
case '7' : images.add(AssetImage('assets/sign-lang/seven.png'),);
break;
case '8' : images.add(AssetImage('assets/sign-lang/eight.png'),);
break;
case '9' : images.add(AssetImage('assets/sign-lang/nine.png'),);
break;
case 'ا' :
case 'أ' : images.add(AssetImage('assets/sign-lang/أ.png'),);
break;
case 'ب' : images.add(AssetImage('assets/sign-lang/ب.png'),);
break;
case 'ت' : images.add(AssetImage('assets/sign-lang/ت.png'),);
break;
case 'ث' : images.add(AssetImage('assets/sign-lang/ث.png'),);
break;
case 'ج' : images.add(AssetImage('assets/sign-lang/ج.png'),);
break;
case 'ح' : images.add(AssetImage('assets/sign-lang/ح.png'),);
break;
case 'خ' :images.add(AssetImage('assets/sign-lang/خ.png'),);
break;
case 'د' :images.add(AssetImage('assets/sign-lang/د.png'),);
break;
case 'ذ' : images.add(AssetImage('assets/sign-lang/ذ.png'),);
break;
case 'ر' : images.add(AssetImage('assets/sign-lang/ر.png'),);
break;
case 'ز' : images.add(AssetImage('assets/sign-lang/ز.png'),);
break;
case 'س' : images.add(AssetImage('assets/sign-lang/س.png'),);
break;
case 'ش' : images.add(AssetImage('assets/sign-lang/ش.png'),);
break;
case 'ص' : images.add(AssetImage('assets/sign-lang/ص.png'),);
break;
case 'ض' : images.add(AssetImage('assets/sign-lang/ض.png'),);
break;
case 'ط' : images.add(AssetImage('assets/sign-lang/ط.png'),);
break;
case 'ظ' : images.add(AssetImage('assets/sign-lang/ظ.png'),);
break;
case 'ع' : images.add(AssetImage('assets/sign-lang/ع.png'),);
break;
case 'غ' : images.add(AssetImage('assets/sign-lang/غ.png'),);
break;
case 'ف' : images.add(AssetImage('assets/sign-lang/ف.png'),);
break;
case 'ق' : images.add(AssetImage('assets/sign-lang/ق.png'),);
break;
case 'ك' : images.add(AssetImage('assets/sign-lang/ك.png'),);
break;
case 'ل' : images.add(AssetImage('assets/sign-lang/ل.png'),);
break;
case 'م' : images.add(AssetImage('assets/sign-lang/م.png'),);
break;
case 'ن' : images.add(AssetImage('assets/sign-lang/ن.png'),);
break;
case 'ه' :
case 'ة' : images.add(AssetImage('assets/sign-lang/ه.png'),);
break;
case 'و' : images.add(Image.asset('assets/sign-lang/و.png'),);
break;
case 'ي' :
case 'ى' : images.add(AssetImage('assets/sign-lang/ي.png'),);
break;
case 'ئ' : images.add(Image.asset('assets/sign-lang/ئ.png'),);
break;
default : images.add(Image.asset('assets/app_logo.png'),);
}
}
}
}

将iterable改为list

List<int>? generateGIF(List<imageGen.Image> images) {
final imageGen.Animation animation = imageGen.Animation();
for(imageGen.Image image in images) {
animation.addFrame( image );
}
return imageGen.encodeGifAnimation(animation);
}

相关内容

  • 没有找到相关文章

最新更新