Ask Your Question

which class can show an image in java?

asked 2013-08-06 10:01:39 -0500

outlaw gravatar image

which class can show an image in java? Now I must to use the OpenCV with java, but have not document about it. In C++ ,we can use imshow(). Which class can show an image in java?

edit retag flag offensive close merge delete

3 answers

Sort by ยป oldest newest most voted

answered 2013-08-06 11:03:05 -0500

berak gravatar image

updated 2013-08-06 11:11:17 -0500

assuming, you're talking about desktop-java here (android is a total different story),

convert your Mat to a BufferedImage, then draw it the usual way:

Mat m = Highgui.imread("some.png");
int type = BufferedImage.TYPE_BYTE_GRAY;
if ( m.channels() > 1 ) {
    Mat m2 = new Mat();
    type = BufferedImage.TYPE_3BYTE_BGR;
    m = m2;
byte [] b = new byte[m.channels()*m.cols()*m.rows()];
m.get(0,0,b); // get all the pixels
BufferedImage image = new BufferedImage(m.cols(),m.rows(), type);
image.getRaster().setDataElements(0, 0, m.cols(),m.rows(), b);  

// later:
public synchronized void update(Graphics g)
edit flag offensive delete link more


Think you very much! I am eager for publicing a document about OpenCV in desktop-java.

outlaw gravatar imageoutlaw ( 2013-08-06 22:19:12 -0500 )edit

Hello this is so helpful and definitely we need a manual for OpenCv in desktop java. I have a question what is the this parameter of g.drawImage(image,0,0,this);?


Ioanna gravatar imageIoanna ( 2013-10-13 07:27:05 -0500 )edit

'this' is an Applet(update is a method of that). and you'll need a Frame, too (for making a desktop standalone app)

berak gravatar imageberak ( 2013-10-13 07:54:44 -0500 )edit

Using System.arraycopy will give you 2 times speedup.

public Image toBufferedImageArrayCopy(Mat m){

    int type = BufferedImage.TYPE_BYTE_GRAY;

    if ( m.channels() > 1 ) {

        type = BufferedImage.TYPE_3BYTE_BGR;


    int bufferSize = m.channels()*m.cols()*m.rows();

    byte [] b = new byte[bufferSize];

    m.get(0,0,b); // get all the pixels

    BufferedImage image = new BufferedImage(m.cols(),m.rows(), type);

    final byte[] targetPixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();

    System.arraycopy(b, 0, targetPixels, 0, b.length);  

    return image;

Daniel Baggio gravatar imageDaniel Baggio ( 2014-01-14 22:40:33 -0500 )edit

Hi berak, you mentioned that android is a total different story, could you care to share examples on performing the same operation on android? I am currently trying to do a simple thresholding on an image on android but like you said, this approach doesn't work.

jiarongkoh gravatar imagejiarongkoh ( 2015-01-06 23:40:20 -0500 )edit

@jiarongkoh , for android, look at matToBitmap / bitMapToMat , and then draw it with an ImageView or such.

berak gravatar imageberak ( 2015-01-06 23:47:08 -0500 )edit

I think highgui is not working anymore. So you may want to reconsider your suggestion.

frageDE gravatar imagefrageDE ( 2016-04-07 11:06:32 -0500 )edit

@frageDE , Imgcodecs.imread() for opencv 3

berak gravatar imageberak ( 2016-04-07 13:02:07 -0500 )edit

answered 2014-03-10 22:02:13 -0500

susannamoore gravatar image

updated 2014-03-10 22:03:09 -0500

As for showing image in Java, I'd share some info here. This following example takes an image from the system and show it on a frame using ImageIO class. User enters the name of the image using the command prompt and then the program shows the same image on the frame. The image is read from the system by using file) method.

import java.awt.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
public class ShowImage extends Panel {
  BufferedImage  image;
  public ShowImage() {
  try {
  System.out.println("Enter image name\n");
  BufferedReader bf=new BufferedReader(new 
 String imageName=bf.readLine();
  File input = new File(imageName);
  image =;
  } catch (IOException ie) {

  public void paint(Graphics g) {
  g.drawImage( image, 0, 0, null);

  static public void main(String args[]) throws
Exception {
  JFrame frame = new JFrame("Display image");
  Panel panel = new ShowImage();
  frame.setSize(500, 500);

Tags: image, image in Java

edit flag offensive delete link more

answered 2014-09-25 13:11:48 -0500

rocksean30 gravatar image

updated 2014-09-25 13:14:20 -0500

OpenCV java wrapper doesn't include Iamshow class But I found someone who posted this Iamshow class, I didn't try it yet you may try it yourself and update me too...

Also try this link I found it useful.

edit flag offensive delete link more
Login/Signup to Answer

Question Tools



Asked: 2013-08-06 10:01:39 -0500

Seen: 5,446 times

Last updated: Sep 25 '14