# flutter - 浮水印(使用套件 image ) | 圖片浮水印 | 文字浮水印 | 滿版浮水印 | | -------- | -------- | -------- | |![](https://i.imgur.com/FqEaZBY.jpg) | ![](https://i.imgur.com/jso72bQ.jpg) | ![](https://i.imgur.com/aBkwQVM.jpg)| ## 安裝套件 ``` flutter pub add image ``` ## 程式碼 ### 使用方式 ```dart //添加 assets 內的圖片浮水印 WaterMark.addImg( oriFilePath: file.path, waterMarkAssetsPath: 'images/yen.png', waterMarkHeight: 100, waterMarkWidth: 100 ); //添加浮水印文字 WaterMark.addText( oriFilePath: file.path, waterMarkText: 'photo by YEN', ); //添加滿版浮水印 WaterMark.addFullText( oriFilePath: file.path, waterMarkText: 'photo by YEN', ); ``` ### 小工具 ```dart! import 'dart:io'; import 'package:flutter/services.dart'; import 'package:image/image.dart'; import 'package:path_provider/path_provider.dart'; class WaterMark { static Future<File> addImg({ required String oriFilePath, required String waterMarkAssetsPath, required int waterMarkHeight, required int waterMarkWidth, int bottomPadding = 10, int rightPadding = 10, }) async { File oriFile = File(oriFilePath); File waterMarkFile = await _getImageFileFromAssets(waterMarkAssetsPath); Uint8List oriUint8List = oriFile.readAsBytesSync(); Image? oriImg = decodeJpg(oriUint8List); Image? waterMarkImg = decodePng(waterMarkFile.readAsBytesSync()); drawImage(oriImg!, waterMarkImg!, dstW: waterMarkWidth, dstH: waterMarkHeight, dstX: oriImg.width - waterMarkWidth - rightPadding, dstY: oriImg.height - waterMarkHeight - bottomPadding); return await oriFile.writeAsBytes(encodeJpg(oriImg)); } static Future<Future<File>> addText({ required String oriFilePath, required String waterMarkText, int bottomPadding = 10, int rightPadding = 10, }) async { File oriFile = File(oriFilePath); Image? oriImg = decodeJpg(oriFile.readAsBytesSync()); drawString(oriImg!, arial_24, (oriImg.width - rightPadding), oriImg.height - bottomPadding - 24, waterMarkText, rightJustify: true); return oriFile.writeAsBytes(encodeJpg(oriImg)); } static Future<Future<File>> addFullText({ required String oriFilePath, required String waterMarkText, int horizontalPadding = 10, int verticalPadding = 40, }) async { File oriFile = File(oriFilePath); Image? oriImg = decodeJpg(oriFile.readAsBytesSync()); int fontSize = 24; for (var j = 0, countj = 0; j < oriImg!.height - fontSize * 2; j++) { j = j + verticalPadding; for (var i = 0, counti = 0; i < oriImg.width - horizontalPadding * 2; i++) { i = i + horizontalPadding; if (countj % 2 == 0 && counti % 2 == 0 || countj % 2 == 1 && counti % 2 == 1) { drawString(oriImg, arial_24, i, j, waterMarkText, color: 0x80ffffff); } i = i + waterMarkText.length * ((fontSize / 2).ceil()); counti = counti + 1; } j = j + verticalPadding; countj = countj + 1; } return oriFile.writeAsBytes(encodeJpg(oriImg)); } static Future<File> _getImageFileFromAssets(String path) async { final byteData = await rootBundle.load('assets/$path'); final file = File('${(await getTemporaryDirectory()).path}/$path'); if (!file.existsSync()) { await file.create(recursive: true); } await file.writeAsBytes(byteData.buffer .asUint8List(byteData.offsetInBytes, byteData.lengthInBytes)); return file; } } ``` ###### tags: `flutter`