flutter为图片右下角添加上水印
//制造字符串水印图片
_makeStrMarkImage(String str, double fontSize) async {
final picRecorder = ui.PictureRecorder();
final paragraphBuilder = ui.ParagraphBuilder(
ui.ParagraphStyle(
textAlign: TextAlign.left,
fontSize: fontSize,
),
);
paragraphBuilder.pushStyle(ui.TextStyle(
fontSize: fontSize,
color: Colors.white,
shadows: <Shadow>[
Shadow(
color: Color(0xFFF4F4F4).withAlpha(25),
blurRadius: 1.0,
offset: Offset(-3, 0),
),
Shadow(
color: Color(0xFFF4F4F4).withAlpha(25),
blurRadius: 3.0,
offset: Offset(-3, -3),
),
],
));
paragraphBuilder.addText(str);
final paragraph = paragraphBuilder.build()
..layout(ui.ParagraphConstraints(
width: fontSize * 10.0,
));
final lineMetrics = paragraph.computeLineMetrics();
double width = 0;
lineMetrics.forEach((element) {
if (element.width > width) {
width = element.width;
}
});
final cvs = Canvas(
picRecorder,
Rect.fromLTRB(0, 0, width, paragraph.height,),
);
cvs.drawParagraph(paragraph, Offset(0, 0));
final pic = picRecorder.endRecording();
final waterMark = await pic.toImage(
width.toInt(),
paragraph.height.toInt(),
);
return waterMark;
}
//确定水印位置、大小,并将水印添加到原图
_makeImageMark(img.Image image, String markStr,img.Image imageLogo,[bool isLeft = true]) async {
final imgHeight = image.height;
final imgWidth = image.width;
final ratio = imgWidth / 375.0;
final fontSize = ratio * 24.0 ~/ 2.0;
final edgeSize = ratio * 8.0 ~/ 2.0;
final waterMarkImage = await _makeStrMarkImage(markStr, fontSize.toDouble());
final waterMarkBytes = await waterMarkImage.toByteData(format: ui.ImageByteFormat.rawRgba);
final waterMarkImg = img.Image.fromBytes(
waterMarkImage.width,
waterMarkImage.height,
Uint8List.sublistView(waterMarkBytes),
);
final resizedImage = img.copyResize(imageLogo,height: edgeSize * 3);
int dstX;
int dstY = imgHeight - waterMarkImg.height - edgeSize;
if (isLeft) {
dstX = edgeSize;
} else {
dstX = imgWidth - edgeSize - waterMarkImg.width;
}
img.copyInto(
image,
waterMarkImg,
dstX: dstX,
dstY: dstY,
);
img.copyInto(
image,
resizedImage,
dstX: dstX - (edgeSize * 7),
dstY: dstY + edgeSize,
);
}
getDecodeImage(Uint8List uInt8List){
Completer<img.Image> completer = new Completer<img.Image>();
img.Image originImage = img.decodeImage(uInt8List);
completer.complete(originImage);
return completer.future;
}
/*
* 图片左、右下脚添加图片+文字水印
* */
Future<String> addWatermarkToImage(Uint8List uInt8List,{Uint8List markUInt8List,String markUserName}) async {
File targetFile;
if (uInt8List != null) {
final markLogo = await getDecodeImage(markUInt8List);
final originImage = await getDecodeImage(uInt8List);
final Directory _directory = await getTemporaryDirectory();
final Directory _imageDirectory = await new Directory('${_directory.path}/image/').create(recursive: true);
String _targetPath = _imageDirectory.path;
await _makeImageMark(
originImage,
markUserName==null?''
:'@$markUserName',
markLogo, false);
final bytes = img.writeJpg(originImage);
targetFile = File('${_targetPath}watermark${DateTime.now().millisecondsSinceEpoch}.jpg');
targetFile.writeAsBytesSync(bytes);
print('====targetFilePath=${targetFile.path}');
}
return Future.value(targetFile.path);
}
调用
//图片添加水印
addWatermarkToImages(BuildContext context) async{
waterImages.clear();
if(addWaterMark && state.selectImageList.length>0){
ByteData byteData;
for (int i = 0; i < state.selectImageList.length; i++) {
Asset asset = state.selectImageList[i];
byteData = await asset.getByteData();
String watermarkImagePath =
await Utils().addWatermarkToImage(byteData.buffer.asUint8List(),
markUInt8List: watermarkUint8List,markUserName: userInfo.user.nickname);
if(watermarkImagePath != null && watermarkImagePath.isNotEmpty){
waterImages.add(watermarkImagePath);
print('==waterImages length=$i=${waterImages.length}');
}
}
//发布
submit(context);
update();
}
}