Flutter image⬅️➡️Mysql.blob⬅️➡️Lazarus image
我最近编写Flutter3.7图像数据的Mysql Blob字段保存方法,以及在十余年前的Lazarus1.0.14上显示图像,在网上查了多天,以为后来者借鉴,现将代码贡献如下:
flutter 图像数据上传到Mysql数据库Blob字段类型中
//对图像文件进行Mysql.Blob编码
//imagePath!为图像文件的全路径名称
Uint8List uint8byte = File(imagePath!).readAsBytesSync();
//插入客户图形数据表
sqltext ='insert into blobtable values(?)';
await connimage.query(sqltext,[uint8byte]);
flutter 从Mysql数据库Blob字段类型中取出数据还原成图像
//选取客户图形数据表
sqltext = 'select image from blobtable;';
var result=await connimage.query(sqltext);
//对Mysql.Blob编码转换为图像
bytesImage = Uint8List.fromList((result.first.values![1] as mysql.Blob).toBytes());
setState(() {});
图像显示代码:Image.memory(bytesImage!,fit: BoxFit.fitWidth)
Lazarus从Mysql的Blob字段读取数据生成图像文件
SQLQuery0.Close;
SQLQuery0.SQL.Text:='SELECT image FROM blobtable";';
SQLQuery0.Open;
Stream:=TMemoryStream.Create;
Jpg:=TjpegImage.Create;
TBlobField(SQLQuery0.FieldByName('image')).SaveToStream(Stream);//显示的转换为BlobField并保存到内存流
Stream.Position:=0;
jpg.LoadFromStream(Stream);//加载图片
image1.Picture.Assign(Jpg);
Stream.Free;
Jpg.Free;
Lazarus图像文件保存到Mysql的Blob字段
Stream:=TMemoryStream.Create;//创建内存流
Image1.Picture.Graphic.SaveToStream(Stream);//将图片保存到内存流中//
SQLQuery0.Close;
SQLQuery0.SQL.Clear;
sqltext:='insert into blobtable value(:photo);';
SQLQuery0.SQL.Text:=sqltext;
SQLQuery0.ParamByName('photo').LoadFromStream(Stream,ftBlob);//读取保存的内存图
SQLQuery0.ExecSQL;
Stream.Free;//释放内存流