2

This is about Flutter, Dart, Image, specifically drawString function.

First of all, what I am trying to do is watermark the picture taken with the text (product name). I mean not just placing text over the image but actually merge it with picture so that the user can submit the picture and share it while sharing the product.

To do so. I found the right function drawString(), but the problem is it only supports arial48, arial24 and arial14 font sizes. Since I have to write the product name in custom fontsize which I'm extracting by calculating the width and height of the original image. Is there any one who tried this before or any who can make it possible. Anyone who knows?

I've tried this.

import 'package:image/image.dart' as img;

img.drawString(originalImage,'$myProductName($pictureTag)',
        x: value,
        y: value,
        color: img.ColorFloat32.rgb(255,255,255), font: img.arial48,
    );

But I need to use custom font size instead of these predefined sizes.

I'm using flutter: 3.19.0 version. and image: ^4.1.7

1 Answer 1

0

Resize image and you can get a different fontSize

if (pickedFile != null) {
        final File imageFile = File(pickedFile.path);
        final imageBytes = imageFile.readAsBytesSync();
        final image = img.decodeImage(imageBytes)!;

        // Resize the image
        final resizedImage = img.copyResize(image, width: 1080, height: 1920);

        final dateTime = DateTime.now().toLocal();
        final watermarkText = '${dateTime.year}-${dateTime.month}-${dateTime.day} ${dateTime.hour}:${dateTime.minute}:${dateTime.second}';
        final whiteColor = 0xFFFFFFFF; // White color in ARGB format
        int size = 40;

        final watermarkImage = img.Image.from(resizedImage);
        img.drawString(
            watermarkImage,
            font: img.arial48,
            y: watermarkImage.height - 120,
            watermarkText,
            color: img.ColorFloat32.rgb(204, 204, 0) // Yellow color
        );

        // Compress image: adjust the quality parameter to control compression
        final watermarkedImageBytes = img.encodeJpg(watermarkImage, quality: 100)!; // Adjust quality as needed
        final watermarkedFile = File('${imageFile.parent.path}/watermarked_${imageFile.uri.pathSegments.last}');
        watermarkedFile.writeAsBytesSync(watermarkedImageBytes);

        onImageSelected(watermarkedFile);
        return watermarkedFile;
      }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.