第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Java Graphics2D drawString 正在“遮蔽”源圖像

Java Graphics2D drawString 正在“遮蔽”源圖像

慕田峪9158850 2023-07-19 15:44:46
Java 8 和 Mac OS (High Sierra) 在這里。我有以下類TextOverlayer,它從文件系統(tǒng)讀取圖像,并且需要將一些紅色文本覆蓋到圖像上,然后將該“覆蓋”圖像保存為文件系統(tǒng)上的不同文件: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;  }}當(dāng)它運行時,不會拋出任何錯誤,并且targetImage.jpg會成功寫入磁盤,除了它只是一個小黑匣子的圖像。我本來期望targetImage.jpg與 完全相同sourceImage.jpg,只是在所需的坐標(biāo)(圖像內(nèi))添加了一些額外的文本。有什么想法我會出錯嗎?
查看完整描述

1 回答

?
斯蒂芬大帝

TA貢獻(xiàn)1827條經(jīng)驗 獲得超8個贊

您不需要 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;


    }

}


查看完整回答
反對 回復(fù) 2023-07-19
  • 1 回答
  • 0 關(guān)注
  • 172 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號