检测DPI,计算监视器的物理尺寸 [英] Detect DPI, calculate physical dimension of monitor

查看:73
本文介绍了检测DPI,计算监视器的物理尺寸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我想获取显示器的显示尺寸..

可以使用Flash的JavaScript吗?

我尝试了以下功能:

Hi all,

I want to get the Display Dimensions of my Monitor..

Is it possible to do with JavaScript of Flash?

I tried the following function:

var dpi_x = document.getElementById(''testdiv'').offsetWidth;
        var dpi_y = document.getElementById(''testdiv'').offsetHeight;
        var width_in = screen.width / dpi_x;
        var height_in = screen.height / dpi_y;
        var diagonal_in = Math.round(10 * Math.sqrt(width_in * width_in + height_in * height_in)) / 10;



但是它不显示固定的对角线值..因为当我更改显示分辨率时..

我希望固定尺寸如16.5....我的意思是应该为任何特定的显示器固定尺寸..



but it doesnt shows the fixed diagonal value..because when i change the resolution of display..

Where as i want the fixed dimension like 16.5"....i mean it should be fixed for any specific monitor..

推荐答案

use below code

screen.width;
screen.height;


尝试查询

此处你去,这会做到的.

我的屏幕是13.5 x 7.5英寸-它是15.3英寸的屏幕.
这(错误)将其计算为14.22 x 8-或16.3"
不知道为什么会给出错误的结果-它给出的dpi为96,听起来不错,并在我的显示设置中签出. (**请参阅下面的编辑以获取更新**)

正如我之前所暗示的,我只是将元素的大小设置为1"x1",然后得到它的大小(以像素为单位),并根据像素/英寸和屏幕计算出屏幕的物理尺寸.分辨率.

随着屏幕分辨率的变化,如果屏幕模式在屏幕的侧面具有黑色的竖线,则返回的大小也会改变.也就是说,它可以测量包含内容的屏幕的大小.我将其设置为1024 * 768,并在屏幕的任一侧形成了约150像素的黑条.垂直大小保持不变,但水平&对角线尺寸缩小.

Here you go, this will do it.

My screen is 13.5 x 7.5 inches - it''s a 15.3" screen.
This (wrongly) calculates it as 14.22 x 8 - or 16.3"
Don''t know why it gives the wrong result - it gives the dpi as 96 which sounds right and checks out in my display settings. (** See edit below for update **)

As I hinted earlier, I''ve just set the size of an element to be 1"x1" I then get it''s size in pixels and calculate the screen''s physical size based on the pixels/inch and the screen resolution.

As screen resolution changes, the size returned changes if the screen-mode has black vertical bars on the sides of the screen. That''s to say it measures the size of the screen that contains content. I set 1024*768 and got ~ 150pixels of black bars on either side of the screen. The vertical size stayed the same, but the horizontal & diagonal dimensions shrunk.

<!DOCTYPE html>
<html>
<style>
#myDiv
{
	width: 1in;
	height: 1in;
	padding: none;
	margin: none;
	background-color: black;
}
</style>
<script>

function getSize()
{
	var tgt = document.getElementById('myDiv');
	tgtX = tgt.clientWidth;//offsetWidth;
	tgtY = tgt.clientHeight;//offsetHeight;
	
	var width_in = screen.width / tgtX;
    var height_in = screen.height / tgtY;
	
	width_in = parseInt(width_in * 100);
	width_in /= 100;
	
	diag = Math.sqrt( width_in*width_in + height_in*height_in);
	
	//alert('Div size: ' + tgtX + "x" + tgtY + " px.");
	alert('Screen size: ' + width_in + "\" x " + height_in + "\"  - Diagonal: " + diag + "\"");
}
</script>
</head>
<body onload='getSize();'>
	<div id='myDiv'></div>
<body>
</html>



将宽度和高度设置为2.7cm而不是1in表示正方形在我的屏幕上为1英寸.然后,这或多或少地给出了正确的结果. 13.39英寸x 7.52英寸(15.36英寸直径)



Setting the width and height to 2.7cm instead of 1in means the square measures 1inch on my screen. This then gives more or less the right results. 13.39" x 7.52" (15.36" diag)


这篇关于检测DPI,计算监视器的物理尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