Writing on a premade Image - java

http://www.robinswebsitestudio.com/ButtonSamples/buttons.html
The following code does write to a premade image. The resulting image, though, has significant problems.
the original png file is a 3D button with Index Transparency and a matte so that the gif file would
blend in with the page background color
the resulting gif file is flat, the matte is gone, and the text doesn’t look smooth. The resulting
file is therefore is not something you would get from Adobe Photoshop or Fireworks and doesn’t look
professional.
If anyone has suggestions, I’m all ears. Is it impossible to get a high quality 3D button using Java?
String pathToWeb = getServletContext().getRealPath(File.separator);
File f = new File(pathToWeb + "activities.png");
BufferedImage bufferedImage = ImageIO.read(f);
// Create a graphics which can be used to draw into the buffered image
Graphics2D g2d = bufferedImage.createGraphics();
g2d.setFont(new Font( "Arial", Font.BOLD, 15 ));
//create a string with black
g2d.setColor(Color.black);
g2d.drawString("Activities", 40, 40);
// Disposes of this graphics context and releases any system resources that it is using.
g2d.dispose();
// Save as gif
File file = new File("myactivities.gif");
ImageIO.write(bufferedImage, "gif", file);

Related

Graphics2D rotate before saving file

I create this image with g2d:
Here is the code:
BufferedImage bufferedImage = new BufferedImage(408, 408, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = bufferedImage.createGraphics();
List<Pixel> pixels = cacheRepo.findAll();
pixels.stream().forEach(pixel -> {
g2d.setColor(getColorFromPixel(pixel));
g2d.fillRect(getPos(pixel.getPosition().x), getPos(pixel.getPosition().y), 20, 20);
});
Now I am trying to rotate it 90° anticlok so that the bleu square appear on the lower left:
So I add this:
g2d.rotate(Math.toRadians(90));
g2d.drawRenderedImage(bufferedImage, null);
But the rotation doesn't occur (I still have the same image).
Here is the complete piece of code, with the part that save image:
// Constructs a BufferedImage of one of the predefined image types.
BufferedImage bufferedImage = new BufferedImage(408, 408, BufferedImage.TYPE_INT_RGB);
// Create a graphics which can be used to draw into the buffered image
Graphics2D g2d = bufferedImage.createGraphics();
List<Pixel> pixels = cacheRepo.findAll();
pixels.stream().forEach(pixel -> {
g2d.setColor(getColorFromPixel(pixel));
g2d.fillRect(getPos(pixel.getPosition().x), getPos(pixel.getPosition().y), 20, 20);
});
g2d.rotate(Math.toRadians(90));
g2d.drawRenderedImage(bufferedImage, null);
g2d.dispose();
// Save as PNG
File file = new File("myimage.png");
try {
ImageIO.write(bufferedImage, "png", file);
} catch (IOException e) {
e.printStackTrace();
}
Transformations should be applied BEFORE any operations you want to be effected by them, transformations won't affect anything that was done before it...
BufferedImage bufferedImage = new BufferedImage(408, 408, BufferedImage.TYPE_INT_RGB);
// Create a graphics which can be used to draw into the buffered image
Graphics2D g2d = bufferedImage.createGraphics();
g2d.rotate(Math.toRadians(90));
List<Pixel> pixels = cacheRepo.findAll();
pixels.stream().forEach(pixel -> {
g2d.setColor(getColorFromPixel(pixel));
g2d.fillRect(getPos(pixel.getPosition().x), getPos(pixel.getPosition().y), 20, 20);
});
//g2d.rotate(Math.toRadians(90));
// Not sure what you're hoping to achieve here
//g2d.drawRenderedImage(bufferedImage, null);
g2d.dispose();
If you prefer, use two BufferedImages. Render the "normal" content to the first, then use the second to paint the first, but with a rotation transformation ... because transformations do my head in 😝
using your code draw a black image
You probably need to supply a anchor point around which the image can be rotated, otherwise it will be rated about the top/left corner
And you'll forgive me, but it's not like this kind of think hasn't been asked before
Java rotate image turns part of background black
Rotate a buffered image in Java
Rotating Image with AffineTransform
Rotating an Image object
I'm rotating image in java but want to save rotated image
How to save rotated buffered image in another buffered image?
BufferedImage rotated, change resulting background

Crop image by polygon area in Java

by using Canvas and JS I can draw a shape like this and have the x,y of each point :
Tha area can be choosen by more than 4 points, look at this link to have an idea.
I need to save and crop the image of the selected area by using the points. I can not use BufferedImage as it is just rectangular. Which lib in java I can use?
Okay, so starting with...
I used...
BufferedImage source = ImageIO.read(new File("Example.jpg"));
GeneralPath clip = new GeneralPath();
clip.moveTo(65, 123);
clip.lineTo(241, 178);
clip.lineTo(268, 405);
clip.lineTo(145, 512);
clip.closePath();
Rectangle bounds = clip.getBounds();
BufferedImage img = new BufferedImage(bounds.width, bounds.height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = img.createGraphics();
clip.transform(AffineTransform.getTranslateInstance(-65, -123));
g2d.setClip(clip);
g2d.translate(-65, -123);
g2d.drawImage(source, 0, 0, null);
g2d.dispose();
ImageIO.write(img, "png", new File("Clipped.png"));
to generate...
Now, the image is rectangular, that's just the way it works
Now, setClip is quite rough and isn't effect by any RenderingHints, you could make use of "soft clipping" instead, which is more involved, but generates a nicer results. See this example and this exmaple for more details

Using Java AWT to generate monochrome 1bitpp PNG with Ancillary Chunks

I am trying to generate PNG with the following ancillary chunks (header from a reference image)
The ancillary chunks in the reference image probably came from GIMP processing.
Whereas the Image I generate with Java AWT does not have those ancillary chunks.
Here is the header of the PNG I am generating. Please note the critical chunks are identical.
Here is the code fragment
{
:
// The color map contains the colors black and white
byte[] cMap = {0, 0, 0, (byte)255, (byte)255, (byte)255};
// Create an IndexColorModel setting white as the transparent color
IndexColorModel monochrome = new IndexColorModel(8, 2, cMap, 0, false, 0);
BufferedImage img = new BufferedImage(width_img, height_img,
BufferedImage.TYPE_BYTE_INDEXED,monochrome);
Graphics2D g2d = img.createGraphics();
:
g2d.setColor(Color.WHITE);
g2d.fillRect(0, 0, width_img, height_img);
Font font = new Font("Arial Bold", Font.PLAIN, 48);
g2d.setFont(font);
FontMetrics fm = g2d.getFontMetrics();
g2d.setColor(Color.BLACK);
:
:
g2d.dispose();
imgName = ".\\Panel"+width_img+"x"+height_img+".png";
ImageIO.write(img, "png", new File(imgName));
:
}
How do I add chunks (pHYs, tIME) using Java AWT?
Any pointers will be helpful too- please let me know.
Thank you
I concluded that AWT does not support - this questions identifies couple of ways to add chunks
How can I save a PNG with a tEXt or iTXt chunk from Java?
Here is what I propose to do -
Use AWT / Graphics 2D to generate the .PNG
Read PNG, refer to the link for details on how to do it
Add the Chunks you want to add
Write it(them) back to the .PNG

Improve "drawString" methods text quality

I'm in the process of making a captcha in Java but I'm having trouble improving the text quality the "drawString" method generates on top of my image.
Example of the text quality:
You can actually see the horrible edges on the text.
Java code:
File file = new File("C:\\captcha.png");
File file2 = new File("C:\\captcha2.png");
File fontfile = new File("C:\\xerox.ttf");
BufferedImage bfimage = ImageIO.read(file);
Graphics2D g = bfimage.createGraphics();
Font myfont = Font.createFont(Font.PLAIN, fontfile);
myfont = myfont.deriveFont(50f);
g.setFont(myfont);
g.setColor(Color.black);
AffineTransform att = new AffineTransform();
g.translate(100, 50);
att.rotate(Math.toRadians(15), 100, 50);
g.setTransform(att);
g.drawString("12345", 100, 50);
RenderedImage rimg = bfimage;
ImageIO.write(rimg, "PNG", file2);
Example of same font used in php, but here the quality is A LOT better with smooth edges:
How do I improve the text quality generated by the "drawString" method in Java?
Graphics and Graphics2D provide a rendering hint framework that allows you to configure some parts of the rendering of a component. Use an antialiasing rendering hint:
g2d.setRenderingHint( RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_DEFAULT);
and you should get antialiased text on your captcha.
http://docs.oracle.com/javase/6/docs/api/java/awt/RenderingHints.html for reference

Loading only part of a file into an Image

[Java] I'm using Images / BufferedImages (I don't currently know a better way) to load external pictures unto the canvas.
Q: Is there a possibility of loading / displaying only a part of the picture? For example, using only a 60x60 area from a larger 500x300 picture file. (I believe this is called a Sprite) Loading the file using ImageIO.
Thanks in advance.
BufferedImage input = ImageIO.read(new File(inputFile));
BufferedImage output = new BufferedImage(60, 60, BufferedImage.TYPE_INT_ARGB)
Graphics g = output .getGraphics();
g.drawImage(input, 0, 0, null); //adjust 0, 0 to some x, y if necessary
ImageIO.write(output, "png", new File(outputFile));

Categories