将Graphics2D转换为图像或BufferedImage。

时间:2022-04-19 10:55:54

I have a little problem here.

我这里有个小问题。

I have an applet, where user can "draw" inside it. To do that, I use the java.awt.Graphics2D. But, how can I do to save the user draw image as a JPEG image, or at least, convert it to a BufferedImage or something? I don't know how to do that.

我有一个applet,用户可以在里面“画”。为此,我使用java.awt.Graphics2D。但是,如何才能将用户绘制成JPEG图像,或者至少将其转换为BufferedImage之类的呢?我不知道该怎么做。

Thanks.

谢谢。

4 个解决方案

#1


11  

Have them draw directly in a BufferedImage by way of it's Graphics2D object which you can get via getGraphics(). Then use ImageIO.write(...) to output the image to whatever file type you desire (and that's supported). The ImageIO API should help you with this: ImageIO API.

让他们直接用它的Graphics2D对象,通过getGraphics()得到一个BufferedImage。然后使用ImageIO.write(…)将图像输出到您想要的任何文件类型(并得到支持)。ImageIO API应该帮助您使用这个:ImageIO API。

The other issue you'll have is where are they supposed to save the image once it has been drawn? On their own computer? If so and this is an applet program, then the applet will need to be "signed" in order for it to have the permission to write to disk. If you're unsure on this, check out Google, this article, or you may wish to write a new question for this issue alone.

另一个问题是,一旦图像被绘制出来,他们应该在哪里保存图像呢?在自己的电脑吗?如果是这样的话,这是一个applet程序,那么applet将需要“签名”,以便它获得写入磁盘的权限。如果您对此不确定,请查看谷歌,这篇文章,或者您可能希望单独为这个问题写一个新的问题。

Edit 1: code example
For example:

编辑1:代码示例:

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

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

@SuppressWarnings("serial")
public class DrawAndSaveImage extends JPanel {
   private static final int BI_WIDTH = 600;
   private static final int BI_HEIGHT = BI_WIDTH;
   private static final Color LABEL_DRAW_COLOR = new Color(180, 180, 255);
   private static final Stroke LABEL_DRAW_STROKE = new BasicStroke(1);
   private static final Stroke BIMAGE_DRAW_STROKE = new BasicStroke(4);
   private static final int COLOR_DIV = 5;
   private BufferedImage bImage = new BufferedImage(BI_WIDTH, BI_HEIGHT,
            BufferedImage.TYPE_INT_RGB);
   private List<Point> pointList = new ArrayList<Point>();
   private JLabel imageLabel;
   private List<Color> colorList = new ArrayList<Color>();
   private Random random = new Random();

   public DrawAndSaveImage() {
      Graphics2D g2d = bImage.createGraphics();
      g2d.setBackground(Color.white);
      g2d.clearRect(0, 0, BI_WIDTH, BI_HEIGHT);
      g2d.dispose();

      for (int r = 0; r < COLOR_DIV; r++) {
         for (int g = 0; g < COLOR_DIV; g++) {
            for (int b = 0; b < COLOR_DIV; b++) {
               Color c = new Color((r * 255) / COLOR_DIV,
                        (g * 255) / COLOR_DIV, (b * 255) / COLOR_DIV);
               colorList.add(c);
            }
         }
      }

      imageLabel = new JLabel(new ImageIcon(bImage)) {
         @Override
         protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            paintInLabel(g);
         }
      };
      MyMouseAdapter myMouseAdapter = new MyMouseAdapter();
      imageLabel.addMouseListener(myMouseAdapter);
      imageLabel.addMouseMotionListener(myMouseAdapter);
      imageLabel.setBorder(BorderFactory.createEtchedBorder());

      JButton saveImageBtn = new JButton("Save Image");
      saveImageBtn.addActionListener(new ActionListener() {
         @Override
         public void actionPerformed(ActionEvent e) {
            saveImageActionPerformed();
         }
      });

