Resizing Images for full-screen applications? - java

I've got a full-screen JFrame and I'd like to create an background for it by filling a JLabel with an image and matching it's size to the dimension of the screen.
Whilst I can create a JLabel that matches the screen's size I can't seem to resize my image to fit. In essence, I want to end up with a full-screen JFrame that is completely filled with an image (regardless of the image's or the screen's dimensions).
Current code is below, thanks!
public void addBackground(ImageIcon imgIcon) {
Image img = imgIcon.getImage();
BufferedImage buffImg = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB);
BufferedImage resizeBuffImg = resize(buffImg, screenSize.width, screenSize.height);
Image finalImg = Toolkit.getDefaultToolkit().createImage(resizeBuffImg.getSource());
ImageIcon back = new ImageIcon(finalImg);
System.out.println("IMAGE WIDTH: " + back.getIconWidth());
System.out.println("IMAGE HEIGHT: " + back.getIconHeight());
JLabel backgroundLabel = new JLabel(back);
getContentPane().add(backgroundLabel);
}
public BufferedImage resize(BufferedImage image, int width, int height) {
int type = image.getType() == 0? BufferedImage.TYPE_INT_ARGB : image.getType();
BufferedImage resizedImage = new BufferedImage(width, height, type);
Graphics2D g = resizedImage.createGraphics();
g.setComposite(AlphaComposite.Src);
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.setRenderingHint(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g.drawImage(image, 0, 0, width, height, null);
g.dispose();
return resizedImage;
}
EDIT - Got it! Using Image's getScaledInstance() works. It's not perfect, but I'm intending to scale down large images rather than expand smaller ones.

Related

Resize Java BufferedImage keeping aspect ratio and fill with background

I'm working with Java to store and modify .jpg images, (not Android or Swing). I want to transform an image with a new dimension keeping the aspect ratio and filling the background with white color if the new dimension is not proportional to the original image.
BufferedImage image = /*Here i read from disk and load the image */;
image = resizeImage(image, newWidth, newHeight);
ImageIO.write(image, extension, new File(filename + "_" + page + "." + extension));
The function I'm trying to implement is resizeImage: in the example resizes the image but it doesn't keep the aspect ratio.
private static BufferedImage resizeImage(BufferedImage originalImage, int width, int height) {
BufferedImage resizedImage = new BufferedImage(width, height, originalImage.getType());
Graphics2D g = resizedImage.createGraphics();
g.drawImage(originalImage, 0, 0, width, height, null);
g.dispose();
return resizedImage;
}
I think a picture will be more illustrative of what I'm asking for:
If the original image is 200x200 and is asked to resize to 400x300 the result should be a picture with white margin and the original picture resized inside. In this example should be 300x300.
The problem is not how to resize, it's how to fill the remaining image with white and the original resized image on the center of it.
This code worked for me:
private static BufferedImage resizeImage(BufferedImage originalImage, int newWidth, int newHeight) {
BufferedImage resizedImage = new BufferedImage(newWidth, newHeight, originalImage.getType());
Graphics2D graphics = resizedImage.createGraphics();
graphics.setColor(Color.WHITE);
// fill the entire picture with white
graphics.fillRect(0, 0, newWidth, newHeight);
int maxWidth = 0;
int maxHeight = 0;
// go along the width and height in increments of 1 until one value is equal to the specified resize value
while (maxWidth <= newWidth && maxHeight <= newHeight)
{
++maxWidth;
++maxHeight;
}
// calculate the x value with which the original image is centred
int centerX = (resizedImage.getWidth() - maxWidth) / 2;
// calculate the y value with which the original image is centred
int centerY = (resizedImage.getHeight() - maxHeight) / 2;
// draw the original image
graphics.drawImage(originalImage, centerX, centerY, maxWidth, maxHeight, null);
graphics.dispose();
return resizedImage;
}
Before:
After:

Java Add span to image background color

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;
}

Draw a rectangle around image

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.

Java Image, after scaled get black color, how to make it white?

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);

Resize the ImageIcon or the Buffered Image?

I'm trying to resize an image to 50 * 50 pixels. Im taking the images, from their path stored in a Database. I have no problem getting the images and displaying them. I'm just wondering at what point should I try resize the images. Should it be when I get the image as a buffered image, or just try to resize the icon?
while (rs.next()) {
i = 1;
imagePath = rs.getString("path");
System.out.println(imagePath + "\n");
System.out.println("TESTING - READING IMAGE");
System.out.println(i);
myImages[i] = ImageIO.read(new File(imagePath));
**resize(myImages[i]);**
imglab[i] = new JLabel(new ImageIcon(myImages[i]));
System.out.println(i);
imgPanel[i]= new JPanel();
imgPanel[i].add(imglab[i]);
loadcard.add(imgPanel[i], ""+i);
i++;
The above code is retrieving the image and assigning it to an ImageIcon, then JLabel. I have attempted to resize the buffered image, by using the below resize method. Could you guys, shed any light on why this isn't working for me? Not getting any errors, just the image remains its original size.
public static BufferedImage resize(BufferedImage img) {
int w = img.getWidth();
int h = img.getHeight();
int newH = 50;
int newW = 50;
BufferedImage dimg = dimg = new BufferedImage(newW, newH, img.getType());
Graphics2D g = dimg.createGraphics();
System.out.println("Is this getting here at all " + dimg);
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(img, 0, 0, newW, newH, 0, 0, w, h, null);
g.dispose();
return dimg;
}
You are calling resize() on each image, but not replacing the images in the array. So the output of resize() is being thrown away:
myImages[i] = ImageIO.read(new File(imagePath)); // create an image
resize(myImages[i]); // returns resized img, but doesn't assign it to anything
imglab[i] = new JLabel(new ImageIcon(myImages[i])); // uses _original_ img
You need to change the middle line to:
myImages[i] = resize(myImages[i]);
to make this work.

Categories

Resources