通过浏览器以编程方式确定 DPI?
我想以编程方式确定用户显示器的 DPI,以便以精确的单位数(厘米/英寸)显示网页。 我知道这是一个奇怪的请求:这是一个可视化研究项目,它是一种控制。 目前,我们通过让用户将信用卡放置在屏幕上并将可调整大小的 div(通过 Mootools)与真实信用卡相匹配来实现此目的,瞧,我们可以获得 DPI 并正确显示页面。
有人能想到一种编程方式来做到这一点吗?
I would like to programmaticaly determine the DPI of a user's display in order to show a web page at a precise number of units (centimeters/inches). I know it a weird request: it's for a visualization research project and it's kind of a control. We currently do it by having the user place a credit card to the screen and match a resizable div (via Mootools) to the real credit card, and voila we can get the DPI and display the page correctly.
Can anyone think of a programmatic way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果目标计算机上没有运行代码,则无法通过浏览器实现此目的。 如果您可以在目标计算机上安装某些内容,您也许能够确定有关显示器的足够信息来计算此值。
It's not possible from a browser without code running on the target computer. If you could install something on the target computer you might be able to determine enough about the monitor to calculate this.
我认为你不会得到精确的结果 - 例如你可以使用显示器调整图片大小。 我宁愿坚持使用用户驱动的方法,尽管您可以检测屏幕分辨率以进行初步估计。
I think that you won't get precise results - for example you can resize the picture using the monitor. I'd rather stick with the user-driven method, although you can f.e. detect screen resolution for a first estimate.
你不能。 DPI 是屏幕像素分辨率和显示器物理尺寸的函数。 后者在我所知道的任何浏览器界面中都不可用。
You can't. DPI is a function of the pixel resolution of the screen and the physical dimensions of the display. The latter is not available in any browser interface I am aware of.
如果您在 javascript/mootools 中执行此操作,CSS 单元 就是您的朋友。 您可以以英寸或厘米甚至磅(1/72 英寸)为单位指定尺寸。 如果您出于某种原因绝对需要 DPI,即使您可以以这些单位指定尺寸,只需将您正在使用的“可调整大小”div 调整为已知尺寸,然后根据转换后的像素尺寸进行计算即可。
编辑:
这是一个很好的观点; 浏览器倾向于通过假设默认 DPI(我认为是 72 或 96)并遵循该值来伪造它。
好吧,如果您需要像您所要求的那样精确调整尺寸,那么您就不走运了。 如果无法读取浏览器所在显示器的当前分辨率和该显示器的“可视屏幕区域”,您将永远无法获得它。 你不可能通过 Javascript 得到它。
我建议您根据 CSS 单位做出浏览器默认的假设和大小。 然后,您可以允许用户使用您提到的方法“调整大小”,但仅在必要时。 在单独的页面上执行此操作,并将 DPI 计算存储为用户会话的一部分或作为 cookie。
或者,一旦 Microsoft 修复了此错误,您就可以使用 Silverlight 的“设备独立单位”进行精确缩放。
If you're doing this in javascript/mootools, CSS units are your friend. You can specify sizes in inches or centimeters, or even points (1/72 of an inch). If you absolutely need DPI for whatever reason even though you can specify sizes in those units, simply size the "resizable" div you are using to a known size and calculate from the translated pixel size.
Edit:
That's a good point; browsers tend to fake it by assuming a default DPI (I think 72 or 96) and going with that.
Well, if you need precision sizing like you're asking for, you're out of luck. You'll never be able to get it without being able to read both the current resolution of the monitor the browser is on and the "viewable screen area" of that monitor. Ain't no way you're gonna get that via Javascript.
I suggest that you make the default assumption that browsers do and size according to the CSS units. You can then allow your users to "adjust the sizing" using the method you mentioned, but only if it's necessary. Do this with on a separate page with the DPI calculation stored as part of the users session or as a cookie.
Alternatively, once Microsoft fixes this bug, you could use Silverlight's "device independent units" for accurate scaling.