IplImage in JPanel, JLabel

2,270 views
Skip to first unread message

Menegussi

unread,
Nov 28, 2011, 12:09:39 PM11/28/11
to javacv
Hi all,

I need to develop an application using a MVC Design Pattern.

My doubt is how to insert the IplImage inside of JLabel or JPanel,
because i always, until now, work with CanvasFrame do display de
Images.

I saw some examples but i did not understood because i dont have
experience with GUI in JAVA.

Can you help me, urgently.

Menegussi

Samuel Audet

unread,
Nov 28, 2011, 8:51:51 PM11/28/11
to jav...@googlegroups.com
Just call IplImage.getBufferedImage(), it will return a java.awt.Image
you can use with JLabel or whatever you use those Image objects for

Samuel

André Menegussi

unread,
Nov 28, 2011, 9:16:12 PM11/28/11
to jav...@googlegroups.com
Hi Samuel,
 
My MVC design pattern is almost ready. Only the view is not ready!
 
Sorry my lack of knowledge but i never worked with java GUI.
So, can you give me a snippet code from how to do that?
 
Bellow is my View code:
 
import com.googlecode.javacv.cpp.opencv_core.IplImage;
.
.
. some imports
 
public class View implements ActionListener, Observer {
 
 private Subject model;
 private Controller buttonsManager;
 
 
 JFrame tela; //tela principal
 
 JPanel panelBotoes1;
 JPanel panelDados1;
 JButton ligar;
 JButton desligar;
 
 JLabel labelTemp;
  
 
 public View(Subject mo, Controller co) {
  this.model = mo;
  this.buttonsManager = co;
 }
 
 //criação da view
 public void createView(){
  
  tela = new JFrame("Motion Detection");
  tela.setSize(800, 600);
  tela.setVisible(true);
  tela.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
  
  panelBotoes1 = new JPanel();
  
  panelDados1 = new JPanel();
  
  ligar = new JButton("Ligar");
  desligar = new JButton("Desligar");
  labelTemp = new JLabel();
  
  ligar.addActionListener(this);
  desligar.addActionListener(this);
  
  panelBotoes1.add(ligar);
  panelBotoes1.add(desligar);
  panelBotoes1.setBackground(Color.green);
  
  panelDados1.add(labelTemp); 
  tela.add(panelBotoes1);
  tela.add(panelDados1);
 
  
  tela.getContentPane().add(BorderLayout.SOUTH, panelBotoes1);
  
  tela.getContentPane().add(BorderLayout.NORTH, panelDados1);
  
  tela.setVisible(true);
  
 }
 //--------------------------------------------
 
 @Override
 public void update(IplImage frame) { // here is my update from the IplImage.....
 
 // TODO Auto-generated method stub
 System.out.println("fiz o update (VIEW)  -------------------------------------------------------------------------"); 
  
--- I dont know what to do  here.......
 
frame.getBufferedImage();
 
labelTemp.add(frame.getBufferedImage());
 
------------------------------------------------
 } 
  
 //--------- Controle de Mouse
 @Override

 public void actionPerformed(ActionEvent event) {
  // TODO Auto-generated method stub
  
  if (event.getSource() == ligar){
   this.buttonsManager.controlaBotoes(true);
   System.out.println("pressionei o iniciar");
 }
  if (event.getSource() == desligar){
   this.buttonsManager.controlaBotoes(false);
   System.out.println("pressionei o parar");
 }
 }
 
}

 Help me, please :)

 

André Menegussi

 
 

2011/11/28 Samuel Audet <samuel...@gmail.com>

Samuel Audet

unread,
Nov 28, 2011, 9:29:37 PM11/28/11
to jav...@googlegroups.com
On 2011-11-29 11:16, Andr� Menegussi wrote:
> Hi Samuel,
> My MVC design pattern is almost ready. Only the view is not ready!
> Sorry my lack of knowledge but i never worked with java GUI.
> So, can you give me a snippet code from how to do that?
> Bellow is my View code:
> import com.googlecode.javacv.cpp.opencv_core.IplImage;

Well if you want to display the image in a JLabel, for example, we can call:
new JLabel(new ImageIcon(IplImage.getBufferedImage())

Samuel

André Menegussi

unread,
Nov 28, 2011, 9:43:15 PM11/28/11
to jav...@googlegroups.com
Ok, but if i update continously this jLabel can I make the new ImageIcon every time without "eat" memory?
 
Thanks!!

2011/11/29 Samuel Audet <samuel...@gmail.com>

André Menegussi

unread,
Nov 28, 2011, 10:02:35 PM11/28/11
to jav...@googlegroups.com
Hi Samuel, another thing:
 
I wanna use the same  JLabel (labelTemp), but i dont know how to pass every time the new image to the labelTemp.
 
Can you help again?
 
Sorry about my poor knowloge!
 
André Menegussi

2011/11/29 André Menegussi <amene...@gmail.com>

André Menegussi

unread,
Nov 29, 2011, 7:55:26 AM11/29/11
to jav...@googlegroups.com
Samuel,
 
Now i can put de IplImage inside a JLabel, but tuo put a sequence of image (video stream from webcam) dont works, i'm making something wrong?
 


 
2011/11/29 André Menegussi <amene...@gmail.com>

André Menegussi

unread,
Nov 29, 2011, 1:28:47 PM11/29/11
to jav...@googlegroups.com
Hi Samuel,
 
I'm again!!
 
How could i update the JLabel with news frames grabbed from the webcam? I tried to use the commands:
 
ImageIcon quadro = new ImageIcon( frame.getBufferedImage());
 
labelTemp.setIcon(quadro);   //new ImageIcon( frame.getBufferedImage()));

 labelTemp.repaint();


But dont works.
 
I dont know how to do that! :)
 
Somebody has a idea?
 
Thanks

 
2011/11/29 André Menegussi <amene...@gmail.com>

Samuel Audet

unread,
Dec 1, 2011, 8:33:35 AM12/1/11
to jav...@googlegroups.com
Just following the tutorial here:
http://docs.oracle.com/javase/tutorial/2d/images/drawimage.html

Samuel

On 2011-11-30 03:28, Andr� Menegussi wrote:
> Hi Samuel,
> I'm again!!
> How could i update the JLabel with news frames grabbed from the webcam?
> I tried to use the commands:

> *ImageIcon quadro = new*ImageIcon( frame.getBufferedImage());

Samuel Audet

unread,
Sep 29, 2012, 8:02:27 PM9/29/12
to jav...@googlegroups.com
On 2012-09-24 06:53, Raamon Paiva wrote:
> Did you found the solution to your problem? I have the issue...

Try something similar to what they explain here:
http://docs.oracle.com/javase/tutorial/2d/images/drawimage.html

Samuel

Srijit Chandrashekhar Nair

unread,
Oct 25, 2012, 1:01:29 PM10/25/12
to jav...@googlegroups.com
The below code works for me

public class CameraPanel extends JPanel {

private static final long serialVersionUID = 1L;
public JFrame parentWindow;
BufferedImage img = null;
public CameraPanel(JFrame parentWindow){
this.parentWindow = parentWindow;
}
public void displayImage(IplImage img){
this.img=img.getBufferedImage(); 
repaint();
}
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g.create();
super.paintComponent(g2);
g2.drawImage(img, 0,0,parentWindow.getWidth(),parentWindow.getHeight(), null);
}
}


Try this..

Thanks
Srijit

Srijit Chandrashekhar Nair

unread,
Apr 6, 2013, 6:38:30 PM4/6/13
to jav...@googlegroups.com
It would be helpful if you can tell a bit more about what you are trying to do and the error you are getting


On Sat, Apr 6, 2013 at 3:22 PM, <tomas...@electrodevsweden.com> wrote:

Dear Srijit or anyone else..,

I did the Java-course at school about 10years ago and now I am ofcourse really tumbeling when I try to re-learn...

I tried your code below but without success, I would be really glad if someone could please help me out here, probably it is extremely simple..

My simple code looks like the following:

 import java.awt.BorderLayout;
import java.awt.Container;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import com.googlecode.javacv.OpenCVFrameGrabber;

import com.googlecode.javacv.cpp.opencv_core.IplImage;

public class javaVision extends JFrame implements ActionListener{
  

private static final long serialVersionUID = 1L;//bara frö att få bort ett felmeddelande från eclipse..
 
 // 0-default camera, 1 - next...so on
 OpenCVFrameGrabber grabber = new OpenCVFrameGrabber(1);
 IplImage IplImg = new IplImage();
 
    JPanel prePanel = new JPanel();
    JPanel postPanel = new JPanel();
   
    JButton captureButton = new JButton("Capture");
    
    public javaVision() {
        initUI();
    }

 

 
    public final void initUI() {
      
     Container contentPane = this.getContentPane();
     contentPane.setLayout(new BorderLayout());
    
     captureButton.setBounds(100, 60, 100, 30);
     
        //Add action listener to button
        captureButton.addActionListener(this);

        prePanel.setSize(640,480);
     
     this.add(prePanel, BorderLayout.SOUTH);
     this.add(captureButton, BorderLayout.NORTH);

     this.setTitle("Tooltip");
     this.setSize(1024, 728);
    
     setLocationRelativeTo(null);
     setDefaultCloseOperation(EXIT_ON_CLOSE);
    
     prePanel.setVisible(true);
     this.setVisible(true);
    }
   
