繁体   English   中英

Java Graphics2D drawString正在“涂黑”源图像

[英]Java Graphics2D drawString is “blacking out” the source image

Java 8和Mac OS(High Sierra)在这里。 我有以下类TextOverlayer ,它从文件系统读取图像,并且需要在该图像上覆盖一些红色文本,然后将该“覆盖的”图像另存为文件系统上的另一个文件:

public class TextOverlayer implements ImageObserver {

  public static void main(String[] args) throws IOException {

      // Instantiate a TextOverlayer and read a source/input image from disk
      TextOverlayer textOverlayer = new TextOverlayer();
      BufferedImage bufferedImage = ImageIO.read(Paths.get("/User/myuser/pix/sourceImage.jpg").toFile());

      // Lay some text over the image at specific coordinates
      BufferedImage drawn = textOverlayer.drawText(bufferedImage, "Some text");

      // Write the overlayed image to disk
      File outputfile = new File("/User/myuser/pix/targetImage.jpg");
      ImageIO.write(drawn, "jpg", outputfile);

  }

  private BufferedImage drawText(BufferedImage old, String text) {

      int w = old.getWidth() / 3;
      int h = old.getHeight() / 3;
      BufferedImage img = new BufferedImage(
              w, h, BufferedImage.TYPE_INT_ARGB);
      Graphics2D g2d = img.createGraphics();
      g2d.drawImage(old, 0, 0, w, h, this);
      g2d.setPaint(Color.red);
      g2d.setFont(new Font("Serif", Font.BOLD, 20));
      FontMetrics fm = g2d.getFontMetrics();
      int x = img.getWidth() - fm.stringWidth(text) - 5;
      int y = fm.getHeight();
      g2d.drawString(text, x, y);
      g2d.dispose();

      return img;

  }

  @Override
  public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) {
      return false;
  }

}

运行此命令时,不会抛出任何错误,并且targetImage.jpg已成功写入磁盘,除了只是一个小黑框的图像。 我希望targetImage.jpgsourceImage.jpg ,只是在所需的坐标处(在图像内)添加了一些额外的文本。

有什么想法我要去哪里吗?

您不需要ImageObserver,并且可能无法通过附带的编写器将带有alpha通道的图像写入JPEG文件。

这有效:

public class TextOverlayer {

    public static void main(String[] args) throws IOException {

        // Instantiate a TextOverlayer and read a source/input image from disk
        TextOverlayer textOverlayer = new TextOverlayer();
        BufferedImage bufferedImage = ImageIO.read(Paths.get("/User/myuser/pix/sourceImage.jpg").toFile());

        // Lay some text over the image at specific coordinates
        BufferedImage drawn = textOverlayer.drawText(bufferedImage, "Some text");

        // Write the overlayed image to disk
        File outputfile = new File("/User/myuser/pix/targetImage.jpg");
        boolean result = ImageIO.write(drawn, "jpg", outputfile);
        if (!result) {
            System.out.println("FAILED");
        }
    }

    private BufferedImage drawText(BufferedImage old, String text) {

        int w = old.getWidth();
        int h = old.getHeight();
        BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2d = img.createGraphics();
        g2d.drawImage(old, 0, 0, w, h, null);
        g2d.setPaint(Color.red);
        g2d.setFont(new Font("Serif", Font.BOLD, 20));
        FontMetrics fm = g2d.getFontMetrics();
        int x = img.getWidth() - fm.stringWidth(text) - 5;
        int y = fm.getHeight();
        g2d.drawString(text, x, y);
        g2d.dispose();

        return img;

    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM