java中如何获取屏幕DPI?

发布于 2024-11-18 00:46:53 字数 1696 浏览 4 评论 0原文

我正在开发一个需要屏幕 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); 

但是无论我的屏幕分辨率的值是什么,pixelPerInch 值仍然存在96 也一样。代码有什么问题?

我得到了另一个 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();
}

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

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:

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 pixelPerInch value remains the same at 96. What is the problem with the code??

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

  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??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

西瓜 2024-11-25 00:46:53

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

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

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.

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.

猥︴琐丶欲为 2024-11-25 00:46:53

这段代码在 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;
    }

但它使用了一些 awt api

this code works for me on 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 awt apis though

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文