简体   繁体   English

ImageIO.write 仅在 Java 中的 png 中起作用的镜像图像

[英]ImageIO.write a mirrored image only functional in png in Java

I was using ImageIO to write a mirror image to the local file.我正在使用 ImageIO 将镜像写入本地文件。 However, I found that the mirrored image file would be created only if I wrote the image to png file.但是,我发现只有将图像写入 png 文件时才会创建镜像文件。 Please note that the original image was in jpg format.请注意,原始图像为 jpg 格式。 If I change the ImageIO.write format to "jpg" or some formats other than "png", no image file would be created.如果我将 ImageIO.write 格式更改为“jpg”或“png”以外的其他格式,则不会创建图像文件。

BufferedImage image = null;
URL url = null;
String link = "http://www.fullerton.edu/_resources/images/empowerment-james.jpg";
try {

    url = new URL(link);
    image = ImageIO.read(url);
    width = image.getWidth();
    height = image.getHeight();

    BufferedImage mirror = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

    for (int y = 0; y < height; y++) {
        for (int lx = 0, rx = width - 1; lx < width; lx++, rx--) {
            int lp = image.getRGB(lx, y);

            mirror.setRGB(rx, y, lp);

        }
    }
    JFrame frame2 = new JFrame();
    frame2.setSize(width + offset, height + offset);
    JLabel label2 = new JLabel(new ImageIcon(mirror));
    frame2.add(label2);
    frame2.setVisible(true);
    file = new File("mirror.png");

    ImageIO.write(mirror, "png", file);
} catch (IOException e) {
    System.out.println(e.getMessage());
}

So, beware, not all image formats will support transparency (ie BufferedImage.TYPE_INT_ARGB ), so, make sure you actually know where the file is been written, it might "appear" as it's not getting written, but it might be getting written to some place you're not expecting.因此,请注意,并非所有图像格式都支持透明度(即BufferedImage.TYPE_INT_ARGB ),因此,请确保您确实知道文件的写入位置,它可能会“出现”,因为它没有被写入,但它可能会被写入某个你没想到的地方。

So, the following example works just fine, pay attention to the output at the end of the file, it's telling the directory the images were written to.所以,下面的例子工作得很好,注意文件末尾的输出,它告诉了图像写入的目录。

import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class Main {
    public static void main(String[] args) {
        try {
            new Main();
        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public Main() throws MalformedURLException, IOException {
        BufferedImage original = ImageIO.read(new URL("http://www.fullerton.edu/_resources/images/empowerment-james.jpg"));
        BufferedImage mirrored = new BufferedImage(original.getWidth(), original.getHeight(), original.getTransparency());
        Graphics2D g2d = mirrored.createGraphics();
        g2d.setTransform(AffineTransform.getScaleInstance(-1, 1));
        g2d.drawImage(original, -original.getWidth(), 0, null);
        g2d.dispose();

        JPanel panel = new JPanel();
        panel.add(new JLabel(new ImageIcon(original)));
        panel.add(new JLabel(new ImageIcon(mirrored)));

        JOptionPane.showMessageDialog(null, panel);

        System.out.println("Writing to " + System.getProperty("user.dir"));

        ImageIO.write(mirrored, "png", new File("Test.png"));
        ImageIO.write(mirrored, "jpg", new File("Test.jpg"));
    }


}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM