Java: change alpha of BufferedImage with a polygon - java

I have a buffered image which has a set alpha and would like to change the alpha of the buffered image but only in a specific polygon.
For example the buffered image has an alpha of 200 and i want to add 20 to all the pixels on the buffered image within(or colliding with) the polygon.
I couldn't find any which does this exactly in the graphics class or on Google.
I would appreciate any form of help :D
Thanks!

Related

Creating image of Triangle with given co-ordinates

I want to image file containing polygon with given coordinates with white color inside the polygon and black color outside of given size. Size of the image and co-ordinates of the polygon are given. How can I write java code to output the image file. Will there be any inbuilt functions? Thank you in advance.
Create a Polygon and use the contains(int x, int y) inside a loop to figure out which pixels should be white and which should be black. Create a BufferedImage, and use createGraphics() to draw the correct colors to the image. Finally, check How to save a BufferedImage as a File for help with outputting the image file.

JAI Add Alpha Channel to RenderedImage

I have two RenderedImages. I want to do an Overlay Operation with these two images and therefore they need to match in data type and the number of bands.
The problem I have is that one image has 3 bands (RGB) and the second image has 4 bands (ARGB).
My question is how can I add an Alpha Channel to the first image so I can do the Overlay Operation?
EDIT
Ok, I found a method of adding an Alpha Channel to the first image. Below is the code. I simply created a single banded constant image and merged it with my first image.
ParameterBlock pb = new ParameterBlock();
pb.add(new Float(finalImage.getWidth())).add(new Float(finalImage.getHeight()));
pb.add(new Byte[] {new Byte((byte)0xFF)});
RenderedImage alpha = JAI.create("constant", pb);
finalImage = BandMergeDescriptor.create(finalImage, alpha, null);
The problem I have now is that everytime I add an overlay the image changes colors. All the colors become nuances of red or pink. When I add a second overlay, the image becomes normal again, but the first overlay changes colors. All black areas become white.
Also the background of the overlay is not transparent. It is grey.
Below are examples of the images, so you see how the change colors:
As you can see, the picture and overlays change colors and the background of the overlay isn't transparent.
Can you help me solve this problem, so that the image is always displayed correctly? Thanks!
You can try to create a new BufferedImage with ARGB-model, and just paint the non-transparent background picture into this new BufferedImage. Then you have a BufferedImage with alpha-channel (although all of the pixels are opaque), so the Composition should hopefully work.
im not sure about TYPE_4BYTE_ARGB as I usually work with BufferedImages of TYPE_INT_ARGB but I have often used the method of drawing a RGB BufferedImage to a new ARGB BufferedImage and then drawing that onto other things without problem. the change in color would suggest an unwanted change is being made to the other channels in the overlay process as it doesn't seem to be specific to a specific image. If your overlay operation is similar to drawing one image onto another with alpha I would probably suggest using the Graphics.drawImage()/drawRenderedImage() method for the overlay itself not to mention the background should not even need alpha in this case.
the code:
public RenderedImage overlay(RenderedImage back, RenderedImage front, AffineTransform overlayTransformation)
{
BufferedImage newBack = new BufferedImage(back.getWidth(), back.getHeight(), TYPE_3BYTE_RGB);
newBack.setData(back.getData());
Graphics2D graphics = (Graphics2D)(newBack.getGraphics());
graphics.drawRenderedImage(front, overlayTransformation);
return newBack;
}
you may want to ensure the original back Raster is not modified though.

how to add transparent alpha channel back to png image in java?

I am trying to attach the transparent alpha channel to png image in java. Image should not be changed after merging of alpha channel.
In short,
I have png image of dimension p X q
I have alpha channel(transparent) of 100% transparency and of size p X q
Above both images(1 and 2) should be merged and image 1 should remain as it is without any effect of alpha channel...
Please help me out...
You need to set the alpha channel in the first image according to the transparency information in the second image. Something like this: Set BufferedImage alpha mask in Java
If you don't want to have any changes in the first image, then you can create a copy/clone of it and work with this copy. Here's how to create a copy of a BufferedImage: How do you clone a BufferedImage

Simple image rotation

I am making a game in Java. I made a planet seen from outer space and I want to make it appear like the planet is slowly rotating. But I don't know how to rotate a image. I need a simple command that rotates my image 1 degree around its own center once. Any help?
This is what I want to do:
Image
Take a look at these tutorials:
Java2D: Have Fun With Affine Transform
Coordinate Translations and Rotations: Example Code
Transforming Shapes, Text, and Images
What you are describing is not rotating an image, but changing an image to represent a 3D rotation of the object in the image.
Ideally you wouldn't be working with this as an image but rather as a 3D object with a different camera angle. Then you would simply rotate the camera around the object and display the resulting image to the user.
However if you're set on doing this as an image, then you need to create a different images representing various states of rotation of your planet and have a separate thread that would replace the displayed image with the next one in sequence, at repeated intervals. Search the web for "java image animation" - there are plenty of tutorials on how to do this.
If you want to rotate an image in 2d space, you can use something like this:
Image image = ...
Graphics2D g2d = ...; //
g2d.translate(170, 0); // If needed
g2d.rotate(1); // Rotate the image by 1 radian
//or g2d.rotate(180.0/3.14); to rotate by 1 degree
g2d.drawImage(image, 0, 0, 200, 200, observer);

How to grab pixels of a rectangle

How to grab pixels of a rectangle and how to get color of grabbed pixels in Java...
(Assuming you want to grab pixels of a rectangle in an existing Image), have a look at the PixelGrabber class. The example in the Javadocs shows how to get the color of each grabbed pixel.

Categories