flutter - 是否可以在 Flutter 中的图像文件上绘制文本?
问题描述
这是我一直在使用的代码,这个问题有时是在 iOS 中,文本会出现在图像之外并剪切。我希望文本在右下角的图像中正确换行。
PS 这不是 UI 问题...我想在图像文件上绘制文本并将其用于进一步的目的,而不仅仅是在 UI 上。
Future<File?> drawTextOnImage() async {
final ImagePicker _picker = ImagePicker();
// Capture a photo
final image = await _picker.pickImage(
source: ImageSource.camera,
maxHeight: 366,
maxWidth: 550,
imageQuality: 50);
final position = await locationManager.getCurrentPosition(
desiredAccuracy: LocationAccuracy.high);
if (image != null) {
var decodeImg = UIImage.decodeImage(File(image.path).readAsBytesSync());
final TextToDraw =
'Lat: ${position.latitude}\nLong:${position.longitude}';
var stringWidth = 10;
var stringHeight = 10;
final chars = TextToDraw.codeUnits;
for (var c in chars) {
if (!UIImage.arial_14.characters.containsKey(c)) {
continue;
}
final ch = UIImage.arial_14.characters[c]!;
stringWidth += ch.xadvance;
if (ch.height + ch.yoffset > stringHeight) {
stringHeight = ch.height + ch.yoffset;
}
}
if (decodeImg != null) {
UIImage.drawString(
decodeImg,
UIImage.arial_14,
decodeImg.width - stringWidth,
decodeImg.height - stringHeight,
TextToDraw);
var encodeImage = UIImage.encodeJpg(decodeImg, quality: 100);
var finalImage = File(image.path)..writeAsBytesSync(encodeImage);
return finalImage;
}
}
helper.ToastFlutter('Problem Fetching Image, Please Try again');
return null;
}
解决方案
推荐阅读
- sqlite - 如何编译 sqlite3 扩展 - CSV 虚拟表
- python - 如何计算 Pyspark 中 None 或 NaN 值的百分比?
- sql - 寻找一个基于变量“年”为我提供所有工作日(星期一,星期二...)的函数
- javascript - Amcharts 饼图工具提示位置
- path - 尽管路径是合法的,但 OpenSolutionAsync 因“路径中的非法字符”而崩溃
- oracle - 使用 while 循环将值插入创建的表中
- python - 如何使用 Python 读取 json 文件夹数据?
- python-3.x - 如何在特定日期从 Google Analytics API 获取非抽样数据
- git - 如何还原未提交的修改?
- c++ - 将 boost mapped_matrix 与 std::unique_ptr 一起使用有什么限制?