flutter 水印相机实现,图片编辑
2023-03-26 本文已影响0人
H_vv
1 效果图
WechatIMG5.jpeg WechatIMG6.jpeg WechatIMG7.jpeg2 需要插件
/// 相机
camera: ^0.10.3
///图片保存
image_gallery_saver: ^1.7.1
///权限
permission_handler: ^10.2.0
///代码tool
common_utils: ^2.1.0
flustars: ^2.0.1
///状态管理
get: ^4.6.5
///获取水印当前位置
flutter_bmflocation: ^3.3.0
///通知
event_bus: ^2.0.0
///获取路径
path_provider: ^2.0.1
///图片
image: ^3.0.0
///签名
screenshot: ^1.2.3
2代码实现
import 'dart:io';
import 'package:base_tool/dc_photos/fk_photos.dart';
import 'package:path_provider/path_provider.dart';
import 'camera_page_state.dart';
/// 拍照
void takePicture({required CameraPageState state,callBack}) async {
if (state.cameraController == null || state.cameraController!.value.isTakingPicture) return;
XFile file = await state.cameraController!.takePicture();
state.curFile = file;
if(callBack != null){
callBack();
}
}
/// 切换闪光灯
void toggleFlash({required CameraPageState state}) {
if (state.cameraController == null) return;
switch (state.cameraController!.value.flashMode) {
case FlashMode.auto:
state.cameraController!.setFlashMode(FlashMode.off);
state.flashImagePath = "images/water/flash_off.png";
break;
case FlashMode.off:
state.cameraController!.setFlashMode(FlashMode.torch);
state.flashImagePath = "images/water/flash_on.png";
break;
case FlashMode.always:
// state.cameraController!.setFlashMode(FlashMode.torch);
// state.flashImagePath = "";
// break;
case FlashMode.torch:
state.cameraController!.setFlashMode(FlashMode.auto);
state.flashImagePath = "images/water/flash_auto.png";
break;
}
}
void toggleCameraLens( CameraPageState state,callBack) {
final lensDirection = state.cameraController?.description.lensDirection;
CameraDescription newDescription;
if (lensDirection == CameraLensDirection.front) {
newDescription =
state.availableCameras!.firstWhere((description) => description
.lensDirection == CameraLensDirection.back);
callBack(newDescription);
}
else {
newDescription =
state.availableCameras!.firstWhere((description) => description
.lensDirection == CameraLensDirection.front);
callBack(newDescription);
}
}
/// 生成临时文件
Future<String> generateCameraTempFilePath() async {
final Directory tempDir = await getTemporaryDirectory();
final Directory imgTempDir = Directory('${tempDir.path}/IMG_TEMP');
if (!imgTempDir.existsSync()) {
await imgTempDir.create();
}
final String fileName = "${DateTime.now().millisecondsSinceEpoch}.png" ;
return '${imgTempDir.path}/$fileName';
}