Java:图像以原始尺寸显示并被裁剪而不是缩小

问题描述 投票:0回答:1

我正在编写一个程序,该程序应该以幻灯片形式显示不断变化的图像。这样做的目的是缩小图像,这样它们就不会超出屏幕的边界。但图像不是按比例缩小显示,而是以其原始大小显示并在两侧进行裁剪。

到目前为止,我已经尝试过不同的方法 - 尝试方法 .getScaledInstance() (这只是提供这种废话),尝试使用 Graphis 或 Graphis2D 重绘文件,它只返回白色表面,尝试实现 Scalr 和 Thumbnator (这没有根本不起作用,只是抛出错误消息)和 Marvin 甚至无法编译。因此,像本教程中stackoverflow上这样的可能解决方案对我来说并不成功。

此时我严重迷失,迫切需要帮助。

我正在使用的代码现在处于这种状态:

    public static Image displayImage(File f, Dimension screenSize) throws IOException {
        
        BufferedImage originalImage = ImageIO.read(f);
        double width = screenSize.getWidth()*0.75;
        double z1 = (originalImage.getWidth()/width);
        double z2 = (originalImage.getHeight()/(screenSize.getHeight()-20));
        int setWidth, setHeight;

        //case 1: height is smaller than width so scaling depends on the width
        if (originalImage.getHeight()/z1 <= width && originalImage.getHeight()/z1 < (screenSize.getHeight()-20)) {

            if (originalImage.getWidth()/z1 < width) {
                setWidth = (int) width;
                setHeight = (int)((originalImage.getHeight()/z1)*((originalImage.getWidth()/z1)/width));
            } else {
                setWidth = (int) (originalImage.getWidth()/z1);
                setHeight = (int)(originalImage.getHeight()/z1);
            }

        //case 2: width is smaller than height so scaling depends on the height
        } else {
            if (originalImage.getHeight()/z2 < (screenSize.getHeight()-20)) {
                setWidth = (int) width;
                setHeight = (int)((originalImage.getHeight()/z2)/(originalImage.getWidth()/z2)*width);
            } else {
                setWidth = (int) (originalImage.getWidth()/z2);
                setHeight = (int)(originalImage.getHeight()/z2);
            }
        }
        return originalImage.getScaledInstance(setWidth, setHeight, Image.SCALE_AREA_AVERAGING);
    }  
}

您可以帮助让程序缩小我想要显示的图像而不是裁剪它们吗?

java image image-processing scaling
1个回答
0
投票

我过去曾使用此功能来完成您想要做的事情:

    public static int[] scaleImage(Image image, int newWidth, int newHeight)
    {
        double thumbRatio = (double) newWidth / (double) newHeight;
        int imageWidth = image.getWidth(null);
        int imageHeight = image.getHeight(null);
        double aspectRatio = (double) imageWidth / (double) imageHeight;

        int x = 0;
        int y = 0;

        if (thumbRatio < aspectRatio)
        {
            y = newHeight;
            newHeight = (int) (newWidth / aspectRatio);
            y /= 2;
            y -= newHeight / 2;
        }
        else
        {
            x = newWidth;
            newWidth = (int) (newHeight * aspectRatio);
            x /= 2;
            x -= newWidth / 2;
        }

        return new int[] { newWidth, newHeight, x, y };
    }

然后从这样的组件中使用:

        JFrame frame = new JFrame();
        
        JComponent panel = new JComponent()
        {
            @Override
            public void paintComponent(Graphics g)
            {
                Graphics2D g2 = (Graphics2D) g;
                Image img = // get an image from somewhere!

                int w = this.getWidth();
                int h = this.getHeight();

                int[] scale = scaleImage(img, w, h);

                g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                g2.setColor(Color.black);
                g2.fillRect(0, 0, getWidth(), getHeight());
                g2.drawImage(buffer, scale[2], scale[3], scale[0], scale[1], null);
            }
        };
        panel.setBackground(Color.black);
        frame.add(panel, BorderLayout.CENTER);
        frame.setBackground(Color.black);
        frame.setVisible(true);
© www.soinside.com 2019 - 2024. All rights reserved.