介绍
有时候我们需要根据截图的尺寸来判断设备的分辨率,下面有两种获取图片文件分辨率的方法。
1.本地获取
public static void main(String[] args) throws IOException {
File picture = new File("C:\\Users\\Administrator\\Desktop\\20230111153212081T3009.jpg");
BufferedImage sourceImg =ImageIO.read(new FileInputStream(picture));
System.out.println(String.format("%.1f",picture.length()/1024.0));// 源图大小
System.out.println(sourceImg.getWidth()); // 源图宽度
System.out.println(sourceImg.getHeight()); // 源图高度
}
2.获取服务器图片的尺寸(第一种)
public static void main(String[] args) throws IOException {
URL url = new URL("https://img.zcznbj.cn/oss-screen/2023/01/20230111153212081T3009.jpg");
URLConnection connection = url.openConnection();
connection.setDoOutput(true);
BufferedImage image = ImageIO.read(connection.getInputStream());
int srcWidth = image .getWidth(); // 源图宽度
int srcHeight = image .getHeight(); // 源图高度
System.out.println("srcWidth = " + srcWidth);
System.out.println("srcHeight = " + srcHeight);
}
3.获取服务器图片的尺寸(第二种)
public static void main(String[] args) throws IOException {
InputStream murl = new URL("https://img.zcznbj.cn/oss-screen/2023/01/20230111153212081T3009.jpg").openStream();
BufferedImage sourceImg =ImageIO.read(murl);
System.out.println(sourceImg.getWidth()); // 源图宽度
System.out.println(sourceImg.getHeight()); // 源图高度
}