简体   繁体   中英

how to tint a black and white .png image to a certain color in Java

I have a .png image that I need to tint a custom color with the RGB values of 207, 173, 23. ( https://github.com/pret/pokecrystal/blob/master/gfx/tilesets/players_room.png?raw=true )

I did some research and found the following code:

public BufferedImage getBufferedImage(String source, int redPercent, int greenPercent, int bluePercent) throws IOException{
    BufferedImage img = null;
    File f = null;

    try{
        f = new File(source);
        img = ImageIO.read(f);
    }catch(IOException e){
        System.out.println(e);
    }

    int width = img.getWidth();
    int height = img.getHeight();

    for(int y = 0; y < height; y++){
        for(int x = 0; x < width; x++){
            int p = img.getRGB(x,y);

            int a = (p>>24)&0xff;
            int r = (p>>16)&0xff;
            int g = (p>>8)&0xff;
            int b = p&0xff;

            p = (a<<24) | (redPercent*r/100<<16) | (greenPercent*g/100<<8) | (bluePercent*b/100);

            img.setRGB(x, y, p);
        }
    }

    return img;
}

This method is supposed to return a buffered image with the entered RGB values. However, whenever I use it, it only returns a lighter of darker version of the image with no color. I am wondering if the problem lies in the image itself, perhaps having to do with the transparency, or is the problem the code?

The problem is that the PNG image is set up to hold greyscale data only and so the BufferedImage img is also only capable of holding greyscale data. To fix this just create an output BufferedImage in RGB colour mode.

I also tidied up your exception handling.

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


class SOQuestion {
    public static BufferedImage getBufferedImage(String source,
            int redPercent, int greenPercent, int bluePercent) {
        BufferedImage img = null;
        File f = null;

        try {
            f = new File(source);
            img = ImageIO.read(f);
        } catch (IOException e) {
            System.out.println(e);
            return null;
        }

        int width = img.getWidth();
        int height = img.getHeight();
        BufferedImage out = new BufferedImage(width, height,
            BufferedImage.TYPE_INT_RGB);

        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                int p = img.getRGB(x,y);

                int a = (p>>24) & 0xff;
                int r = (p>>16) & 0xff;
                int g = (p>>8) & 0xff;
                int b = p & 0xff;

                p = (a<<24) | (redPercent*r/100<<16) |
                    (greenPercent*g/100<<8) | (bluePercent*b/100);

                out.setRGB(x, y, p);
            }
        }

        return out;
    }

    public static void main(String[] args) {
        BufferedImage result = SOQuestion.getBufferedImage(args[0], 81, 68, 9);
        File outputfile = new File("output.png");
        try {
            ImageIO.write(result, "png", outputfile);
        } catch (IOException e) {
            System.out.println(e);
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM