Affichage camera ip java

cs_pauline75 Messages postés 3 Date d'inscription lundi 5 mars 2012 Statut Membre Dernière intervention 6 mars 2012 - 6 mars 2012 à 13:30
cs_walid25 Messages postés 3 Date d'inscription dimanche 17 octobre 2010 Statut Membre Dernière intervention 21 mars 2012 - 21 mars 2012 à 14:08
Bonjour, j'aimerais pour un projet récupérer le flux video d'une camera IP et l'afficher (dans un jFrame par exemple). Apres avoir parcouru environ une centaine de pages je ne trouve pas de tutoriel qui explique la démarche qu'il faut suivre. Le seul code qui revient assez souvent est à la fin de mon messsage. Seulement quand je le teste j'ai un nullPointerException au moment de la récupération du flux, il y a vraiment quelque chose qui m'échappe. Si quelqu'un peut m'aider à comprendre(tuto, code qui marche,....) je serai vraiment reconnaissante je commence à m'approcher du deadline :(
 import java.net.*;
import com.sun.image.codec.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?resolution=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;
    HttpURLConnection huc=null;
    Component parent;
 
    /** Creates a new instance of AxisCamera1 */
    public AxisCamera1(Component parent_) {
    parent = parent_;
    }
 
    public static Image getScaledInstanceAWT(BufferedImage source, double factor) {
    int w = (int) (source.getWidth() * factor);
    int h = (int) (source.getHeight() * factor);
    return source.getScaledInstance(w, h, Image.SCALE_SMOOTH);
    }
 
    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.TYPE_BYTE_INDEXED);
    //BufferedImage bimage = getDefaultConfiguration().createCompatibleImage(w, h, Transparency.OPAQUE);
    Graphics2D g = bimage.createGraphics();
    g.drawImage(image, 0, 0, null);
    g.dispose();
    return bimage;
    }
 
    public void connect()
    {
        try
        {
            URL u = new URL(useMJPGStream?mjpgURL:jpgURL);
            huc = (HttpURLConnection) u.openConnection();
            //System.out.println("huc="+huc);
            System.out.println(huc.getInputStream());
            InputStream is = huc.getInputStream();
            //System.out.println("is="+is);
            connected = true;
            BufferedInputStream bis = new BufferedInputStream(is);
            //System.out.println("bis="+bis);
            dis= new DataInputStream(bis);
            if (!initCompleted) initDisplay();
        }
        catch(IOException e)
        { //incase no connection exists wait and try again, instead of printing the error
 
            try
            {
                huc.disconnect();
                Thread.sleep(60);
            }
            catch(InterruptedException 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));
        setPreferredSize(imageSize);
        parent.setSize(imageSize);
        parent.validate();
        initCompleted = true;
    }
 
    public void disconnect(){
    try{
    if(connected){
    dis.close();
    connected = false;
    }
    }catch(Exception e){;}
    }
 
    public void paint(Graphics g) { //used to set the image on the panel
    if (image != null)
    g.drawImage(image, 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(Exception 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{
    JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(dis);
    image = decoder.decodeAsBufferedImage();
 
    String s="a"+counter+".jpg";
    counter++;
    String filename="c:\\find_stillset\"+s;
    System.out.println(filename);
 
    BufferedImage smaller = toBufferedImage(getScaledInstanceAWT(image, 1.0/3.0));//scalling image size
 
    try {
    OutputStream out = new FileOutputStream(filename);
    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
    encoder.encode(smaller);
    out.close();
    } catch (Exception e) {
    System.out.println(e);
    }
 
    }catch(Exception e){e.printStackTrace();disconnect();}
    }
 
    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(DataInputStream dis){
    try{
    boolean end = false;
    String lineEnd = "\n"; //assumes that the end of the line is marked with this
    byte[] lineEndBytes = lineEnd.getBytes();
    byte[] byteBuf = new byte[lineEndBytes.length];
 
    while(!end){
    dis.read(byteBuf,0,lineEndBytes.length);
    String t = new String(byteBuf);
    System.out.print(t); //uncomment if you want to see what the lines actually look like
    if(t.equals(lineEnd)) end=true;
    }
    }catch(Exception e){e.printStackTrace();}
 
 
    }
    public void run() {
        System.out.println("in Run...................");
        connect();
        readStream();
    }
 
    public static void main(String[] args) {
 
        //System.setProperty("http.proxyHost","proxy.tp.edu.sg");
    //    System.setProperty("http.proxyPort","80");
    JFrame jframe = new JFrame();
    jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    AxisCamera1 axPanel = new AxisCamera1(jframe);
    new Thread(axPanel).start();
    jframe.getContentPane().add(axPanel);
    jframe.pack();
    jframe.show();
    }
 
}

4 réponses

cs_Julien39 Messages postés 6414 Date d'inscription mardi 8 mars 2005 Statut Modérateur Dernière intervention 29 juillet 2020 371
6 mars 2012 à 13:39
Bonjour,

En général, ce genre de chose peu être assez complexe à réaliser et dépend vraiment de la marque de ta webcam.

C'est sans doute pour cela que tu ne trouves pas de tutoriel qui fonctionne chez toi.

Je ne peux pas t'en dire plus, je ne suis pas un spécialiste de ce genre de choses.

Bon courage
0
cs_pauline75 Messages postés 3 Date d'inscription lundi 5 mars 2012 Statut Membre Dernière intervention 6 mars 2012
6 mars 2012 à 13:58
ce sont des camera physiques ip que je vais utiliser : d-link , heden et axis. En fait, j'aimerais trouver un code qui ferait marcher au mmoins une des 3 cameras pour comprendre, après je pourrai adapter pour les 2 autres. Merci quand meme
0
cs_pauline75 Messages postés 3 Date d'inscription lundi 5 mars 2012 Statut Membre Dernière intervention 6 mars 2012
6 mars 2012 à 16:54
En essayant de comprendre étapes par étape, je crois que l'exception vient de la fonction connect plus exactement à la ligne


InputStream is = huc.getInputStream();


la ligne d'initialisation

huc = (HttpURLConnection) u.openConnection();



marche bien , j'ai verifié la valeur de huc qui contient un url qui m'affiche bien la vidéo lorsque je le tape dans navigateur web. A quoi peut etre dû ce null pointer exception?
0
cs_walid25 Messages postés 3 Date d'inscription dimanche 17 octobre 2010 Statut Membre Dernière intervention 21 mars 2012
21 mars 2012 à 14:08
Au début du méthode "connect()" tu doit mettre:

URL u = new URL(useMJPGStream?mjpgURL:jpgURL);
//System.out.println(u.openStream());
huc = (HttpURLConnection) u.openConnection();
String userPassword = camLogin+""+password;
String encoding = new sun.misc.BASE64Encoder().encode(userPassword
.getBytes());
huc.setRequestProperty("Authorization", "Basic " + encoding);
System.out.println(huc.getContentType());
0
Rejoignez-nous