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.

JavaScript

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.

JavaScript

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:

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement