调整ImageIcon或缓冲图像的大小? [英] Resize the ImageIcon or the Buffered Image?

查看:449
本文介绍了调整ImageIcon或缓冲图像的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将图像尺寸调整为50 * 50像素。我从存储在数据库中的图像中获取图像。我没有问题获取图像并显示它们。我只是想知道我应该在什么时候尝试调整图像的大小。

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

上面的代码是检索图像并将其分配给ImageIcon,然后分配给JLabel。我试图通过使用下面的调整大小方法来调整缓冲图像的大小。你们能说明为什么这对我不起作用吗?没有任何错误,只是图像保持了其原始大小。

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


推荐答案

您正在调用resize()在每个图像上,但不能替换阵列中的图像。因此,resize()的输出将被丢弃:

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

即可完成这项工作。

这篇关于调整ImageIcon或缓冲图像的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