When generating an image, I just want to visualize it and not save it

Asked

Viewed 131 times

-2

When using this code I save the image, but instead of saving I just want to visualize it.

String key = "Lucão MC";
    BufferedImage bufferedImage = ImageIO.read(new File("recibo.png"));

    Graphics graphics = bufferedImage.getGraphics();
    graphics.setColor(Color.BLACK);
    graphics.setFont(new Font("Arial Regular", Font.PLAIN, 55));
    graphics.drawString(key, 300, 300);
    ImageIO.write(bufferedImage, "png", new File("recibo1.png"));
    System.out.println("Image Created");
  • Where do you want to view?

  • @Thiagoluizdomacoski By default in windows.

  • You want to open a window with the image (in Swing), that’s it, right?

  • Yes, preferably with the default windows viewer.

  • 1

    To use the default Windows viewer, use the answer of colleague Dilnei (just think that you will have to save the file before). If you want to use a generic form (which works in any OS), use my answer. P.S.: Next time you ask, follow these two tips: 1) be specific, because this way you avoid that people waste time and speed up your help; 2) prepare a [mcve], because not everyone has good will to take your code and try to arrange to compile.

  • 1

    All right, thanks for the tips.

Show 1 more comment

2 answers

3


Use the class Desktop of the awt, ex:

Desktop.getDesktop().open(new File("C:\\images\\suaImage.jpg"));
  • Example, in case I just want to visualize, without saving.

  • Sorry Dilnei. AP was confusing. Apparently he wanted to use the external Windows viewer, and so his answer is valid (even won my +1). :)

  • 1

    without problems, yours is as valid as mine, after all he wants to do in memory :)

3

Create a JFrame, inside it add a JLabel and set the label content as an image using a ImageIcon encapsulating your BufferedImage. Example:

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Teste {

    public static void main(String[] args) throws IOException {

        String key = "Lucão MC";
        BufferedImage bufferedImage = ImageIO.read(new File("recibo.png"));

        Graphics graphics = bufferedImage.getGraphics();
        graphics.setColor(Color.BLACK);
        graphics.setFont(new Font("Arial Regular", Font.PLAIN, 55));
        graphics.drawString(key, 300, 300);

        //ImageIO.write(bufferedImage, "png", new File("recibo1.png"));
        Teste.showImage(bufferedImage);

        System.out.println("Image Created");


    }

    // Esse método é o que exibe a imagem em uma janela
    protected static void showImage(BufferedImage img) {
        JFrame frame = new JFrame();
        frame.getContentPane().setLayout(new FlowLayout());
        frame.getContentPane().add(new JLabel(new ImageIcon(img)));
        frame.pack();
        frame.setVisible(true);
    }

}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.