Java rescaling an image creating half white half grey image - java

I am having a problem with the Grapics2D API, I am trying to re-size an image but instead of getting the smaller image its showing a half white and half grey area. what am I doing wrong? I tried with and without render hints and it didn't work, or is there another approach that will give me a new Buffered Image object at the specified size?
//height = 400, width = 600, capture_rect = screen size
BufferedImage img = robot.createScreenCapture(CAPTURE_RECT);
BufferedImage resized = new BufferedImage(WIDTH, HEIGHT, img.getType());
Graphics2D g = resized.createGraphics();
g.drawImage(img, 0, 0, WIDTH, HEIGHT, 0, 0, img.getWidth(), img.getHeight(), null);
g.dispose();
setImage(resized);

See the article on Perils of Image.getScaledImage. It has some recommendations.
Here is some code it recommends:
private float xScaleFactor, yScaleFactor = ...;
private BufferedImage originalImage = ...;
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
int newW = (int)(originalImage.getWidth() * xScaleFactor);
int newH = (int)(originalImage.getHeight() * yScaleFactor);
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2.drawImage(originalImage, 0, 0, newW, newH, null);
}
It looks like you just need to specify the new width/height and it will do the scaling for you automatically.
Or maybe the problem is with the img.getType() method. You should be able to just using:
BufferedImage.TYPE_INT_RGB // (or ARGB)
See which works the best.

Related

changing the size of ImageIcon

I have a Jtable. In my Jtable, the first column, I manage to import an image from my directory path store in a text file.
model.addRow(new Object[]{
new ImageIcon(value[1]),
value[2],
value[0],
value[3],
value[4],
});
But the image size is too large that it only show part of the image in the limited space in the table column.
How do I resize the image to height = 100 and Width = 100.
Load your Image first, then do
Image scaledIm = orignalIm.getScaledInstance(100, 100, Image.SCALE_SMOOTH);
ImageIcon icon = new ImageIcon(scaledIm);
Or use another rendering hints value
Resizing the icon is not straightforward. You need to use Java's graphics 2D to scale the image.
private Image getScaledImage(Image srcImg, int w, int h){
BufferedImage resizedImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = resizedImg.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2.drawImage(srcImg, 0, 0, w, h, null);
g2.dispose();
return resizedImg;
}

Draw transparency of image on BufferStrategy

Good day,
I tried to draw a picture on a BufferStrategy with its Graphics. The picture has a transparent background and if I draw it on the screen the transparent areas change to black.
The blue thing is the image I want to draw but without the black parts (in the original picture they are not there).
That is how I draw the picture:
BufferedImage image = loadImage(path);
g.drawImage(image, x, y, null);
public BufferedImage loadImage(String path) {
ImageIcon icon = new ImageIcon(this.getClass().getClassLoader().getResource(path));
BufferedImage image = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_RGB);
Graphics g = image.createGraphics();
icon.paintIcon(null, g, 0, 0);
g.dispose();
return image;
}
The comment from Andreas is correct but is should be ARGB rather than RGBA.
To do this just change BufferedImage.TYPE_INT_RGB in this line:
BufferedImage image = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_RGB);
To BufferedImage.TYPE_INT_ARGB:
BufferedImage image = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
Edit in response to your comment, here is the complete answer:
In addition to creating a BufferedImage as TYPE_INT_ARGB you also need to apply the AlphaComposite SRC_OVER to your buffered image using Graphics2D like this:
public static BufferedImage loadImage(String path)
{
ImageIcon icon = new ImageIcon(path);
//using TYPE_INT_ARGB
BufferedImage image = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
//changed to G2D change here
Graphics2D g2d = image.createGraphics();
//get alpha
AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER);
//set alpha
g2d.setComposite(ac);
icon.paintIcon(null, g2d, 0, 0);
g2d.dispose();
return image;
}

Zoom out/in using Buffered Image / AffineTransformation / SWING

I'm using an ImageIcon in order to display an image in a JPanel. I need to implement zoom in / zoom out functions over that image.
If I use the following code it does the zoom out/in work, but the quality of the image reduces significantly due to the transparency.
BufferedImage resizedImage = new BufferedImage(newImageWidth, newImageHeight, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = resizedImage.createGraphics();
g.drawImage(image, 0, 0, newImageWidth , newImageHeight , null);
g.dispose();
return resizedImage;
I have read that this other following code is a solution but it gives me the same result as above.
AffineTransform af = new AffineTransform();
af.scale(zoomLevel, zoomLevel);
AffineTransformOp operation = new AffineTransformOp(af, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
BufferedImage result = operation.filter((BufferedImage) image, null);
return result;
Any suggestion? Thanks in advance guys.

How to convert an image to greyscale?

I want to simply convert an image to greyscale. But all I get is a completely black image. Why?
BufferedImage original = ImageIO.read(url);
BufferedImage image = new BufferedImage(original.getWidth(), original.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
Graphics g = image.getGraphics();
g.drawImage(image, 0, 0, null);
g.dispose();
It has the correct width and height like the source image, but is completely black...
You are writing empty image to itself.
Change
g.drawImage(image, 0, 0, null);
to
g.drawImage(original, 0, 0, null);

How to get rid of White rectangle from an image using java

I had tired the following code to generate the thumb nail but when I place it on another image, the thumbnail has a white rectangle. How can I remove it using java graphics 2D?
Image image = javax.imageio.ImageIO.read(new File(originalFile));
BufferedImage thumbImage = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics2D = thumbImage.createGraphics();
graphics2D.setBackground(Color.WHITE);
graphics2D.setPaint(Color.WHITE);
graphics2D.fillRect(0, 0, thumbWidth, thumbHeight);
graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null);
File file = new File(thumbnailFile);
if(javax.imageio.ImageIO.write(thumbImage, "png", file))
return file;
}
Even if i remove these three lines I am getting black rectangle.
graphics2D.setBackground(Color.WHITE);
graphics2D.setPaint(Color.WHITE);
graphics2D.fillRect(0, 0, thumbWidth, thumbHeight);
How to get this image as transparent? Please some one help me out.
Color transparent = new Color(0,0,0,0)
by setting Composite . here : AlphaComposite
// Create an image that supports arbitrary levels of transparency
Graphics2D g2d = (Graphics2D) image.getGraphics();
BufferedImage thumbImage = g2d.getDeviceConfiguration().createCompatibleImage(
thumbWidth, thumbHeight, Transparency.TRANSLUCENT);
..BufferedImage.TYPE_INT_RGB
That should be:
..BufferedImage.TYPE_INT_ARGB

Categories