java获取图片尺寸

/*** 本地获取* */@Testpublic void testImg2() throws IOException{File picture = new File("D:/5G.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()); // 源图高度}/*** 获取服务器上的* @throws FileNotFoundException* @throws IOException*/@Testpublic void getImg() throws FileNotFoundException, IOException{URL url = new URL("https://img-bss.csdn.net/1603118730831.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);}/*** 获取服务器上的* * @throws IOException*/@Testpublic void testImg1() throws IOException{InputStream murl = new URL("https://img-bss.csdn.net/1603118730831.jpg").openStream();BufferedImage sourceImg =ImageIO.read(murl);   System.out.println(sourceImg.getWidth());   // 源图宽度System.out.println(sourceImg.getHeight());   // 源图高度}


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部