      JButton clearImageBtn = new JButton("Clear Image");
      clearImageBtn.addActionListener(new ActionListener() {
         @Override
         public void actionPerformed(ActionEvent e) {
            Graphics2D g2 = bImage.createGraphics();
            g2.setBackground(Color.white);
            g2.clearRect(0, 0, BI_WIDTH, BI_HEIGHT);
            g2.dispose();
            imageLabel.repaint();
         }
      });

      JPanel btnPanel = new JPanel();
      btnPanel.add(saveImageBtn);
      btnPanel.add(clearImageBtn);

      setLayout(new BorderLayout());
      add(imageLabel, BorderLayout.CENTER);
      add(btnPanel, BorderLayout.SOUTH);
   }

   private void saveImageActionPerformed() {
      JFileChooser filechooser = new JFileChooser();
      FileNameExtensionFilter filter = new FileNameExtensionFilter(
               "JPG Images", "jpg");
      filechooser.setFileFilter(filter);
      int result = filechooser.showSaveDialog(this);
      if (result == JFileChooser.APPROVE_OPTION) {
         File saveFile = filechooser.getSelectedFile();
         try {
            ImageIO.write(bImage, "jpg", saveFile);
         } catch (IOException e) {
            e.printStackTrace();
         }
      }
   }

   private void paintInLabel(Graphics g) {
      Graphics2D g2d = (Graphics2D) g;
      g2d.setColor(LABEL_DRAW_COLOR);
      g2d.setStroke(LABEL_DRAW_STROKE);
      if (pointList.size() < 2) {
         return;
      }
      for (int i = 1; i < pointList.size(); i++) {
         int x1 = pointList.get(i - 1).x;
         int y1 = pointList.get(i - 1).y;
         int x2 = pointList.get(i).x;
         int y2 = pointList.get(i).y;
         g2d.drawLine(x1, y1, x2, y2);
      }
   }

   private class MyMouseAdapter extends MouseAdapter {

      @Override
      public void mousePressed(MouseEvent e) {
         pointList.add(e.getPoint());
         imageLabel.repaint();
      }

      @Override
      public void mouseReleased(MouseEvent e) {
         Graphics2D g2d = bImage.createGraphics();
         g2d.setColor(colorList.get(random.nextInt(colorList.size())));
         g2d.setStroke(BIMAGE_DRAW_STROKE);
         if (pointList.size() >= 2) {
            for (int i = 1; i < pointList.size(); i++) {
               int x1 = pointList.get(i - 1).x;
               int y1 = pointList.get(i - 1).y;
               int x2 = pointList.get(i).x;
               int y2 = pointList.get(i).y;
               g2d.drawLine(x1, y1, x2, y2);
            }
         }
         g2d.dispose();

         pointList.clear();
         imageLabel.repaint();
      }

      @Override
      public void mouseDragged(MouseEvent e) {
         pointList.add(e.getPoint());
         imageLabel.repaint();
      }
   }

   private static void createAndShowUI() {
      JFrame frame = new JFrame("DrawAndSaveImage");
      frame.getContentPane().add(new DrawAndSaveImage());
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      java.awt.EventQueue.invokeLater(new Runnable() {
         public void run() {
            createAndShowUI();
         }
      });
   }
}

#2


8  

I do it that way, and works very well:

我这样做,而且工作得很好:


BufferedImage awtImage = new BufferedImage(drawPanel.getWidth(), drawPanel.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics g = awtImage.getGraphics();
drawPanel.printAll(g);

try
{
String caminhoImagem = System.getProperty("user.home") + "\\temps\\assinatura.jpg";
FileOutputStream fos = new FileOutputStream(caminhoImagem);
JPEGImageEncoderImpl j = new JPEGImageEncoderImpl(fos);
j.encode(awtImage);
fos.close();
} catch(e) {..... }

That's all :) Thanks everyone :)

这就是全部:)谢谢大家:

#3


4  

Use the drawImage method provided by Graphics2D and write it using ImageIO

使用Graphics2D提供的drawImage方法,并使用ImageIO编写它。

BufferedImage img;
g2dObject.drawImage(img, null, 0, 0);
ImageIO.write(img, "JPEG", new File("foo.jpg"));

