So, I have these lines of code :
BufferedImage bufferedImage = new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB);
Graphics g = bufferedImage.getGraphics();
g.setColor(Color.cyan);
g.fillRect(0, 0, 10, 10);
(In reality I have different shappes drawn on this image..)
But how to to a (vertical) symmetry of this image? Is there a method to do that? Or I have to code the whole code...
Thank you in advance
From https://examples.javacodegeeks.com/desktop-java/awt/image/flipping-a-buffered-image/
// Flip the image vertically
AffineTransform tx = AffineTransform.getScaleInstance(1, -1);
tx.translate(0, -image.getHeight(null));
AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
image = op.filter(image, null);
// Flip the image horizontally
tx = AffineTransform.getScaleInstance(-1, 1);
tx.translate(-image.getWidth(null), 0);
op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
image = op.filter(image, null);
// Flip the image vertically and horizontally; equivalent to rotating the image 180 degrees
tx = AffineTransform.getScaleInstance(-1, -1);
tx.translate(-image.getWidth(null), -image.getHeight(null));
op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
image = op.filter(image, null);
Related
i want to add additional space in images, lets call it "span".
My code is:
BufferedImage newImage = new BufferedImage(image2.getWidth(), image2.getHeight()+200, image2.getType());
Graphics g = newImage.getGraphics();
g.setColor(Color.white);
g.fillRect(0,0,image.getWidth(),image.getHeight()+100);
g.drawImage(image, 0, 100, null);
g.setColor(Color.white);
g.dispose();
RenderedImage rendImage = newImage;
String newUrl8006002 = splitUrl[0]+"-800x6002.jpg";
File file = new File(newUrl8006002);
ImageIO.write(rendImage, "jpg", file);
The problem is, that image at the bottom has black background and i expect white (at the top is white).
Do you know what to change to add white background in whole image?
How about this:
static BufferedImage growY(BufferedImage im, int span, Color color)
{
BufferedImage newImage = new BufferedImage(im.getWidth(), im.getHeight()+span, im.getType());
int topSpan = span/2;
int botSpan = span - topSpan;
Graphics g = newImage.getGraphics();
g.setColor(color);
g.fillRect(0, 0, newImage.getWidth(), topSpan);
g.fillRect(0, topSpan+im.getHeight(), newImage.getWidth(), botSpan);
g.drawImage(im, 0, topSpan, null);
g.dispose();
return newImage;
}
I am trying to rotate a buffered image in Java (a plane icon on the map) around its centre using help from here:
Rotating BufferedImage instances
When I use this code:
AffineTransform at = new AffineTransform();
at.rotate(Math.toRadians(planeHeading),origImage.getWidth() / 2, origImage.getHeight() / 2);
AffineTransformOp op = new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR);
origImage = op.filter(origImage, null);
g.drawImage(origImage, x-origImage.getWidth() / 2, y-origImage.getHeight() / 2, null);
on rotation of 180-270 degree, the image is placed higher and a bit to the left of its centre:
If I use this code:
AffineTransform at = new AffineTransform();
at.translate(x, y);
at.rotate(Math.toRadians(planeHeading));
at.translate(-origImage.getWidth()/2, -origImage.getHeight()/2);
g.drawImage(origImage, at, null);
the image is rotated correctly, however the image itself gets very pixelated on its edges.
Can someone please help find the culprit?
This is the whole method:
#Override
public void paintWaypoint(Graphics2D g, JXMapViewer viewer, MapPlane w)
{
g = (Graphics2D)g.create();
try
{
origImage = ImageIO.read(getClass().getResource("/images/map/mapPLANE.png"));
Point2D point = viewer.getTileFactory().geoToPixel(w.getPosition(), viewer.getZoom());
// Center coordinates
int x = (int)point.getX();
int y = (int)point.getY();
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// Get heading of the plane and rotate the image
String planeHeadingStr = w.getHeading();
try
{
double planeHeading = Double.parseDouble(planeHeadingStr);
AffineTransform at = new AffineTransform();
//Do the actual rotation
at.rotate(Math.toRadians(planeHeading),origImage.getWidth() / 2, origImage.getHeight() / 2);
AffineTransformOp op = new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR);
origImage = op.filter(origImage, null);
// Draw the image
g.drawImage(origImage, x-origImage.getWidth() / 2, y-origImage.getHeight() / 2, null);
}
catch(NumberFormatException e)
{
}
g.dispose();
}
catch (Exception ex)
{
log.warn("couldn't read mapPLANE.png", ex);
}
}
Thanks a lot!
To achieve the same bilinear interpolation that you got for your AffineTransformOp in the second case where you draw directly using an AffineTransform, you should set another RenderingHint:
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
Otherwise, in your case, it defaulted to NEAREST_NEIGHBOUR interpolation.
I want to draw a rectangle around BufferedImage so it will create a border like frame.
So I load 2 BufferedImage:
BufferedImage a = ImageIO.read(new File(aPath));
BufferedImage b = ImageIO.read(new File(bPath));
And send it for drawing:
private void drawImageBorder(BufferedImage imageWithoutBorder) {
Graphics2D graph = imageWithoutBorder.createGraphics();
graph.setColor(Color.BLACK);
//create a black Rectangle - 1px bigger the original image
graph.fill(new Rectangle(imageWithoutBorder.getMinX(), imageWithoutBorder.getMinY(), imageWithoutBorder.getWidth() + 1, imageWithoutBorder.getHeight() +1));
//draw the image inside it
graph.drawImage(imageWithoutBorder, 0, 0, null);
graph.dispose();
}
For some reason it does nothing, there are similer questions like drawing-filled-rectangle-over-a-bufferedimage but I could not finnd helpful answers.
Thanks.
Almost right, but for the enlarged size and positioning.
BufferedImage image = ImageIO.read(new File(imagePath));
int w = image.getWidth();
int h = Image.getHeight();
int border = 1;
BufferedImage framedImage = new BufferedImage(w + 2*border, h + 2*border, image.getType());
Graphics2D graph = framedImage.createGraphics();
graph.setColor(Color.BLACK);
graph.fill(new Rectangle(0, 0, w + 2*border, h + 2*border));
graph.drawImage(image, border, border, null);
graph.dispose();
Possible reason can be, that you don't persist the changes made to the image, for example writing them back into an image file with ImageIO.write.
I want to shear my images with AffineTransform in Java. If I do that I always get black bounds.
for (File input : inputImages) {
if (!input.getName().contains(".DS_Store")) {
BufferedImage buffer = ImageIO.read(input);
for (int i = 0; i <= 2; i++) {
AffineTransform tx = new AffineTransform();
tx.translate(buffer.getHeight() / 2, buffer.getWidth() / 2);
tx.shear(0.3, 0);
tx.translate(-buffer.getWidth() / 2, -buffer.getHeight() / 2);
AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);
BufferedImage newImage = new BufferedImage(buffer.getHeight(), buffer.getWidth(), buffer.getType());
op.filter(buffer, newImage);
File output = new File("output/" + FilenameUtils.getBaseName(input.getName()) + i + ".png");
ImageIO.write(newImage, "png", output);
}
}
}
Is there are way to avoid these black bounds and to get a white or transparent background?
Use BufferedImage.TYPE_INT_ARGB as the image type to create a transparent image
If I use TYPE_ARGB my output image is completely transparent
Works fine for more
BufferedImage buffer = ImageIO.read(...);
AffineTransform tx = new AffineTransform();
tx.translate(buffer.getHeight() / 2, buffer.getWidth() / 2);
tx.shear(0.3, 0);
tx.translate(-buffer.getWidth() / 2, -buffer.getHeight() / 2);
AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);
BufferedImage newImage = new BufferedImage(buffer.getHeight(), buffer.getWidth(), BufferedImage.TYPE_INT_ARGB);
op.filter(buffer, newImage);
JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(newImage)));
If you prefer, you could fill the newImage with a default color
I try to scale image to 50x50 px, but I got black color. I need to make black to white
after scaled
this my code:
BufferedImage imgs = urlToBufferImage("src//imgTest.jpg");
BufferedImage resizedImage = new BufferedImage(50, 50, imgs.getType());
Graphics2D g = resizedImage.createGraphics();
// g.setBackground(Color.WHITE);
// g.drawImage(imgs, 0, 0, 50, 50,Color.WHITE, null);
g.drawImage(imgs.getScaledInstance(50, -1, Image.SCALE_DEFAULT), 0, 0, this);
g.dispose();
This is pretty simple.
My approach would be not to create a new BufferedImage, but to do:
BufferedImage imgs = urlToBufferImage("src//imgTest.jpg");
Graphics g = imgs.createGraphics();
g.drawImage(imgs, x, y, 50, 50, null);
or instead of drawing the image inside of the bounds, you could do
Graphics2D g2d = imgs.createGraphics();
g2d.scale(0.5, 0.5);
g2d.drawImage(imgs, x, y, null);