1

我正在尝试获取输入图像并使用 Java 中的文本字符串对其进行水印。我有这个程序的 2 个版本。

第一个版本使用字体大小作为相等的间距水平和垂直地遍历图像。

第二个版本创建一个 Rectangle 对象列表,并检查添加到列表中的每个矩形是否与现有矩形相交。如果它确实相交,那么它会随机移动 Rectangle 直到它适合。

我的问题是 - 即使我正在检查现有矩形列表,第二个版本也不能防止重叠文本: if(rectList.get(j).intersects(rect))

任何人都可以帮忙吗?

版本 1

/*
* Author: Hugh Pearse
* Purpose: watermark image
*/
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import java.awt.Font;

public class Prog5 {

    public static void main(String[] args) {

        try {
            int fontSize = 12;
            BufferedImage image = ImageIO.read(new File(args[0]));
            Graphics2D g = (Graphics2D) image.getGraphics();

            AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .3f);
            g.setComposite(ac);
            g.setFont(new Font( "SansSerif", Font.BOLD, fontSize ));
            g.setColor(Color.blue);

            String msg = "©Hugh Pearse";
            for(int i=1; i<=image.getHeight(); i=i+fontSize)
            for(int j=1; j<=image.getWidth(); j=j+(fontSize*msg.length()))
            g.drawString(msg, j, i);

            ImageIO.write(image, "jpg", new File(args[0].toString().replace(".", "_watermarked.")));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

版本 2

/*
 * Author: Hugh Pearse
 * Purpose: Randomly located watermark v3
 */
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.imageio.ImageIO;
import java.awt.Font;

public class Image1 {
    public static void main(String[] args) {
        try {
            Random rand = new Random();

            //variables for controlling visual effect
            int fontSize = 40;
            String msg = "©Hugh Pearse";
            float msgOpacity = .1f;
            String typeface = "SansSerif";
            int style = Font.BOLD;
            Color msgColour = Color.blue;
            int numberOfWatermarks = 16;

            //image objects
            BufferedImage image = ImageIO.read(new File(args[0]));
            Graphics2D g = (Graphics2D) image.getGraphics();

            //apply settings for overlay
            AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, msgOpacity);
            g.setComposite(ac);
            g.setColor(msgColour);

            List<Rectangle> rectList = new ArrayList<Rectangle>();

            for(int i=0; i<=numberOfWatermarks; i++){
                System.out.println("Attempting to draw rectangle " + i);

                //message Rectangle attributes
                Font msgFont = new Font( typeface, style, fontSize );
                g.setFont(msgFont);
                FontMetrics metrics = g.getFontMetrics(msgFont);
                int w = metrics.stringWidth(msg);
                int h = metrics.getHeight();
                int x = 0;
                int y = 0;
                Rectangle rect = new Rectangle(x, y, w, h);

                //randomly choose location for text
                rect.x = rand.nextInt(image.getHeight());
                rect.y = rand.nextInt(image.getWidth());

                if(rectList.size() == 0){
                    rectList.add(rect);
                    g.drawString(msg, rect.x, rect.y);
                }
                else{
                    //compare to every existing rectangle
                    for(int j=0; j<rectList.size(); j++){
                        //check if new rectangle intersects with existing rectangles
                        int attempts = 0;
                        while(attempts <= 100){
                            if (rectList.get(j).intersects(rect)){
                                //change rectangle location if intersects
                                rect.setLocation(rand.nextInt(image.getHeight()), rand.nextInt(image.getWidth()));
                                attempts++;
                            }
                            else{
                                rectList.add(rect);
                                g.drawString(msg, rect.x, rect.y);
                                attempts = 101;
                            }
                        }
                    }
                }
            }

            ImageIO.write(image, "jpg", new File(args[0].toString().replace(".", "_watermarked.")));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
4

0 回答 0