1

在将每个图像添加到我的数据库之前,我一直在将其调整为固定分辨率。为此,我一直在使用 FileUpload 和以下代码:-

logo_name = System.currentTimeMillis() + ".png";
            File  uploadedFile = new File("/www/static.appcanvas.com/"+logo_name);
            BufferedImage bi = ImageIO.read(item.getInputStream()); 
            Image img = bi.getScaledInstance(125,125,Image.SCALE_SMOOTH);
            BufferedImage img_logo = new BufferedImage(125,125,BufferedImage.TYPE_INT_RGB);
            Graphics2D g = img_logo.createGraphics();
            g.drawImage(img,0,0,null); 
            if(g != null) g.dispose();
            ImageIO.write(img_logo,"png",uploadedFile)

我得到了所需分辨率的图像,但有一个我无法删除的不必要的黑色背景。前图:http ://www.rocketcampus.com/images/test.png 后图: http: //static.appcanvas.com/1334085929080.png

4

1 回答 1

3

您可以将您的类型更改BufferedImageBufferedImage.TYPE_INT_ARGB具有透明背景。

这对我有用:

public static void main(String[] args) throws IOException {
    FileInputStream item = new FileInputStream("D:/tmp/OpenFlexo_07.gif");
    String logo_name = System.currentTimeMillis() + ".png";
    File uploadedFile = new File("d:/www/static.appcanvas.com/" + logo_name);
    BufferedImage bi = ImageIO.read(item);
    Image img = bi.getScaledInstance(125, 125, Image.SCALE_SMOOTH);
    BufferedImage img_logo = new BufferedImage(125, 125, BufferedImage.TYPE_INT_RGB);
    Graphics2D g = img_logo.createGraphics();
    g.drawImage(img, 0, 0, null);
    if (g != null) {
        g.dispose();
    }
    uploadedFile.getParentFile().mkdirs();
    uploadedFile.createNewFile();
    ImageIO.write(img_logo, "png", uploadedFile);

}
于 2012-04-10T20:17:29.703 回答