如何阻止JLabel对图像进行缩放?

huangapple 未分类评论61阅读模式
英文:

How can I stop JLabel scaling an image?

问题

我有一个Swing应用程序,可以通过将图像加载到JLabel中来显示图像。在Java 8之前,这个功能正常工作。自从Java 9以来,有一些变化旨在使应用程序在更高像素密度下调整大小,包括重新调整图像大小:当像素密度较高时,图像会被放大。结果在许多情况下都很不美观。例如,如果我有一张像素高的照片,我的应用程序会将其缩小以适应可用空间,但然后它会自动放大,以适应更高的像素密度 - 看起来很丑。

我可以看到有几种避免这个问题的方法:

  1. 实现java.awt.image.MultiResolutionImage接口,看起来应该有帮助,但我不知道如何在JLabel中使用它。
  2. 也许有一种方法可以欺骗Swing,让它认为不需要进行任何像素缩放,但我找不到类似的方法。

以下代码是一个简单的示例,展示了如何在标签中显示图像。在Java 8中,图像以像素为单位显示。在Java 9+中,整个UI,包括图像在内,会根据您的操作系统认为的像素密度进行放大。您应该能够显式设置此缩放 - Windows 10中可以在“显示”、“缩放和布局”下找到此选项。我的设置为125%,这似乎是一个完美的数值,可以使几乎任何图像看起来都不美观。

import java.awt.*;
import javax.imageio.*;
import javax.swing.*;
import java.io.*;

public class Img extends JFrame {
    public static void main(String[] args) throws IOException {
        new Img(new File(args[0]));
    }

    private Img(File file) throws IOException {
        super("Img: " + file.getName());
        ImageIcon img = new ImageIcon(ImageIO.read(file));
        JLabel label = new JLabel(img);
        label.setPreferredSize(new Dimension(img.getIconWidth(), img.getIconHeight()));
        add(label);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        pack();
        setVisible(true);
    }
}
英文:

I have a Swing app that can display an image by loading it into a JLabel. Up to Java 8, this worked fine. Since Java 9 there are changes intended to make apps resize on higher pixel densities, including rescaling images: when pixel density is high, images are scaled up. The results are ugly in many cases. As an example, if I have a photo with a high pixel count, my app scales it down to fit the available space, but then it's automatically scaled back up to allow for the higher pixel density - ugly.

A couple of options I can see for avoiding this problem are:

  1. Implementing the interface java.awt.image.MultiResolutionImage which looks like it should help, but I don't see how to use that with JLabel.
  2. Maybe there's a way to fool Swing into thinking that no pixel scaling is necessary, but I can't find anything like that

The following code is a simple example of displaying an image in a label. In Java 8, the image is displayed pixel-for-pixel. In Java 9+, the whole UI including the image is scaled up according to what your OS thinks is your pixel density. You should be able to set this scaling explicitly - Windows 10 has it under "Display", "Scale and Layout". Mine is set to 125% which seems to be a perfect amount to make almost any image look ugly.

import java.awt.*;
import javax.imageio.*;
import javax.swing.*;
import java.io.*;

public class Img extends JFrame {
	public static void main( String[] args ) throws IOException {
		new Img( new File( args[0] ) );
	}

	private Img( File file ) throws IOException {
		super( "Img: " + file.getName () );
		ImageIcon img = new ImageIcon( ImageIO.read( file ) );
		JLabel label = new JLabel( img );
		label.setPreferredSize( new Dimension( img.getIconWidth(), img.getIconHeight() ) );
		add( label );
		setDefaultCloseOperation( EXIT_ON_CLOSE );
		pack();
		setVisible( true );
	}
}

huangapple
  • 本文由 发表于 2020年3月15日 06:36:11
  • 转载请务必保留本文链接:https://java.coder-hub.com/60687940.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定