0

There is some not a silly issue.. Let's say, we have a lot of only black colored png icons (over hundred), but they may consist of more than one unconnected elements like:

enter image description here enter image description here

What I want to achieve is to make set of colors and automatically color(fill black content) all those graphics, and leave the rest parameters like unchanged(format/resolution). Is there any library or approach that should be applied to perform this task?

Thanks

CJ Jacobs
  • 299
  • 1
  • 16
Jacob
  • 14,949
  • 19
  • 51
  • 74
  • 1
    You could do two for loops (x and y of image) and apply on each pixel: ­­­­`color = currentPixel.getARGB() | 0xffff0000;`. This would change all black into red. You could wrap this function into another loop for each images you have to deal with. – Jazzwave06 Oct 17 '14 at 22:57
  • Hope android will have reasonable performance for hundreds png's. – Jacob Oct 18 '14 at 10:40
  • Shouldn't you pre-compute the pngs before deployment? – Jazzwave06 Oct 19 '14 at 03:10
  • What you mean by "pre-compute" ? – Jacob Oct 19 '14 at 10:57
  • Can you explain to me what are you trying to achieve? Maybe I haven't understood well, however, it would be way faster to precompute the images into red before installing the app on the client. If you need many colors, I would pre-compute the some (16) colors for each pngs, and I would include them in the final package. If the color is supplied by the client as a customization, maybe provide a web service that will return all pngs in the given color? – Jazzwave06 Oct 19 '14 at 15:46

1 Answers1

1

Here's a little procedure that will do it:

public static void main(String[] args) {
    final String directoryPath = "C:\\images";
    final String outputPath = "C:\\images\\out";
    final int color = 0x00ff0000;
    File directory = new File(directoryPath);
    File[] files = directory.listFiles();

    if (files == null) {
        return;
    }

    for (File file : files) {
        String extension;

        int extensionIndex = file.getName().lastIndexOf('.');
        if (extensionIndex > 0) {
            extension = file.getName().substring(extensionIndex + 1);
        } else {
            extension = "bmp";
        }

        BufferedImage image;
        try {
            image = convert(ImageIO.read(file), BufferedImage.TYPE_INT_ARGB);
            for (int i = 0; i < image.getWidth(); i++) {
                for (int j = 0; j < image.getHeight(); j++) {
                    image.setRGB(i, j, image.getRGB(i, j) | color);
                }
            }

            File newFile = new File(outputPath + "\\" + file.getName());
            ImageIO.write(image, extension, newFile);
        } catch (IOException e) {
            // Handle
        }
    }
}

I used the convert method of this post: How to convert between color models

Community
  • 1
  • 1
Jazzwave06
  • 1,883
  • 1
  • 11
  • 19