0

文字列の画像を表示する Web エンドポイントがあります... 次のコードを (Tomcat で) 実行すると、OSX のタスクバーに Java アイコンが生成されます。それが問題なのか、何が起こっているのかわからない。なんらかの説明を求めて

@RequestMapping("/text/{text}")
public void textImage(HttpServletResponse response, @PathVariable("text") String text){
    response.setContentType("image/png");

    try{
        OutputStream os = response.getOutputStream();

        BufferedImage bufferedImage = new BufferedImage( (text.length()*10) , 14, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2d = bufferedImage.createGraphics();
        g2d.setBackground(Color.WHITE);
        g2d.setPaint(Color.BLACK);
        Font font = new Font("sansserif", Font.PLAIN, 12);
        g2d.setFont(font);
        g2d.drawString(text, 0, 12);

        ImageIO.write(bufferedImage, "png", os);
    } catch(Exception e) {
        // nothing we can do, simply log the error
        logger.error("Could not draw string: ", e);
    }
}
4

1 に答える 1

1

デフォルトでは、グラフィックスを使用すると、ウィンドウサーバー接続が得られます(実際に画面にレンダリングしているかどうかは関係ありません)。ヘッドレスモードを使用して回避できます。

于 2010-05-23T18:34:58.943 に答える