#4


3  

Use the "drawOnImage" example from Custom Painting Approaches. Then to create the image of the panel you can use the Screen Image class.

使用自定义绘制方法中的“drawOnImage”示例。然后创建面板的图像,您可以使用屏幕图像类。

#1


11  

Have them draw directly in a BufferedImage by way of it's Graphics2D object which you can get via getGraphics(). Then use ImageIO.write(...) to output the image to whatever file type you desire (and that's supported). The ImageIO API should help you with this: ImageIO API.

让他们直接用它的Graphics2D对象,通过getGraphics()得到一个BufferedImage。然后使用ImageIO.write(…)将图像输出到您想要的任何文件类型(并得到支持)。ImageIO API应该帮助您使用这个:ImageIO API。

The other issue you'll have is where are they supposed to save the image once it has been drawn? On their own computer? If so and this is an applet program, then the applet will need to be "signed" in order for it to have the permission to write to disk. If you're unsure on this, check out Google, this article, or you may wish to write a new question for this issue alone.

另一个问题是,一旦图像被绘制出来,他们应该在哪里保存图像呢?在自己的电脑吗?如果是这样的话,这是一个applet程序,那么applet将需要“签名”,以便它获得写入磁盘的权限。如果您对此不确定,请查看谷歌,这篇文章,或者您可能希望单独为这个问题写一个新的问题。

Edit 1: code example
For example:

编辑1:代码示例:

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

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

@SuppressWarnings("serial")
public class DrawAndSaveImage extends JPanel {
   private static final int BI_WIDTH = 600;
   private static final int BI_HEIGHT = BI_WIDTH;
   private static final Color LABEL_DRAW_COLOR = new Color(180, 180, 255);
   private static final Stroke LABEL_DRAW_STROKE = new BasicStroke(1);
   private static final Stroke BIMAGE_DRAW_STROKE = new BasicStroke(4);
   private static final int COLOR_DIV = 5;
   private BufferedImage bImage = new BufferedImage(BI_WIDTH, BI_HEIGHT,
            BufferedImage.TYPE_INT_RGB);
   private List<Point> pointList = new ArrayList<Point>();
   private JLabel imageLabel;
   private List<Color> colorList = new ArrayList<Color>();
   private Random random = new Random();

   public DrawAndSaveImage() {
      Graphics2D g2d = bImage.createGraphics();
      g2d.setBackground(Color.white);
      g2d.clearRect(0, 0, BI_WIDTH, BI_HEIGHT);
      g2d.dispose();

      for (int r = 0; r < COLOR_DIV; r++) {
         for (int g = 0; g < COLOR_DIV; g++) {
            for (int b = 0; b < COLOR_DIV; b++) {
               Color c = new Color((r * 255) / COLOR_DIV,
                        (g * 255) / COLOR_DIV, (b * 255) / COLOR_DIV);
               colorList.add(c);
            }
         }
      }

      imageLabel = new JLabel(new ImageIcon(bImage)) {
         @Override
         protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            paintInLabel(g);
         }
      };
      MyMouseAdapter myMouseAdapter = new MyMouseAdapter();
      imageLabel.addMouseListener(myMouseAdapter);
      imageLabel.addMouseMotionListener(myMouseAdapter);
      imageLabel.setBorder(BorderFactory.createEtchedBorder());

      JButton saveImageBtn = new JButton("Save Image");
      saveImageBtn.addActionListener(new ActionListener() {
         @Override
         public void actionPerformed(ActionEvent e) {
            saveImageActionPerformed();
         }
      });

      JButton clearImageBtn = new JButton("Clear Image");
      clearImageBtn.addActionListener(new ActionListener() {
         @Override
         public void actionPerformed(ActionEvent e) {
            Graphics2D g2 = bImage.createGraphics();
            g2.setBackground(Color.white);
            g2.clearRect(0, 0, BI_WIDTH, BI_HEIGHT);
            g2.dispose();
            imageLabel.repaint();
         }
      });

      JPanel btnPanel = new JPanel();
      btnPanel.add(saveImageBtn);
      btnPanel.add(clearImageBtn);

      setLayout(new BorderLayout());
      add(imageLabel, BorderLayout.CENTER);
      add(btnPanel, BorderLayout.SOUTH);
   }

   private void saveImageActionPerformed() {
      JFileChooser filechooser = new JFileChooser();
      FileNameExtensionFilter filter = new FileNameExtensionFilter(
               "JPG Images", "jpg");
      filechooser.setFileFilter(filter);
      int result = filechooser.showSaveDialog(this);
      if (result == JFileChooser.APPROVE_OPTION) {
         File saveFile = filechooser.getSelectedFile();
         try {
            ImageIO.write(bImage, "jpg", saveFile);
         } catch (IOException e) {
            e.printStackTrace();
         }
      }
   }

   private void paintInLabel(Graphics g) {
      Graphics2D g2d = (Graphics2D) g;
      g2d.setColor(LABEL_DRAW_COLOR);
      g2d.setStroke(LABEL_DRAW_STROKE);
      if (pointList.size() < 2) {
         return;
      }
      for (int i = 1; i < pointList.size(); i++) {
         int x1 = pointList.get(i - 1).x;
         int y1 = pointList.get(i - 1).y;
         int x2 = pointList.get(i).x;
         int y2 = pointList.get(i).y;
         g2d.drawLine(x1, y1, x2, y2);
      }
   }

   private class MyMouseAdapter extends MouseAdapter {

      @Override
      public void mousePressed(MouseEvent e) {
         pointList.add(e.getPoint());
         imageLabel.repaint();
      }

      @Override
      public void mouseReleased(MouseEvent e) {
         Graphics2D g2d = bImage.createGraphics();
         g2d.setColor(colorList.get(random.nextInt(colorList.size())));
         g2d.setStroke(BIMAGE_DRAW_STROKE);
         if (pointList.size() >= 2) {
            for (int i = 1; i < pointList.size(); i++) {
               int x1 = pointList.get(i - 1).x;
               int y1 = pointList.get(i - 1).y;
               int x2 = pointList.get(i).x;
               int y2 = pointList.get(i).y;
               g2d.drawLine(x1, y1, x2, y2);
            }
         }
         g2d.dispose();

         pointList.clear();
         imageLabel.repaint();
      }

      @Override
      public void mouseDragged(MouseEvent e) {
         pointList.add(e.getPoint());
         imageLabel.repaint();
      }
   }

   private static void createAndShowUI() {
      JFrame frame = new JFrame("DrawAndSaveImage");
      frame.getContentPane().add(new DrawAndSaveImage());
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      java.awt.EventQueue.invokeLater(new Runnable() {
         public void run() {
            createAndShowUI();
         }
      });
   }
}

#2


8  

I do it that way, and works very well:

我这样做,而且工作得很好:


BufferedImage awtImage = new BufferedImage(drawPanel.getWidth(), drawPanel.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics g = awtImage.getGraphics();
drawPanel.printAll(g);

try
{
String caminhoImagem = System.getProperty("user.home") + "\\temps\\assinatura.jpg";
FileOutputStream fos = new FileOutputStream(caminhoImagem);
JPEGImageEncoderImpl j = new JPEGImageEncoderImpl(fos);
j.encode(awtImage);
fos.close();
} catch(e) {..... }

That's all :) Thanks everyone :)

这就是全部:)谢谢大家:

#3


4  

Use the drawImage method provided by Graphics2D and write it using ImageIO

使用Graphics2D提供的drawImage方法,并使用ImageIO编写它。

BufferedImage img;
g2dObject.drawImage(img, null, 0, 0);
ImageIO.write(img, "JPEG", new File("foo.jpg"));

#4


3  

Use the "drawOnImage" example from Custom Painting Approaches. Then to create the image of the panel you can use the Screen Image class.

使用自定义绘制方法中的“drawOnImage”示例。然后创建面板的图像,您可以使用屏幕图像类。