watermark ok

This commit is contained in:
2026-01-14 21:50:40 +01:00
parent fa466d2dfc
commit ce68049d5d
2 changed files with 70 additions and 24 deletions

View File

@@ -19,6 +19,11 @@
<artifactId>ij</artifactId>
<version>1.51h</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.15.1</version>
</dependency>
</dependencies>
</project>

View File

@@ -2,38 +2,34 @@ package org.example;
import ij.IJ;
import ij.ImagePlus;
import ij.process.ImageProcessor;
import ij.process.ImageConverter;
import org.apache.commons.io.FilenameUtils;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
/*
TODO:
- Parse properly the CLI arguments
- Add error handling
- Write many lines with an angle
- Write for any image (format & responsiveness)
- optional: Export to PDF?
*/
public class Main {
public static void main(String[] args) {
if (args.length == 0)
{
System.out.println("Usage: java -jar watermark.jar <input file> -s <string> [-o <output file>]");
System.out.println("librewatmark - libre watermark tool\nUsage: <input file> -s <string> [-o <output file>]");
System.exit(0);
}
String filename = null;
File f = null;
String watermark = null;
String outputfile = null;
for (int i=0; i<args.length; i++)
{
System.out.println("Argument " + i + ": " + args[i]);
//System.out.println("Argument " + i + ": " + args[i]);
if (i == 0) {
// First argument is input file, if not, gtfo here
String filename = args[i];
filename = args[i];
f = new File(filename);
if (!f.exists() || f.isDirectory()) {
System.out.println("Input file \"" + filename + "\" does not exist.");
@@ -45,10 +41,12 @@ public class Main {
case '-':
switch (args[i].charAt(1)) {
case 's': // -s string: to be put as watermark
System.out.println("OPTION B");
watermark = args[i+1];
i++;
break;
case 'o': // -o file: output file
System.out.println("OPTION C");
outputfile = args[i+1];
i++;
break;
default:
System.out.println("Unknown argument: \"" + args[i] + "\"");
@@ -59,15 +57,58 @@ public class Main {
}
}
// test
ImagePlus image = IJ.openImage(f.getAbsolutePath());
Font font = new Font("Arial", Font.PLAIN, 18);
ImageProcessor ip = image.getProcessor();
ip.setColor(Color.WHITE);
ip.setFont(font);
ip.drawString("Watermark Text", 10, 20);
image.updateAndDraw();
IJ.save(image, "output.png");
if (outputfile == null)
{
outputfile = FilenameUtils.removeExtension(filename) + "-watermark";
}
if (watermark == null)
{
System.out.println("No watermark text provided.");
System.exit(1);
}
ImagePlus image = IJ.openImage(f.getAbsolutePath());
if (image.getType() != ImagePlus.COLOR_RGB) {
ImageConverter ic = new ImageConverter(image);
ic.convertToRGB();
}
BufferedImage bufferedImage = image.getBufferedImage();
Graphics2D g2d = bufferedImage.createGraphics();
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
Font font = new Font("Arial", Font.PLAIN, 18);
g2d.setFont(font);
g2d.setColor(new Color(0, 0, 0, 180));
double angle = Math.toRadians(-45);
g2d.rotate(angle,
bufferedImage.getWidth() / 2.0,
bufferedImage.getHeight() / 2.0);
FontMetrics fm = g2d.getFontMetrics();
int textWidth = fm.stringWidth(watermark);
int textHeight = fm.getHeight();
for (int y = -bufferedImage.getHeight(); y < bufferedImage.getHeight() * 2; y += textHeight * 4) {
for (int x = -bufferedImage.getWidth(); x < bufferedImage.getWidth() * 2; x += textWidth + 50) {
g2d.drawString(watermark, x, y);
}
}
g2d.dispose();
ImagePlus out = new ImagePlus("watermarked", bufferedImage);
if (!outputfile.matches(".*\\.(png|jpg|jpeg|tif|tiff)$")) {
outputfile += ".png";
}
IJ.save(out, outputfile);
System.out.println("Watermarked file saved as \"" + outputfile + "\"");
}
}