日期:2014-05-20  浏览次数:20728 次

求助!java图片加水印问题!!!
使用java生成了一张透明的png图片,然后将文字打到图片上,图片就变成一坨黑色了,不透明了,请问有没有人给点建议啊

 public static void textMark(String imgPath, String text, Font font,
             Color color, int x, int y, float alpha) {
     try {
             Font Dfont = (font == null) ? new Font("宋体", 20, 13) : font;

             Image img = ImageIO.read(new File(imgPath));

             BufferedImage image = new BufferedImage(img.getWidth(null), img
                             .getHeight(null), BufferedImage.TYPE_INT_ARGB);
             Graphics2D g = image.createGraphics();

             g.drawImage(img, 0, 0, null);
             g.setColor(color);
             g.setFont(Dfont);
             g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,
                             alpha));
             g.drawString(text, x, y);
             g.dispose();
             FileOutputStream out = new FileOutputStream(imgPath);
             JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
             encoder.encode(image);
             out.close();
     } catch (Exception e) {
             System.out.println(e);
     }
}

------解决方案--------------------
http://my.oschina.net/u/1034481/blog/131194 仅供参考
------解决方案--------------------
你用JPEGImageEncoder对图片编码,得到的是JPG格式的图片,不是你想要的的PNG格式;
JPG格式不支持透明.