Java:BufferedImage 上的 getHeight() 返回图像宽度,getWidth() 返回图像高度

vam*_*ddy 5 java metadata image

我正在使用 Java 进行 Jpeg 压缩来压缩图像,然后在存储之前调整它们的大小。我将存储高度保持为480并根据纵横比计算height,以便保持原始height:width比率相同。

这是我正在使用的代码

String inputImagePath = "1.JPG";
String outputImagePath = "Test Compression\\" + "1.JPG";

File imageFile = new File(inputImagePath);
File compressedImageFile = new File(outputImagePath);

int height = 640;

System.out.print("Conversion Start!\n");

if (imageFile != null)
{

    InputStream is = new FileInputStream(imageFile);
    OutputStream os = new FileOutputStream(compressedImageFile);

    float quality = 0.2 f;

    BufferedImage image = ImageIO.read(is);

    double aspectRatio = (double) image.getWidth() / image.getHeight();

    width = (int)(height * aspectRatio);
    System.out.println("Original height = " + image.getHeight() + " Original  width = " + image.getWidth());
    System.out.println(" aspect ratio = " + aspectRatio);
    System.out.println("height = " + height + " width = " + width + " aspect ratio = " + aspectRatio);

    BufferedImage resizedImage = new BufferedImage(width, height, image.getType());
    Graphics2D g = resizedImage.createGraphics();
    g.drawImage(image, 0, 0, width, height, null);
    g.dispose();

    Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("jpg");

    if (!writers.hasNext())
        throw new IllegalStateException("No writers found");

    ImageWriter writer = (ImageWriter) writers.next();
    ImageOutputStream ios = ImageIO.createImageOutputStream(os);
    writer.setOutput(ios);

    ImageWriteParam param = writer.getDefaultWriteParam();
    param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
    param.setCompressionQuality(quality);

    writer.write(null, new IIOImage(resizedImage, null, null), param);
}

System.out.print("Conversion compete!");
Run Code Online (Sandbox Code Playgroud)

这是图像元数据

这里宽度= 1920,高度= 2560

以下是打印的内容:

String inputImagePath = "1.JPG";
String outputImagePath = "Test Compression\\" + "1.JPG";

File imageFile = new File(inputImagePath);
File compressedImageFile = new File(outputImagePath);

int height = 640;

System.out.print("Conversion Start!\n");

if (imageFile != null)
{

    InputStream is = new FileInputStream(imageFile);
    OutputStream os = new FileOutputStream(compressedImageFile);

    float quality = 0.2 f;

    BufferedImage image = ImageIO.read(is);

    double aspectRatio = (double) image.getWidth() / image.getHeight();

    width = (int)(height * aspectRatio);
    System.out.println("Original height = " + image.getHeight() + " Original  width = " + image.getWidth());
    System.out.println(" aspect ratio = " + aspectRatio);
    System.out.println("height = " + height + " width = " + width + " aspect ratio = " + aspectRatio);

    BufferedImage resizedImage = new BufferedImage(width, height, image.getType());
    Graphics2D g = resizedImage.createGraphics();
    g.drawImage(image, 0, 0, width, height, null);
    g.dispose();

    Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("jpg");

    if (!writers.hasNext())
        throw new IllegalStateException("No writers found");

    ImageWriter writer = (ImageWriter) writers.next();
    ImageOutputStream ios = ImageIO.createImageOutputStream(os);
    writer.setOutput(ios);

    ImageWriteParam param = writer.getDefaultWriteParam();
    param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
    param.setCompressionQuality(quality);

    writer.write(null, new IIOImage(resizedImage, null, null), param);
}

System.out.print("Conversion compete!");
Run Code Online (Sandbox Code Playgroud)

我将代码应用到其他实际存在的图像上,width > height并且没有旋转问题。此旋转问题仅发生在图像中 据height > width我所知,我的代码中没有任何问题,我一定缺少与 getHeight() 和 getWidth() 函数相关的内容。请帮帮我

Ser*_*nev 3

jpeg 图像中可能存在 Java 默认无法识别的旋转元数据。

它看起来像:Orientation : rotate 90在 jpeg 属性中。

您需要第 3 方库来处理它,请参阅如何根据方向元数据旋转 JPEG 图像?