Search code examples
javaimageresizepnggif

Resize image in Java without losing transparency


I want to resize an image (jpg, png, gif) without losing the transparency.

I want to save the resized version to the disk after resizing.

I googled a lot but I only found solutions that lose the transparency and fill the transparent space black...

I am looking for a snippet or a library that does the job :)


Solution

  • This is how i solved my problem:

    public static BufferedImage convertRGBAToIndexed(BufferedImage src) {
        BufferedImage dest = new BufferedImage(src.getWidth(), src.getHeight(), BufferedImage.TYPE_BYTE_INDEXED);
        Graphics g = dest.getGraphics();
        g.setColor(new Color(231, 20, 189));
        g.fillRect(0, 0, dest.getWidth(), dest.getHeight());
        dest = makeTransparent(dest, 0, 0);
        dest.createGraphics().drawImage(src, 0, 0, null);
        return dest;
    }
    
    public static BufferedImage makeTransparent(BufferedImage image, int x, int y) {
        ColorModel cm = image.getColorModel();
        if (!(cm instanceof IndexColorModel))
            return image; // sorry...
        IndexColorModel icm = (IndexColorModel) cm;
        WritableRaster raster = image.getRaster();
        int pixel = raster.getSample(x, y, 0);
        int size = icm.getMapSize();
        byte[] reds = new byte[size];
        byte[] greens = new byte[size];
        byte[] blues = new byte[size];
        icm.getReds(reds);
        icm.getGreens(greens);
        icm.getBlues(blues);
        IndexColorModel icm2 = new IndexColorModel(8, size, reds, greens, blues, pixel);
        return new BufferedImage(icm2, raster, image.isAlphaPremultiplied(), null);
    }
    
    public static Dimension getScaledDimension(Dimension imgSize, Dimension boundary) {
        int original_width = imgSize.width;
        int original_height = imgSize.height;
        int bound_width = boundary.width;
        int bound_height = boundary.height;
        int new_width = 0;
        int new_height = 0;
    
        if (original_width > original_height) {
            new_width = bound_width;
            new_height = (new_width*original_height)/original_width;
        } else {
            new_height = bound_height;
            new_width = (new_height*original_width)/original_height;
        }
    
        return new Dimension(new_width, new_height);
    }
    
    public static void resizeImage(File original_image, File resized_image, int IMG_SIZE) {
        try {
            BufferedImage originalImage = ImageIO.read(original_image);
    
            String extension = Files.getFileExtension(original_image.getName());
    
            int type = extension.equals("gif") || (originalImage.getType() == 0) ? BufferedImage.TYPE_INT_ARGB : originalImage.getType();
    
            Dimension new_dim = getScaledDimension(new Dimension(originalImage.getWidth(), originalImage.getHeight()), new Dimension(IMG_SIZE,IMG_SIZE));
    
            BufferedImage resizedImage = new BufferedImage((int) new_dim.getWidth(), (int) new_dim.getHeight(), type);
            Graphics2D g = resizedImage.createGraphics();
            g.drawImage(originalImage, 0, 0, (int) new_dim.getWidth(), (int) new_dim.getHeight(), null);
            g.dispose();            
    
            if (!extension.equals("gif")) {
                ImageIO.write(resizedImage, extension, resized_image);
            } else {
                // Gif Transparence workarround
                ImageIO.write(convertRGBAToIndexed(resizedImage), "gif", resized_image);
            }
    
        } catch (IOException e) {
            Utils.log("resizeImage", e.getMessage());
        }
    }