Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在何处/如何调用my resizeimage方法_Java_Resize_Bufferedimage_Graphics2d_Imageicon - Fatal编程技术网

Java 在何处/如何调用my resizeimage方法

Java 在何处/如何调用my resizeimage方法,java,resize,bufferedimage,graphics2d,imageicon,Java,Resize,Bufferedimage,Graphics2d,Imageicon,好的,我创建了一个调整图像图标大小的方法。我的问题是如何/在哪里调用它,以便使图像图标大小达到我想要的大小。谢谢,该方法应该可以工作,因此任何想要调整大小的方法的人都应该能够使用它:) 首先保存已调整大小的图像。然后按资源将imageicon设置为已调整大小的图像 public static Boolean resizeImage(String sourceImage, String destinationImage, Integer Width, Integer Height) {

好的,我创建了一个调整图像图标大小的方法。我的问题是如何/在哪里调用它,以便使图像图标大小达到我想要的大小。谢谢,该方法应该可以工作,因此任何想要调整大小的方法的人都应该能够使用它:)


首先保存已调整大小的图像。然后按资源将imageicon设置为已调整大小的图像

public static Boolean resizeImage(String sourceImage, String destinationImage, Integer   Width, Integer Height) {
    BufferedImage origImage;
    try {

        origImage = ImageIO.read(new File(sourceImage));
        int type = origImage.getType() == 0? BufferedImage.TYPE_INT_ARGB :  origImage.getType();

        //*Special* if the width or height is 0 use image src dimensions
        if (Width == 0) {
            Width = origImage.getWidth();
        }
        if (Height == 0) {
            Height = origImage.getHeight();
        }

        int fHeight = Height;
        int fWidth = Width;

        //Work out the resized width/height
        if (origImage.getHeight() > Height || origImage.getWidth() > Width) {
            fHeight = Height;
            int wid = Width;
            float sum = (float)origImage.getWidth() / (float)origImage.getHeight();
            fWidth = Math.round(fHeight * sum);

            if (fWidth > wid) {
                //rezise again for the width this time
                fHeight = Math.round(wid/sum);
                fWidth = wid;
            }
        }

        BufferedImage resizedImage = new BufferedImage(fWidth, fHeight, 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(origImage, 0, 0, fWidth, fHeight, null);
        g.dispose();

        ImageIO.write(resizedImage, "png", new File(destinationImage));

    } catch (IOException ex) {
        System.out.println(""+ex);
        return false;
    }

    return true;
}
然后打电话

ImageIcon ico = new ImageIcon(destinationImage);
labelforIcon.setIcon(ico);
ImageIcon ico = new ImageIcon(destinationImage);
labelforIcon.setIcon(ico);