Ask Your Question
0

which class can show an image in java?

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

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
3

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

berak gravatar image

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

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();
    Imgproc.cvtColor(m,m2,Imgproc.COLOR_BGR2RGB);
    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)
{
    g.drawImage(image,0,0,this);    
}
edit flag offensive delete link more

Comments

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 -0600 )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);?

Thanks,

Ioanna gravatar imageIoanna ( 2013-10-13 07:27:05 -0600 )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 -0600 )edit
1

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 -0600 )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 -0600 )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 -0600 )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 -0600 )edit
1

@frageDE , Imgcodecs.imread() for opencv 3

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

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

susannamoore gravatar image

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

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 ImageIO.read(File file) method.

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
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 
  InputStreamReader(System.in));
 String imageName=bf.readLine();
  File input = new File(imageName);
  image = ImageIO.read(input);
  } catch (IOException ie) {
  System.out.println("Error:"+ie.getMessage());
  }
  }

  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.getContentPane().add(panel);
  frame.setSize(500, 500);
  frame.setVisible(true);
  }
}

Tags: image, image in Java

edit flag offensive delete link more
0

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

rocksean30 gravatar image

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

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

Question Tools

2 followers

Stats

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

Seen: 6,668 times

Last updated: Sep 25 '14