如何翻转和保存图像

时间:2012-12-21 03:15:06

标签: java image bufferedimage flip imageicon

我的图像声明:

ImageIcon imageIcon1 = new ImageIcon(main.class.getResource("image1.png"));
Image image1 = imageIcon1.getImage();

如何拍摄image1,沿着垂直轴翻转并将其保存为另一张图像?

我已经google了,我发现的每个解决方案都带有某种类型的转换错误。 另外,如果有更有效的方式来声明我的形象,请告诉我。

2 个答案:

答案 0 :(得分:8)

你说:

  

我用google搜索,我发现的每个解决方案都有某种类型的投射错误。

这只告诉我们你做错了什么但没告诉我们什么,限制我们如何帮助你。我只能告诉你一些对我有用的步骤:

  • 创建另一个与第一个相同的BufferedImage,
  • 通过createGraphics()
  • 获取其Graphics2D上下文
  • 通过AffineTransform.getScaleInstance(-1, 1)翻转图形进行水平翻转
  • 不要忘记转换变换以将翻转的图像带到您想要的位置。
  • 将旧图像绘制到新图像
  • 处理Graphics2D对象。

如果您需要更多帮助,请向我们展示您尝试过的内容,并附上所有错误消息。

例如,我在玩镜像精灵图像的同时玩这个。编译并运行它以查看我的意思:

import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.event.*;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.*;

public class FlipViaTransform {
   private static final String SPRITE_SHEET_SPEC = "http://www.funorb.com/img/images/game/"
         + "central/dev_diary/sprite_sheet_full.gif";
   private static final int TIMER_DELAY = 200;
   private static final int SPRITE_ROWS = 8; // an 8 x 8 sprite sheet

   public static void main(String[] args) {
      try {
         URL spriteSheetUrl = new URL(SPRITE_SHEET_SPEC);
         BufferedImage spriteSheet = ImageIO.read(spriteSheetUrl);
         final ImageIcon[] iconsA = new ImageIcon[64];
         final ImageIcon[] iconsB = new ImageIcon[64];
         double wD = (double) spriteSheet.getWidth() / SPRITE_ROWS;
         double hD = (double) spriteSheet.getHeight() / SPRITE_ROWS;
         int w = (int) wD;
         int h = (int) hD;

         // *** here's what I used to flip
         AffineTransform at = AffineTransform.getScaleInstance(-1, 1); // *** flip
         at.translate(-wD, 0);  // *** translate so that flipped image is visible
         for (int i = 0; i < SPRITE_ROWS; i++) {
            for (int j = 0; j < SPRITE_ROWS; j++) {
               int x = (int) (i * wD);
               int y = (int) (j * hD);
               BufferedImage imgA = spriteSheet.getSubimage(x, y, w, h);
               BufferedImage imgB = new BufferedImage(imgA.getWidth(),
                     imgA.getHeight(), BufferedImage.TYPE_INT_ARGB);
               Graphics2D g2 = imgB.createGraphics();
               g2.setTransform(at);   // *** transform
               g2.drawImage(imgA, 0, 0, null);  // *** draw old image into new
               g2.dispose();  // *** get rid of graphics2d object

               iconsA[j * SPRITE_ROWS + i] = new ImageIcon(imgA);
               iconsB[j * SPRITE_ROWS + i] = new ImageIcon(imgB);
            }
         }

         final JLabel labelA = new JLabel("Image");
         final JLabel labelB = new JLabel("Mirror Image");

         labelA.setVerticalTextPosition(JLabel.BOTTOM);
         labelB.setVerticalTextPosition(JLabel.BOTTOM);
         labelA.setHorizontalTextPosition(JLabel.CENTER);
         labelB.setHorizontalTextPosition(JLabel.CENTER);
         labelA.setIcon(iconsA[0]);
         labelB.setIcon(iconsB[0]);
         final JPanel panel = new JPanel(new GridLayout(1, 0));
         panel.add(labelA);
         panel.add(labelB);
         Timer spriteTimer = new Timer(TIMER_DELAY, new ActionListener() {
            int spriteIndex = 0;

            @Override
            public void actionPerformed(ActionEvent arg0) {
               labelA.setIcon(iconsA[spriteIndex]);
               labelB.setIcon(iconsB[spriteIndex]);
               spriteIndex++;
               spriteIndex %= iconsA.length;
            }
         });
         spriteTimer.start();
         JOptionPane.showMessageDialog(null, panel, "AffineTransform Example", JOptionPane.PLAIN_MESSAGE);
         spriteTimer.stop();
      } catch (MalformedURLException e) {
         e.printStackTrace();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

显示为:
enter image description here

答案 1 :(得分:0)

我从来没有这样做过,但你可以尝试研究一下你是否可以使用像ImageMagick这样的3D派对库或者使用带有Java的GraphicsMagic。这些库可以读取PNG图像并对它们执行图形操作。