Skip to content
Advertisement

Can’t create an image file in Java

What I need to do is I have a input image file and I need to change it as user’s parameters. For example if user wants to make it %30 darker, first I get all pixels with their RGB values and store them in an 2D array. Sample array given below.

114 121 140     //Pixel 1
114 121 140     //Pixel 2
114 121 140     //Pixel 3
     .
     .
     .
50 57 83        //Pixel 2073601

After that I overwrite that RGB values (in our case if RGB values are 10:10:10, new values will be 7:7:7). Everything from that point is ok. But now I’m facing some difficulties about creating my output.jpg file using my new array of information. When I run my function, it does not creates any output.jpg file. Here is my function.

public static void createFinalImage(int height, int width, String[][] rgbArray, String outputFile) {
        BufferedImage img;
        img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        File f;

        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                String[] data = rgbArray[y][x].split(" ");
                int red = Integer.parseInt(data[0]);
                int green = Integer.parseInt(data[1]);
                int blue = Integer.parseInt(data[2]);

                int rgb = new Color(red,green,blue).getRGB();
                img.setRGB(x,y,rgb);
            }
        }
        try
        {
            f = new File(outputFile);
            ImageIO.write(img, "jpg", f);
        }
        catch(IOException e)
        {
            System.out.println("Error: " + e);
        }
    }

I cant understand what is the problem and why I cant get darker image using this function. (I know there is always easier and basic ways to do it but this is an multithreading assignment and I must do it like I explained.) If anyone help me, I would be appreciated.

Advertisement

Answer

The ImageIO.write(...) methods return a boolean indicating whether or not there is a plugin installed that can write the image in the given format. It’s good practice to check this value. While there is always a JPEG plugin installed, in recent versions of Java the JPEG plugin no longer supports images with alpha channel. Most other software don’t support 4 channel RGBA JPEGs anyway, so it’s not a big loss…

All you need is to change BufferedImage.TYPE_INT_ARGB to a type without alpha, like BufferedImage.TYPE_INT_RGB or TYPE_3BYTE_BGR, and things will work. You don’t seem to use the alpha channel anyway.

The important changes:

BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

for (int y = 0; y < height; y++) {
    for (int x = 0; x < width; x++) {
       // Manipulate pixels
    }
}

try {
    File f = new File(outputFile);
    if (!ImageIO.write(img, "JPEG", f)) {
        System.err.println("No plugin to write " + img + " in JPEG format to " + f);
    }
}
catch (IOException e) {
    System.out.println("Error: " + e);
}
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement