您可以在以下文件路径的“图像”包中找到将 .ttf 转换为 .zip 的步骤。
lib ==> src ==> bitmap_font.dart
可以与 [drawString] 和 [drawChar] 函数一起使用的位图字体。
如果您想使用自己的字体,请按照以下步骤操作:
1. Get your .ttf file - important is to select file with specific style which you want
for example when you download .ttf file from google fonts: select file from /static folder
example name: Roboto-Black.ttf
2. Convert ttf file to fnt zip with this link: "https://ttf2fnt.com/" and dont forgot to mention required font size like 40px, 60px.
3. Download zip file and drag to your project assets folder and mention path in .yaml file
4. Call below method whereever you want :
const int kYellowColorIntValue = 0xffffff00;
const int kCyanColorIntValue = 0xff00ffff;
void getWaterMarkTextOnImage() async
{
var decodeImg = img.decodeImage(<your origianl photo UInt8List>);
ByteData fBitMapFontData = await loadAssetFont();
img.BitmapFont requiredRobotoFont = img.BitmapFont.fromZip(fBitMapFontData.buffer.asUint8List());
// IMPORTANT NOTE : Assiged color is Cyan. But required color is Yellow.
// It is taking yellow as cyan and cyan as yellow.
// set X, Y axis as per your requirement
img.drawString(decodeImg!, requiredRobotoFont, 80, decodeImg.height - 200,
requiredLatLongString!,
color: kCyanColorIntValue);
img.drawString(decodeImg, requiredRobotoFont, 80, decodeImg.height - 130,
requiredTimeStampString!,
color: kCyanColorIntValue); // KYellowColorIntValue
var encodeImage = img.encodeJpg(decodeImg, quality: 100);
// Take finalImageFile global variable and assign to image widget
File? finalImageFile = File(<your image captured path>)..writeAsBytesSync(encodeImage);
}
Future<ByteData> loadAssetFont() async
{
ByteData imageData = await rootBundle.load(kRobotoBitmapZipFilePath);
setState(() {});
return imageData;
}
5. Create Image widget here.
Widget getCapturedPhotoWidget()
{
double aspRatio, imgHeight = 0.0;
if (<captured pic is portrait mode>) {
// portrait
aspRatio = (1 / widget.overlay.ratio!);
imgHeight = finalImageSize!.width.toDouble();
} else {
// landscape
aspRatio = widget.overlay.ratio! * 1.5;
imgHeight = finalImageSize!.height.toDouble() / 5;
}
return Center(
child: Container(
height: imgHeight, // finalImageSize!.width.toDouble(),
margin: EdgeInsets.only(left: 20, right: 20, top: 20, bottom: 10),
child: ClipRRect(
borderRadius: BorderRadius.circular(16.0),
child: AspectRatio(
aspectRatio: aspRatio, // widget.overlay.ratio!,
child: finalImageFile != null
? Image.file(
finalImageFile!,
fit: BoxFit.fill,
)
: Offstage(),
),
)),
);
}
这就是使用位图的自定义字体
编码快乐!!!