Java 调整图片大小以适合 JLabel
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16343098/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Resize a picture to fit a JLabel
提问by
I'm trying to make a picture fit a JLabel. I wish to reduce the picture dimensions to something more appropriate for my Swing JPanel.
我正在尝试使图片适合 JLabel。我希望将图片尺寸缩小到更适合我的 Swing JPanel 的尺寸。
I tried with setPreferredSize but it doesn't work.
我尝试使用 setPreferredSize 但它不起作用。
I'm wondering if there is a simple way to do it? Should I scale the image for this purpose?
我想知道是否有一个简单的方法来做到这一点?我应该为此目的缩放图像吗?
采纳答案by Gilbert Le Blanc
Outline
大纲
Here are the steps to follow.
以下是要遵循的步骤。
- Read the picture as a BufferedImage.
- Resize the BufferedImage to another BufferedImage that's the size of the JLabel.
- Create an ImageIcon from the resized BufferedImage.
- 将图片读取为 BufferedImage。
- 将 BufferedImage 调整为另一个与 JLabel 大小相同的 BufferedImage。
- 从调整大小的 BufferedImage 创建一个 ImageIcon。
You do not have to set the preferred size of the JLabel. Once you've scaled the image to the size you want, the JLabel will take the size of the ImageIcon.
您不必设置 JLabel 的首选大小。将图像缩放到所需大小后,JLabel 将采用 ImageIcon 的大小。
Read the picture as a BufferedImage
将图片读取为 BufferedImage
BufferedImage img = null;
try {
img = ImageIO.read(new File("strawberry.jpg"));
} catch (IOException e) {
e.printStackTrace();
}
Resize the BufferedImage
调整缓冲图像的大小
Image dimg = img.getScaledInstance(label.getWidth(), label.getHeight(),
Image.SCALE_SMOOTH);
Make sure that the label width and height are the same proportions as the original image width and height. In other words, if the picture is 600 x 900 pixels, scale to 100 X 150. Otherwise, your picture will be distorted.
确保标签的宽度和高度与原始图像的宽度和高度的比例相同。换句话说,如果图片是 600 x 900 像素,则缩放到 100 X 150。否则,您的图片会失真。
Create an ImageIcon
创建一个图像图标
ImageIcon imageIcon = new ImageIcon(dimg);
回答by Aditya Rameshwarpratap Singh
Or u can do it this way. The function u put the below 6 lines will throw an IOException. And will take your JLabel as a parameter.
或者你可以这样做。您在下面 6 行中放置的函数将抛出 IOException。并将您的 JLabel 作为参数。
BufferedImage bi=new BufferedImage(label.width(),label.height(),BufferedImage.TYPE_INT_RGB);
Graphics2D g=bi.createGraphics();
Image img=ImageIO.read(new File("path of your image"));
g.drawImage(img, 0, 0, label.width(), label.height(), null);
g.dispose();
return bi;
回答by shexaMartin
i have done the following and it worked perfectly
我已经完成了以下操作并且效果很好
try {
JFileChooser jfc = new JFileChooser();
jfc.showOpenDialog(null);
File f = jfc.getSelectedFile();
Image bi = ImageIO.read(f);
image1.setText("");
image1.setIcon(new ImageIcon(bi.getScaledInstance(int width, int width, int width)));
} catch (Exception e) {
}
回答by muabal
Assign your image to a string. Eg image Now set icon to a fixed size labe.
将您的图像分配给一个字符串。例如图像 现在将图标设置为固定大小的标签。
image.setIcon(new javax.swing.ImageIcon(image.getScaledInstance(50,50,WIDTH)));
image.setIcon(new javax.swing.ImageIcon(image.getScaledInstance(50,50,WIDTH)));
回答by tirz
You can try it:
你可以试试看:
ImageIcon imageIcon = new ImageIcon(new ImageIcon("icon.png").getImage().getScaledInstance(20, 20, Image.SCALE_DEFAULT));
label.setIcon(imageIcon);
Or in one line:
或者在一行中:
label.setIcon(new ImageIcon(new ImageIcon("icon.png").getImage().getScaledInstance(20, 20, Image.SCALE_DEFAULT)));
The execution time is much more faster than File and ImageIO.
执行时间比 File 和 ImageIO 快得多。
I recommend you to compare the two solutions in a loop.
我建议您循环比较这两种解决方案。
回答by amr foda
public static void main(String s[])
{
BufferedImage image = null;
try
{
image = ImageIO.read(new File("your image path"));
} catch (Exception e)
{
e.printStackTrace();
}
ImageIcon imageIcon = new ImageIcon(fitimage(image, label.getWidth(), label.getHeight()));
jLabel1.setIcon(imageIcon);
}
private Image fitimage(Image img , int w , int h)
{
BufferedImage resizedimage = new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = resizedimage.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2.drawImage(img, 0, 0,w,h,null);
g2.dispose();
return resizedimage;
}
回答by Renjith Thomas
The best and easy way for image resize using Java Swing is:
使用 Java Swing 调整图像大小的最佳且简单的方法是:
jLabel.setIcon(new ImageIcon(new javax.swing.ImageIcon(getClass().getResource("/res/image.png")).getImage().getScaledInstance(200, 50, Image.SCALE_SMOOTH)));
For better display, identify the actual height & width of image and resize based on width/height percentage
为了更好地显示,确定图像的实际高度和宽度并根据宽度/高度百分比调整大小
回答by pettlaurence
public void selectImageAndResize(){
int returnVal = jFileChooser.showOpenDialog(this); //open jfilechooser
if (returnVal == jFileChooser.APPROVE_OPTION) { //select image
File file = jFileChooser.getSelectedFile(); //get the image
BufferedImage bi;
try {
//
//transforms selected file to buffer
//
bi=ImageIO.read(file);
ImageIcon iconimage = new ImageIcon(bi);
//
//get image dimensions
//
BufferedImage bi2 = new BufferedImage(iconimage.getIconWidth(), iconimage.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics g = bi.createGraphics();
iconimage.paintIcon(null, g, 0,0);
g.dispose();
//
//resize image according to jlabel
//
BufferedImage resizedimage=resize(bi,jLabel2.getWidth(), jLabel2.getHeight());
ImageIcon resizedicon=new ImageIcon(resizedimage);
jLabel2.setIcon(resizedicon);
} catch (Exception ex) {
System.out.println("problem accessing file"+file.getAbsolutePath());
}
}
else {
System.out.println("File access cancelled by user.");
}
}