java 如何在java中获取屏幕DPI?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6544510/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 16:18:13  来源:igfitidea点击:

How to get the screen DPI in java?

javascreen-resolution

提问by coder

I am developing an app for which I need the screen DPI.. I checked a few forums and got a code snippet which goes as follows:

我正在开发一个需要屏幕 DPI 的应用程序。我检查了几个论坛并得到了一个代码片段,如下所示:

Dimension screen = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
System.out.println("screen width: "+screen.getWidth()); 
System.out.println("screen height: "+screen.getHeight()); 
int pixelPerInch=java.awt.Toolkit.getDefaultToolkit().getScreenResolution(); 
System.out.println("pixelPerInch: "+pixelPerInch); 

double height=screen.getHeight()/pixelPerInch; 
double width=screen.getWidth()/pixelPerInch; 
double x=Math.pow(height,2); 
double y=Math.pow(width,2); 

But whatever be the value of my screen resolution, the pixelPerInchvalue remains the same at 96. What is the problem with the code??

但是不管我的屏幕分辨率是多少,这个pixelPerInch值还是96。代码有什么问题??

I got another swtcode for the same thing which goes as follows:

我得到swt了同样的事情的另一个代码,如下所示:

  import org.eclipse.swt.graphics.Device;
  import org.eclipse.swt.widgets.Display;
  import org.eclipse.swt.widgets.Shell;

  public class MainClass {
  public void run() {
  Display display = new Display();
  Shell shell = new Shell(display);
  shell.setText("Display Device");
  createContents(shell);
  shell.pack();
  shell.open();
  while (!shell.isDisposed()) {
    if (!display.readAndDispatch()) {
      display.sleep();
    }
  }
  display.dispose();
}

private void createContents(Shell shell) {
  Device device = shell.getDisplay();

  System.out.println("getBounds(): "+ device.getBounds());
  System.out.println("getClientArea(): "+device.getClientArea());
  System.out.println("getDepth(): "+device.getDepth());
  System.out.println("getDPI(): "+device.getDPI());

  device.setWarnings(true);
  System.out.println("Warnings supported: "+device.getWarnings());
}

public static void main(String[] args) {
  new MainClass().run();
}

But again here also whatever be my screen resolution, the getDPI()returns the same value of 96.. What is going wrong?? Is my code wrong or am I interpreting it in a wrong way??

但同样,无论我的屏幕分辨率如何,getDPI()返回的值都是 96 .. 出了什么问题?我的代码是错误的还是我以错误的方式解释它?

采纳答案by Barry Brown

The problem is no one, not even the OS, knows the exact physical dimensions of the screen. You'd need to know that in order to calculate the PPI.

问题是没有人知道屏幕的确切物理尺寸,甚至操作系统也不知道。您需要知道这一点才能计算 PPI。

There's a display setting in the control panel where the user can manually specify the PPI and by default it's set to 96.

控制面板中有一个显示设置,用户可以在其中手动指定 PPI,默认设置为 96。

回答by deviant

this code works for me on win10

这段代码在win10上对我有用

import javafx.stage.Screen 

double getScaleFactor() {
        double trueHorizontalLines = Toolkit.getDefaultToolkit().getScreenSize().getHeight();
        double scaledHorizontalLines = Screen.getPrimary().getBounds().getHeight();
        double dpiScaleFactor = trueHorizontalLines / scaledHorizontalLines;
        return dpiScaleFactor;
    }

it uses some awtapis though

awt虽然它使用了一些api