java 通过长宽乘积判断对应分辨率的方法
时间: 2023-08-05 19:02:47 浏览: 55
可以通过以下代码来获取屏幕分辨率:
```java
DisplayMetrics dm = getResources().getDisplayMetrics();
int screenWidth = dm.widthPixels;
int screenHeight = dm.heightPixels;
```
然后可以通过长宽乘积来判断对应的分辨率,例如:
```java
int screenDensity = dm.densityDpi;
int screenInches = (int) Math.sqrt(Math.pow(screenWidth, 2) + Math.pow(screenHeight, 2)) / screenDensity;
if (screenInches < 7) {
// 手机屏幕
} else {
// 平板屏幕
}
```
在这个例子中,如果屏幕的对角线长度小于7英寸,则判断为手机屏幕,否则判断为平板屏幕。
相关问题
java base64图片获取长宽
可以使用Java中的ImageIO类和BufferedImage类获取Base64编码的图片的长宽。具体实现可以参考以下代码:
```
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Base64;
import javax.imageio.ImageIO;
public class Base64Image {
public static void main(String[] args) throws IOException {
String base64Image = "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEASABIAAD/..."; String[] base64Parts = base64Image.split(",");
String base64Data = base64Parts[1];
byte[] imageData = Base64.getDecoder().decode(base64Data);
BufferedImage bufImg = ImageIO.read(new ByteArrayInputStream(imageData));
int width = bufImg.getWidth();
int height = bufImg.getHeight();
System.out.println("Width: " + width);
System.out.println("Height: " + height);
}
}
```
java excel单元格长宽跟像素的换算
在Java中,单元格的宽度和高度可以用像素或者字符来表示。Excel中默认情况下,每个单元格的宽度为8.43个字符,每个字符的宽度为7像素,每个单元格的高度为15个像素。
因此,可以使用以下公式将单元格的宽度和高度从像素转换为字符:
```java
double widthInPixels = ...; // 单元格宽度(像素)
int widthInChars = (int) Math.floor((widthInPixels / 7) * 100 + 0.5) / 100;
double heightInPixels = ...; // 单元格高度(像素)
int heightInPoints = (int) Math.floor((heightInPixels / 15) * 72 + 0.5);
```
其中,widthInChars是单元格宽度(字符),heightInPoints是单元格高度(磅)。需要注意的是,这里使用了Math.floor()函数来对结果进行向下取整,并且在计算widthInChars时还进行了四舍五入。
同样地,也可以使用以下公式将单元格的宽度和高度从字符转换为像素:
```java
int widthInChars = ...; // 单元格宽度(字符)
double widthInPixels = widthInChars * 7;
int heightInPoints = ...; // 单元格高度(磅)
double heightInPixels = (heightInPoints / 72) * 15;
```
注意,这里将单元格高度从磅转换为像素时,需要先将其除以72来得到英寸,再乘以15来得到像素。