Video capture using JMF

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Kusum Goel
    New Member
    • Sep 2007
    • 1

    Video capture using JMF

    Hello guys,
    I am working on a IP camera for many days.But i am unable to capture the video with the JMF coding. Please provide me correct JMF coding.Please help in out.


    Thanks in advance.
  • amitrana
    New Member
    • Oct 2007
    • 1

    #2
    Originally posted by Kusum Goel
    Hello guys,
    I am working on a IP camera for many days.But i am unable to capture the video with the JMF coding. Please provide me correct JMF coding.Please help in out.


    Thanks in advance.
    hi,

    i m also doing same things. and i have one code that capture MJPEG video format and they store all jpeg file , but i want in MPEG-4 video format so if u have any idea then give me reply....

    here is the code for MJPG video capture on ip camera


    [CODE=java] import java.net.*;
    import com.sun.image.c odec.jpeg.*;
    import java.io.*;
    import java.awt.*;
    import java.awt.event. *;
    import java.awt.image. *;
    import javax.swing.*;


    public class AxisCamera1 extends JPanel implements Runnable
    {
    public boolean useMJPGStream = true; //still image set to false
    //streaming set to true
    public String jpgURL="http://192.168.0.36/axis-cgi/jpg/image.cgi?resol ution=352x288";
    public String mjpgURL="http://192.168.0.36/mjpg/video.mjpg";//"http://192.168.1.198/mjpg/video.mjpg";
    DataInputStream dis;
    private BufferedImage image=null;
    int counter=0;

    public Dimension imageSize = null;
    public boolean connected = false;
    private boolean initCompleted = false;
    HttpURLConnecti on huc=null;
    Component parent;

    /** Creates a new instance of AxisCamera1 */
    public AxisCamera1(Com ponent parent_) {
    parent = parent_;
    }

    public static Image getScaledInstan ceAWT(BufferedI mage source, double factor) {
    int w = (int) (source.getWidt h() * factor);
    int h = (int) (source.getHeig ht() * factor);
    return source.getScale dInstance(w, h, Image.SCALE_SMO OTH);
    }

    public static BufferedImage toBufferedImage (Image image) {
    new ImageIcon(image ); //load image
    int w = image.getWidth( null);
    int h = image.getHeight (null);
    BufferedImage bimage = new BufferedImage(w , h, BufferedImage.T YPE_BYTE_INDEXE D);
    //BufferedImage bimage = getDefaultConfi guration().crea teCompatibleIma ge(w, h, Transparency.OP AQUE);
    Graphics2D g = bimage.createGr aphics();
    g.drawImage(ima ge, 0, 0, null);
    g.dispose();
    return bimage;
    }

    public void connect()
    {
    try
    {
    URL u = new URL(useMJPGStre am?mjpgURL:jpgU RL);
    huc = (HttpURLConnect ion) u.openConnectio n();
    //System.out.prin tln("huc="+huc) ;
    System.out.prin tln(huc.getInpu tStream());
    InputStream is = huc.getInputStr eam();
    //System.out.prin tln("is="+is);
    connected = true;
    BufferedInputSt ream bis = new BufferedInputSt ream(is);
    //System.out.prin tln("bis="+bis) ;
    dis= new DataInputStream (bis);
    if (!initCompleted ) initDisplay();
    }
    catch(IOExcepti on e)
    { //incase no connection exists wait and try again, instead of printing the error

    try
    {
    huc.disconnect( );
    Thread.sleep(60 );
    }
    catch(Interrupt edException ie)
    {
    //huc.disconnect( );connect();
    }
    //connect();
    }
    catch(Exception e){;}
    }

    public void initDisplay()
    { //setup the display
    if (useMJPGStream) readMJPGStream( );
    else
    {
    readJPG();
    disconnect();
    }
    imageSize = new Dimension(image .getWidth(this) , image.getHeight (this));
    setPreferredSiz e(imageSize);
    parent.setSize( imageSize);
    parent.validate ();
    initCompleted = true;
    }

    public void disconnect(){
    try{
    if(connected){
    dis.close();
    connected = false;
    }
    }catch(Exceptio n e){;}
    }

    public void paint(Graphics g) { //used to set the image on the panel
    if (image != null)
    g.drawImage(ima ge, 0, 0, this);
    }

    public void readStream(){ //the basic method to continuously read the stream
    try{
    if (useMJPGStream) {
    while(true){
    readMJPGStream( );
    parent.repaint( );
    }
    }
    else{
    while(true){
    connect();
    readJPG();
    parent.repaint( );
    disconnect();

    }
    }

    }catch(Exceptio n e){;}
    }


    public void readMJPGStream( ){ //preprocess the mjpg stream to remove the mjpg encapsulation
    readLine(4,dis) ; //discard the first 3 lines
    readJPG();
    readLine(1,dis) ; //discard the last two lines
    }

    public void readJPG(){ //read the embedded jpeg image
    try{
    JPEGImageDecode r decoder = JPEGCodec.creat eJPEGDecoder(di s);
    image = decoder.decodeA sBufferedImage( );

    String s="a"+counter+" .jpg";
    counter++;
    String filename="c:\\f ind_stillset\\" +s;
    System.out.prin tln(filename);

    BufferedImage smaller = toBufferedImage (getScaledInsta nceAWT(image, 1.0/3.0));//scalling image size

    try {
    OutputStream out = new FileOutputStrea m(filename);
    JPEGImageEncode r encoder = JPEGCodec.creat eJPEGEncoder(ou t);
    encoder.encode( smaller);
    out.close();
    } catch (Exception e) {
    System.out.prin tln(e);
    }

    }catch(Exceptio n e){e.printStack Trace();disconn ect();}
    }

    public void readLine(int n, DataInputStream dis){ //used to strip out the header lines
    for (int i=0; i<n;i++){
    readLine(dis);
    }
    }

    public void readLine(DataIn putStream dis){
    try{
    boolean end = false;
    String lineEnd = "\n"; //assumes that the end of the line is marked with this
    byte[] lineEndBytes = lineEnd.getByte s();
    byte[] byteBuf = new byte[lineEndBytes.le ngth];

    while(!end){
    dis.read(byteBu f,0,lineEndByte s.length);
    String t = new String(byteBuf) ;
    System.out.prin t(t); //uncomment if you want to see what the lines actually look like
    if(t.equals(lin eEnd)) end=true;
    }
    }catch(Exceptio n e){e.printStack Trace();}


    }
    public void run() {
    System.out.prin tln("in Run............ .......");
    connect();
    readStream();
    }

    public static void main(String[] args) {

    //System.setPrope rty("http.proxy Host","proxy.tp .edu.sg");
    // System.setPrope rty("http.proxy Port","80");
    JFrame jframe = new JFrame();
    jframe.setDefau ltCloseOperatio n(JFrame.EXIT_O N_CLOSE);
    AxisCamera1 axPanel = new AxisCamera1(jfr ame);
    new Thread(axPanel) .start();
    jframe.getConte ntPane().add(ax Panel);
    jframe.pack();
    jframe.show();
    }

    }[/CODE]
    Last edited by r035198x; Oct 12 '07, 08:19 AM. Reason: added code tags

    Comment

    • Swathi Nagaraj

      #3
      To display the image into panel i am using this code.
      Graphics g = e.Graphics;
      g.DrawImage(Buf ferImage, 0, 0);
      g.Dispose();

      please help me why g.DrawImage is used and about all those parameters.

      Comment

      Working...