Skip to main content

APIzation: Replication Package

[Q#10095182][A#10095840] How to remove unnecessary black background after resizing the image to a fixed resolution?

https://stackoverflow.com/q/10095182

I have been resizing every image to a fix resolution before adding it to my database . for this purpose i have been using FileUpload and following code:- I get the image of the desired resolution but there is a unnecessary black background which i am unable to remove. image before : http://www.rocketcampus.com/images/test.png image after : http://static.appcanvas.com/1334085929080.png

Answer

https://stackoverflow.com/a/10095840

You can change the type of your BufferedImage to BufferedImage.TYPE_INT_ARGB to have a transparent background. This worked for me:

APIzation

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);

}
package com.stackoverflow.api;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import javax.imageio.ImageIO;

public class Human10095840 {

  public static void main() 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);
  }
}

package com.stackoverflow.api;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import javax.imageio.ImageIO;

/**
 * How to remove unnecessary black background after resizing the image to a fixed resolution?
 *
 * @author APIzator
 * @see <a href="https://stackoverflow.com/a/10095840">https://stackoverflow.com/a/10095840</a>
 */
public class APIzator10095840 {

  public static void removeBackground() 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);
  }
}