Date and Time Image Watermark in Flutter

121 Views Asked by At

I want to use watermark when i use camera. i want the watermark adding automatically when i click shutter button. the watermark is show time and date in photos. what library in pub.dev that i can use and how to fix the code so it can show watermark when i click camera button?

this is my libraries :

import 'package:image_picker/image_picker.dart';
import 'dart:async';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:image/image.dart' as img;
import 'package:intl/intl.dart';

this is my code :

String _imageFileName = '';

Future<void> _captureImage() async {
    final pickedFile = await ImagePicker().pickImage(
      source: ImageSource.camera,
    );

    if (pickedFile != null) {
      final now = DateTime.now();
      final formatter = DateFormat('dd/MM/yyyy HH:mm');
      final timestamp = formatter.format(now);

      final originalImage = img.decodeImage(File(pickedFile.path).readAsBytesSync());

      final img.Image watermarkedImage = img.copyResize(originalImage!, width: originalImage.width, height: originalImage.height);

      final textColor = img.getColor(0, 0, 0);
      final fontSize = 20;

      img.drawString(watermarkedImage, img.arial_24, 10, originalImage.height - 30, 'Date: $timestamp', color: textColor, fontSize: fontSize);

      final path = (await getTemporaryDirectory()).path;
      final imagePath = '$path/image_$timestamp.jpg';

      File(imagePath).writeAsBytesSync(img.encodeJpg(watermarkedImage));

      setState(() {
        _imageFileName = imagePath;
      });
    }
  }

this is my camera button :

Padding(
                padding: const EdgeInsets.symmetric(horizontal: 20.0),
                child: GestureDetector(
                  onTap: _captureImage,
                  child: Container(
                    height: 50,
                    width: double.infinity,
                    decoration: BoxDecoration(
                      color: Colors.transparent,
                      border: Border.all(color: Colors.black),
                      borderRadius: BorderRadius.circular(8),
                    ),
                    child: _imageFileName != ''
                        ? Padding(
                      padding: const EdgeInsets.all(0.0),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Text(_imageFileName),
                        ],
                      ),
                    )
                        : const Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Icon(
                          CupertinoIcons.photo_camera,
                          size: 20,
                        ),
                        SizedBox(width: 10.0),
                        Text(
                          'Ambil Foto',
                          style: TextStyle(
                            color: Colors.black,
                            fontSize: 14,
                            fontWeight: FontWeight.w400,
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
              ),
0

There are 0 best solutions below