首页 > 解决方案 > 在 Java 应用程序端控制图像质量

问题描述

我在这里发布了一个问题,询问如何处理 java 应用程序中的模糊图片。我发现我的窗口缩放导致​​模糊。现在我有一个更大的问题。我希望通过应用程序控制图像和应用程序的质量,如果可能的话,不必更改任何 Windows 设置。

不是将缩放更改回 100%,而是从这里有另一个 Windows 建议,说更改 java.exe 和/或 javaw.exe 的兼容性设置以覆盖高 DPI 设置以由系统执行缩放。它有效,因为图片更清晰,但文字失去了清晰度。

我为该应用程序找到的最佳质量是让它不覆盖高 DPI,而只是将缩放设置为 100%。文字清晰明了,图片也一样。

我试图弄清楚是否有一种方法可以保持这种质量,而不必让用户更改任何 Windows 设置以使应用程序看起来不错。我想知道代码中是否有办法实现这一点。

这是一个示例程序,我的缩放比例为 125% 图片模糊但文字很好。 在此处输入图像描述

这是一个 DPI 被系统覆盖的示例,缩放比例仍为 125%,请注意文本失去了清晰度,但图片更好。

在此处输入图像描述

最后缩放为 100%,并且 DPI 不再被覆盖。这是最好的文字和图片质量

在此处输入图像描述

这是示例代码,如果有人需要的话。

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Color;
import java.awt.Component;
import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import java.awt.Insets;
import java.awt.Font;
import java.awt.Dimension;
import javax.swing.JTextArea;

@SuppressWarnings("serial")
public class example extends JFrame {

private JPanel contentPane;

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                example frame = new example();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

public example() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 558, 373);
    contentPane = new JPanel();
    contentPane.setBackground(Color.WHITE);
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    GridBagLayout gbl_contentPane = new GridBagLayout();
    gbl_contentPane.columnWidths = new int[]{0, 0};
    gbl_contentPane.rowHeights = new int[]{0, 0};
    gbl_contentPane.columnWeights = new double[]{1.0, Double.MIN_VALUE};
    gbl_contentPane.rowWeights = new double[]{1.0, Double.MIN_VALUE};
    contentPane.setLayout(gbl_contentPane);

    JPanel panel = new JPanel();
    panel.setBackground(Color.WHITE);
    GridBagConstraints gbc_panel = new GridBagConstraints();
    gbc_panel.fill = GridBagConstraints.BOTH;
    gbc_panel.gridx = 0;
    gbc_panel.gridy = 0;
    contentPane.add(panel, gbc_panel);
    GridBagLayout gbl_panel = new GridBagLayout();
    gbl_panel.columnWidths = new int[]{0, 0, 0};
    gbl_panel.rowHeights = new int[]{0, 0, 0, 100, 0};
    gbl_panel.columnWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
    gbl_panel.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
    panel.setLayout(gbl_panel);

    JLabel lblWordsGoHere = new JLabel("Words go here");
    lblWordsGoHere.setFont(new Font("Cambria Math", Font.PLAIN, 18));
    GridBagConstraints gbc_lblWordsGoHere = new GridBagConstraints();
    gbc_lblWordsGoHere.insets = new Insets(0, 0, 5, 0);
    gbc_lblWordsGoHere.gridx = 1;
    gbc_lblWordsGoHere.gridy = 0;
    panel.add(lblWordsGoHere, gbc_lblWordsGoHere);

    Component horizontalStrut = Box.createHorizontalStrut(20);
    horizontalStrut.setPreferredSize(new Dimension(20, 20));
    GridBagConstraints gbc_horizontalStrut = new GridBagConstraints();
    gbc_horizontalStrut.insets = new Insets(0, 0, 5, 5);
    gbc_horizontalStrut.gridx = 0;
    gbc_horizontalStrut.gridy = 1;
    panel.add(horizontalStrut, gbc_horizontalStrut);

    JLabel lblNewLabel = new JLabel("More words here...");
    lblNewLabel.setFont(new Font("Cambria Math", Font.PLAIN, 14));
    GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
    gbc_lblNewLabel.insets = new Insets(0, 0, 5, 0);
    gbc_lblNewLabel.anchor = GridBagConstraints.WEST;
    gbc_lblNewLabel.gridx = 1;
    gbc_lblNewLabel.gridy = 1;
    panel.add(lblNewLabel, gbc_lblNewLabel);

    JLabel lblNewLabel_1 = new JLabel(new ImageIcon(example.class.getResource("/icons/Step3.png")));
    GridBagConstraints gbc_lblNewLabel_1 = new GridBagConstraints();
    gbc_lblNewLabel_1.insets = new Insets(0, 0, 5, 0);
    gbc_lblNewLabel_1.gridx = 1;
    gbc_lblNewLabel_1.gridy = 2;
    panel.add(lblNewLabel_1, gbc_lblNewLabel_1);

    JTextArea txtrLotsOfDescriptive = new JTextArea();
    txtrLotsOfDescriptive.setLineWrap(true);
    txtrLotsOfDescriptive.setWrapStyleWord(true);
    txtrLotsOfDescriptive.setEditable(false);
    txtrLotsOfDescriptive.setFont(new Font("Cambria Math", Font.PLAIN, 14));
    txtrLotsOfDescriptive.setText("Lorem ipsum dolor sit amet, eu tempor maluisset eum. Ipsum detracto mediocrem quo eu. Eos ad ocurreret argumentum. Cum ei vero ipsum alienum, has sonet impetus repudiandae at, cu viris assentior eum.");
    GridBagConstraints gbc_txtrLotsOfDescriptive = new GridBagConstraints();
    gbc_txtrLotsOfDescriptive.fill = GridBagConstraints.BOTH;
    gbc_txtrLotsOfDescriptive.gridx = 1;
    gbc_txtrLotsOfDescriptive.gridy = 3;
    panel.add(txtrLotsOfDescriptive, gbc_txtrLotsOfDescriptive);
}

}

标签: javawindows-10scalingdpi

解决方案


推荐阅读