有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java缓冲区图像宽度和高度随机反转值


我需要知道一个图像是面向的还是面向的,因为我得到了图像的宽度高度,但有时值显示为反转

这是我用来检测图像是否为横向的代码:
public class ProcessImage{
    private static final String IMAGE_ORIENTATION_ERROR ="001";

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

            File file = new File("C:\\myFolder\\nothing.jpg");
            byte[] fileContent = Files.readAllBytes(file.toPath());
            BufferedImage image = createImageFromBytes(fileContent);
            validateImage(image);
            System.out.println(image.getWidth());
        } catch (IOException | ImageOrientationException e) {
            e.printStackTrace();
        }

    }

    private static BufferedImage createImageFromBytes(byte[] imageData) {
        ByteArrayInputStream bais = new ByteArrayInputStream(imageData);
        try {
            return ImageIO.read(bais);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /* Here is where i validate if the image is landscape
       But Values of image.getWidth() and image.getHeight() appear as inverted.*/
    private static void validateImage (BufferedImage image)
             throws ImageOrientationException{
         //if this happens the picture is NOT landscape 
         if (image.getWidth() < image.getHeight()) {
            throw new ImageOrientationException(IMAGE_ORIENTATION_ERROR,
                     "error oriented picture");
          }
}

}

  

我遇到的问题是,对于某些图像,的值是反向的
例如,对于宽度为3024且高度为4032的图像,BufferImage返回的宽度和高度值为宽度为4032且高度为3024。但是,如果我在Paint中编辑相同的图像(相对于原始大小),BufferImage将返回宽度:3024高度:4032(按原样)。
我已经用相同宽度和高度的其他图像进行了测试,BufferImage获得了rigth值
你知道为什么会这样吗?有没有办法知道这张照片是否真的是面向风景的
下面是一个与有问题的图像的链接:http://www.mediafire.com/file/n1a8uhy195zgesd/nothing.jpg/file
提前谢谢


共 (0) 个答案