我正在编写一个程序,该程序应该以幻灯片形式显示不断变化的图像。这样做的目的是缩小图像,这样它们就不会超出屏幕的边界。但图像不是按比例缩小显示,而是以其原始大小显示并在两侧进行裁剪。
到目前为止,我已经尝试了不同的方法 - 尝试方法 .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);
}
如何让程序缩小我想要显示的图像而不是裁剪它们?
这是一个完整的示例:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.RenderingHints;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class Main {
public static void main(String[] args) throws IOException {
JFrame frame = new JFrame();
File pathToFile = new File("./doggo.jpeg");
Image img = ImageIO.read(pathToFile);
JComponent panel = new JComponent()
{
@Override
public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
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(img, scale[2], scale[3], scale[0], scale[1], null);
}
};
panel.setBackground(Color.black);
frame.add(panel, BorderLayout.CENTER);
frame.setBackground(Color.black);
GraphicsDevice gd =
GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
if (gd.isFullScreenSupported()) {
frame.setUndecorated(true);
gd.setFullScreenWindow(frame);
} else {
System.err.println("Full screen not supported");
frame.setSize(765, 1024);
frame.setVisible(true);
}
}
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 };
}
}