    public void actionPerformed(ActionEvent e) {
       
     String cmd = e.getActionCommand();
       
     if (cmd.equals("Capture")){
           this.capture();
     }
    }

   
    public void capture(){
  try {
            grabber.start();
            IplImg = grabber.grab();
  }
     catch (Exception e) {
      e.printStackTrace();
     }
  
  BufferedImage BufImg = IplImg.getBufferedImage();
  int w = BufImg.getWidth();
  int h = BufImg.getHeight();
  System.out.println("width: "+w);
  System.out.println("height: "+h);
  
//BOTH THE IPLIMG AND THE BUFIMG CONTAINS CORRECT INFO , tried to save to file with ´both of them. The fault is probably on the following 4 lines..
  Graphics g = BufImg.getGraphics();


  Graphics2D g2 = (Graphics2D) g.create();
  

  prePanel.paintComponents(g2); 
  g2.drawImage(BufImg, 0, 0, w, h, null);
  //g2.drawLine(10, 10, 50, 50);//THIS DOESNT WORK EITHER
  
  prePanel.repaint();
  this.repaint();
  
  try {
   grabber.stop();
  } catch (com.googlecode.javacv.FrameGrabber.Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  
    }
  
  
     public static void main(String[] args) {
         SwingUtilities.invokeLater(new Runnable() {
             public void run() {
              javaVision ex = new javaVision();
                 ex.setVisible(true);
             
             }
         });
     }

}

 

--
 
---
You received this message because you are subscribed to a topic in the Google Groups "javacv" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/javacv/AJIR6CfHHYg/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to javacv+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--

Thanks
Srijit
860 712 1800
sriji...@gmail.com

Tomas Bergh

unread,
Apr 7, 2013, 3:51:44 AM4/7/13
to jav...@googlegroups.com
Dear Srijit,
What I am trying to achieve is the following:
1: create class extending JFrame
2. set borderlayout to the jframe
3. create and add jbutton to jframe north
4. create and add jpanel to jframe south,JPanel is named prePanel
5. add actionlistener to button
6. when clicked the button I call the function capture(). In capture I want to read a picture from the webcam and put it on the prePanel JFrame so that I can see in on the scree.
 
What happens when executing the code:
1. I enter the capture, captures a picture
2. I print out the size of the picture = ok
3. I tried to save the IplImage and the BufferedImage to disk and it was successful, so there is actually a picture captured.
4. BUT there is no picture shown on the screen!
 
Hope I did not miss anything.
 
Very thankful for your help!
 
Regards,
 
Tomas

2013/4/7 Srijit Chandrashekhar Nair <sriji...@gmail.com>

--
 
---
You received this message because you are subscribed to the Google Groups "javacv" group.
To unsubscribe from this group and stop receiving emails from it, send an email to javacv+un...@googlegroups.com.

Samuel Audet

unread,
Apr 14, 2013, 6:57:28 AM4/14/13
to jav...@googlegroups.com
On 04/07/2013 04:51 PM, Tomas Bergh wrote:
> Dear Srijit,
> What I am trying to achieve is the following:
> 1: create class extending JFrame
> 2. set borderlayout to the jframe
> 3. create and add jbutton to jframe north
> 4. create and add jpanel to jframe south,JPanel is named prePanel
> 5. add actionlistener to button
> 6. when clicked the button I call the function capture(). In capture I
> want to read a picture from the webcam and put it on the prePanel JFrame
> so that I can see in on the scree.
> What happens when executing the code:
> 1. I enter the capture, captures a picture
> 2. I print out the size of the picture = ok
> 3. I tried to save the IplImage and the BufferedImage to disk and it was
> successful, so there is actually a picture captured.
> 4. BUT there is no picture shown on the screen!

We have to actually draw the image for it to show. Follow the steps from
this tutorial:
http://docs.oracle.com/javase/tutorial/2d/images/drawimage.html

Samuel

Tomas Bergh

unread,
Apr 15, 2013, 2:26:15 AM4/15/13
to jav...@googlegroups.com
Hi Samuel,
 
I have now found the reason why I had  troubles... I believe it was related to the override of the paintComponent method.
 
Thank you for your help.
 
Regards,
Tomas
Reply all
Reply to author
Forward
0 new messages