Java 获取图片的大小、宽、高

java


参考:https://www.cnblogs.com/hongten/archive/2012/11/26/hongten_java_ImageReader_BufferedImage.html 

 1 import java.awt.image.BufferedImage;

2 import java.io.File;

3 import java.io.FileInputStream;

4 import java.io.FileNotFoundException;

5 import java.io.IOException;

6

7 import javax.imageio.ImageIO;

8

9 public class Picture {

10 public static void main(String[] args) throws FileNotFoundException, IOException {

11 File picture = new File("E:/PrintScreen/StarSky.jpg");

12 BufferedImage sourceImg = ImageIO.read(new FileInputStream(picture));

13

14 System.out.println(String.format("Size: %.1f KB", picture.length()/1024.0));

15 System.out.println("Width: " + sourceImg.getWidth());

16 System.out.println("Height: " + sourceImg.getHeight());

17 }

18 }

这个没看懂!

 1 import java.io.File;

2 import java.io.IOException;

3 import java.util.Iterator;

4

5 import javax.imageio.ImageIO;

6 import javax.imageio.ImageReader;

7 import javax.imageio.stream.ImageInputStream;

8

9 public class Picture {

10 public static void main(String[] args) {

11 String srcPath = "E:/PrintScreen/1.jpg";

12

13 File file = new File(srcPath);

14 try {

15 Iterator<ImageReader> readers = ImageIO.getImageReadersByFormatName("jpg");

16 ImageReader reader = (ImageReader) readers.next();

17 ImageInputStream iis = ImageIO.createImageInputStream(file);

18 reader.setInput(iis, true);

19 System.out.println("width: " + reader.getWidth(0));

20 System.out.println("height: " + reader.getHeight(0));

21 } catch (IOException e) {

22 e.printStackTrace();

23 }

24 }

25 }

##########################################################################

注意:图片是预先存放在Java Project下的Package中

 1 import java.awt.Image;

2 import java.awt.image.BufferedImage;

3 import java.io.IOException;

4 import java.net.URL;

5

6 import javax.imageio.ImageIO;

7

8 public class GetImageSize {

9 public static void main(String[] args) throws IOException {

10 BufferedImage bi = null;

11

12 try {

13 URL u = GetImageSize.class.getClassLoader().getResource("images/background.png");

14 bi = ImageIO.read(u);

15 } catch (IOException e) {

16 e.printStackTrace();

17 }

18 Image img = bi;

19

20 System.out.println(img.getWidth(null));

21 System.out.println(img.getHeight(null));

22 }

23 }

以上是 Java 获取图片的大小、宽、高 的全部内容, 来源链接: utcz.com/z/390943.html

回到顶部