nesrine18 étudiante | bonjour,
c'est mon code qui sert à récupérer le flux video d'une camera Axis et l'afficher dans un jFrame....
pouvez vous m'aider maintenant pour faire l'enregistrement??? merci
Code :
- import java.awt.Component;
- import java.awt.Dimension;
- import java.awt.Graphics;
- import java.awt.Image;
- import java.io.BufferedInputStream;
- import java.io.ByteArrayInputStream;
- import java.io.DataInputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.net.HttpURLConnection;
- import java.net.URL;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import com.sun.image.codec.jpeg.JPEGCodec;
- import com.sun.image.codec.jpeg.JPEGImageDecoder;
- public class NewClass extends JPanel implements Runnable {
- public boolean useMJPGStream = true;
- public String mjpgURL = http192.168.1.218axis-cgimjpgvideo.cgi;
- private String camLogin = root;
- private String camPwd = waycon;
- DataInputStream dis;
- private Image image = null;
- public Dimension imageSize = null;
- public boolean connected = false;
- private boolean initCompleted = false;
- HttpURLConnection huc = null;
- Component parent;
- Creates a new instance of AxisCamera
- public NewClass(Component parent_) {
- parent = parent_;
- }
- public void connect() {
- System.out.println(NewClass.connect());
- try {
- URL u = new URL(mjpgURL);
- huc = (HttpURLConnection) u.openConnection();
- String userPassword = camLogin + + camPwd;
- String encoding = new sun.misc.BASE64Encoder().encode(userPassword
- .getBytes());
- huc.setRequestProperty(Authorization, Basic + encoding);
- InputStream is = huc.getInputStream();
- connected = true;
- BufferedInputStream bis = new BufferedInputStream(is);
- dis = new DataInputStream(bis);
- if (!initCompleted)
- initDisplay();
- } catch (IOException e) {
- e.printStackTrace();
- try {
- huc.disconnect();
- Thread.sleep(60);
- } catch (InterruptedException ie) {
- ie.printStackTrace();
- huc.disconnect();
- connect();
- }
- connect();
- } catch (Exception e) {
- e.printStackTrace();
- ;
- }
- }
- public void initDisplay() { setup the display
- readMJPGStream();
- 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
- }
- public void readMJPGStream() {
- readLine(0, dis);
- }
- public void readJPG() {
- System.out.println(NewClass.readJPG());
- try {
- JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(dis);
- image = decoder.decodeAsBufferedImage();
- } catch (Exception e) {
- e.printStackTrace();
- disconnect();
- }
- }
- public void readLine(int n, DataInputStream dis) {
- readLine(dis);
- }
- public void readLine(DataInputStream dis) {
- int index = 1;
- try {
- do {
- int length = 0;
- int r;
- for (int i = 0; i ((index == 1) 4 5); i++) {
- String line = dis.readLine();
- System.out.println( index + $$$$ + line);
- String string = Content-Length;
- if (line.contains(string)) {
- String trim = line.substring(string.length()).trim();
- System.out.println(trim);
- length = Integer.parseInt(trim);
- }
- }
- byte[] aux = new byte[1000000];
- int total = 0;
- while (total length) {
- r = dis.read(aux, total, length - total);
- total = total + r;
- }
- System.out.println(r = + total);
- JPEGImageDecoder decoder = JPEGCodec
- .createJPEGDecoder(new ByteArrayInputStream(aux, 0,
- length));
- image = decoder.decodeAsBufferedImage();
- imageSize = new Dimension(image.getWidth(this), image
- .getHeight(this));
- setPreferredSize(imageSize);
- parent.setSize(imageSize);
- parent.repaint();
- index ++;
- }
- while (true);
- } catch (Exception e) {
- System.out.println(error at + index);
- e.printStackTrace();
- }
- }
- public void run() {
- connect();
- readStream();
- }
- public static void main(String[] args) {
- JFrame jframe = new JFrame();
- jframe.setBounds(100, 100, 500, 500);
- jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- NewClass axPanel = new NewClass(jframe);
- new Thread(axPanel).start();
- jframe.getContentPane().add(axPanel);
- jframe.pack();
- jframe.setVisible(true);
- }
- }
|
|