【问题标题】:Why image is rotated and resized instead of resized only?为什么图像被旋转和调整大小而不是仅调整大小?
【发布时间】:2019-06-27 20:58:59
【问题描述】:

我想调整图像大小而不旋转图像 这是我的代码。

Image temp = image.getScaledInstance(width, height,ImageScale.SCALE_SMOOTH);
BufferedImage resized = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = resized.createGraphics();
g2d.drawImage(temp, 0, 0, width, height, null);
g2d.dispose();
return resized;

【问题讨论】:

  • 我实际上对您的代码进行轮换没有任何问题。图像在没有旋转的情况下调整大小(然后我将其写入输出文件)。您能否发布整个课程/代码?在给定代码之后看起来有问题,即当您处理调整大小的图像时。
  • 我曾经遇到过类似的问题,真是令人难以置信。原来是由指定旋转的手机添加的 Exif 元数据。因此,在浏览器或桌面上查看图像时,它看起来很正常。但在 Java 中,它没有看到这个元数据,实际上是颠倒的……仍然需要代码,但可能是一个考虑因素。 en.wikipedia.org/wiki/Exif#Example

标签: java image-rotation


【解决方案1】:

当您使用图像调整大小时,它正在写入的新图像不包含任何 EXIF 数据,因此它显示为原始图像,这使它看起来像是在它的一侧。

我有一个示例代码向您展示如何保留 Exif 数据:

public class ImageData {

public byte[] resize(int maxDimension, File imageFile) throws IOException {
    DataInputStream inStream = null;
    try {
        inStream = new DataInputStream(
                new BufferedInputStream(
                        new FileInputStream(imageFile)));

        byte[] imageData = IOUtils.toByteArray(inStream);

        BufferedImage image = readImage(imageData);

        TiffImageMetadata metadata = readExifMetadata(imageData);

        image = Scalr.resize(image, maxDimension);

        if (metadata != null) {
            imageData = writeExifMetadata(metadata, writeJPEG(image));

        } else {
            imageData = writePNG(image);
        }

        return imageData;

    } catch (IOException | ImageReadException | ImageWriteException e) {
        log.error("image resize failed", e);
        return null;
    } finally {
        assert inStream != null;
        inStream.close();
    }
}

private TiffImageMetadata readExifMetadata(byte[] jpegData) throws ImageReadException, IOException {
    ImageMetadata imageMetadata = Imaging.getMetadata(jpegData);
    if (imageMetadata == null) {
        return null;
    }
    JpegImageMetadata jpegMetadata = (JpegImageMetadata)imageMetadata;
    return jpegMetadata.getExif();
}

private byte[] writeExifMetadata(TiffImageMetadata metadata, byte[] jpegData)
        throws ImageReadException, ImageWriteException, IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    new ExifRewriter().updateExifMetadataLossless(jpegData, out, metadata.getOutputSet());
    out.close();
    return out.toByteArray();
}

private BufferedImage readImage(byte[] data) throws IOException {
    return ImageIO.read(new ByteArrayInputStream(data));
}

private byte[] writeJPEG(BufferedImage image) throws IOException {
    ByteArrayOutputStream jpegOut = new ByteArrayOutputStream();
    ImageIO.write(image, "JPEG", jpegOut);
    jpegOut.close();
    return jpegOut.toByteArray();
}

/**
 *
 * @param image
 * @return byte[]
 * @throws IOException
 * This method calls when the metadata of a JPEG image is null,
 * it will convert the JPEG image to PNG
 */
private byte[] writePNG(BufferedImage image) throws IOException {
    ByteArrayOutputStream pngOut = new ByteArrayOutputStream();
    ImageIO.write(image,"PNG",  pngOut);
    pngOut.close();
    return pngOut.toByteArray();
}

}

你可以像这样使用这个类:

 public String convertImageToThumbnail(String path) {
    try {
        File imageFile = new File(path);
        byte[] imageBytes;
        String ex = path.substring(path.lastIndexOf(".")+1);

       ImageData imageData = new ImageData();
       imageBytes = imageData.resize(50, imageFile);
        
        String output = Base64.getEncoder().encodeToString(imageBytes);
        return "data:image/"+ex+";base64," + output;
    } catch (Exception e) {
        log.error("Converting to thumbnail failed", e);
        return null;
    }
}

您可以使用此示例调整并保留 exif 数据。 希望对你有用

【讨论】:

    猜你喜欢
    • 2017-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-11
    • 2016-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多