⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jwebcamplayer.java

📁 本软件代码是基于linux下进行语音播放、视频显示的一段代码!是开发嵌入式设备的非常好的例子程序
💻 JAVA
字号:
/* * JWebcamPlayer.java * * Created on March 21, 2005, 1:31 AM *//** * * @author Alvaro Salmador (nx5) (naplam33 at msn.com) */import java.applet.*;import java.applet.AppletContext.*;import java.awt.*;import java.awt.event.*;import java.net.URL;import javax.swing.*;import java.io.*;import java.net.*;public class JWebcamPlayer extends javax.swing.JApplet{    public static final boolean DEBUGGING = false;       public JWebcamPlayer()     {    }    public static String checkAppletLoaded()    {        return "ok";    }        public void init()     {        m_strColor = getParameter("Color");        if (m_strColor==null || m_strColor=="") m_strColor = "#FFFFFF";        m_strServer = getParameter("Server");        if (m_strServer==null || m_strServer=="") m_strServer = "127.0.0.1";        m_strPort = getParameter("Port");        if (m_strPort==null || m_strPort=="") m_strPort = "7070";    }       public void createGUI()    {        setBackground(Color.decode(m_strColor));                try {            //UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); //native            UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName()); //java        } catch(Exception e) {  }         Container content = getContentPane();                content.setBackground(Color.decode(m_strColor));                GridBagLayout gbl = new GridBagLayout();        content.setLayout(gbl);        GridBagConstraints c = new GridBagConstraints();        c.fill = GridBagConstraints.BOTH;        c.weighty = 1.0;        c.weightx = 1.0;        c.gridwidth = 1;        c.anchor = GridBagConstraints.NORTHWEST;        c.insets = new Insets(0, 0,0,0);        c.gridheight = 1;        c.gridx = 0;        c.gridy = 0;        m_label = new JLabel();        m_label.setText("JWebcamPlayer Applet");            content.add(m_label, c);    }    public void start()     {        m_stop = false;                try {            javax.swing.SwingUtilities.invokeAndWait(new Runnable()             {                public void run() {                    createGUI();                }            } );        } catch (Exception e) {         }        m_worker = new SwingWorker() {            public Object construct() {                                Socket connection;                InputStream in;                OutputStream out;                try {                  int port = 0;                  try {                    port = Integer.parseInt(m_strPort);                  }                  catch(Exception e) {                    port = 0;                  }                  if (port==0) port = 7070;                  connection = new Socket(m_strServer, port);                  in = connection.getInputStream();                  out = connection.getOutputStream();                }                catch (IOException e) {                  System.out.println(                      "Attempt to create connection failed with error: " + e);                  m_label.setIcon(null);                  m_label.setText("JWebcamPlayer - Connection failed");                  m_stop = true;                  return null;                }                                byte [] buffer = new byte [512*1024];                                m_label.setText("");                        while(!m_stop)                {                    // bucle, constantemente lee imagen del server y la muestra                    byte [] b = {'O','K',0,0, 0,0,0,0,0,0,0,0,0};                                        int n = 0;                    int siz = 0;                    try {                        out.write(b);                        if (DEBUGGING)                            System.out.print("-sending my header ("+Integer.toString(b.length)+") bytes");                                                n = in.read(buffer, 0, HDRLEN);                        /*while(n<HDRLEN && n>0) {                            int r = in.read(buffer, n, HDRLEN-n);                            if (r<=0) break;                        }*/                                                if (DEBUGGING)                            System.out.print("-read header ("+Integer.toString(n)+") bytes");                    }                    catch(Exception e) {                        if (DEBUGGING)                            e.printStackTrace();                                                m_stop = true;                    }                                        if (n<HDRLEN)                     {                        if (n<=0)                             m_stop = true;                        continue;                    }                     else                     {                        // can someone point out a more straightforward way to convert 4 bytes                        // to a java integer? this is just awful...                        int ssz = SZOFS;                        int b0 = (buffer[ssz]<0) ? (256-buffer[ssz]) : buffer[ssz];                        int b1 = (buffer[ssz+1]<0) ? (256-buffer[ssz+1]) : buffer[ssz+1];                        int b2 = (buffer[ssz+2]<0) ? (256-buffer[ssz+2]) : buffer[ssz+2];                        int b3 = (buffer[ssz+3]<0) ? (256-buffer[ssz+3]) : buffer[ssz+3];                        siz = ((b3<<24)&0xFF000000) | ((b2<<16)&0xFF0000) | ((b1<<8)&0xFF00) | (b0&0xFF);                        if (DEBUGGING)                            System.out.println("** reading jpeg, size="+Integer.toString(siz));                        n = HDRLEN;                        if (buffer[0]!='S' || buffer[1]!='P' || buffer[2]!='C' || buffer[3]!='A')                        {                            if (DEBUGGING)                                System.out.println("*Header missing 'SPCA'");                                                        continue;                        }                        else if (siz<=0 || siz>(512*1024))                        {                            siz = 0;                            try {                                 in.read(buffer, n, buffer.length);                             }                               catch(Exception e) {                                if (DEBUGGING)                                    e.printStackTrace();                            }                                                        if (DEBUGGING)                                System.out.println("*Illegal image size");                                                        continue;                        }                        else                         {                            do                            {                                int r = 0;                                try {                                    //r = in.read(buffer, n, HDLREN + siz - n);                                    r = in.read(buffer, n, buffer.length - n);                                }                                   catch(Exception e)                                {                                    e.printStackTrace();                                }                                n += r;                                if (DEBUGGING)                                    System.out.println(" chunk read, size="+Integer.toString(r)+" total="+Integer.toString(n));                                /*if (r<=0)                                 {                                    n = r;                                    break;                                }*/                            }                            while(n<(siz+HDRLEN));                        }                        if (DEBUGGING)                            System.out.println("jpeg read, size="+Integer.toString(n));                    }                                     byte [] buffer2 = new byte [n];                                        for(int i=0; i<n; i++)                        buffer2[i] = buffer[i+HDRLEN];                    ImageIcon ii = new ImageIcon(Toolkit.getDefaultToolkit().createImage(buffer2));                    if (ii.getIconHeight()<=1 || ii.getIconWidth()<=1)                    {                        if (DEBUGGING)                            System.out.println("--invalid image got");                        continue;                    }                    else                     {                            if (DEBUGGING)                            System.out.println("--VALID image got");                    }                    m_label.setIcon(ii);                }//while                                try {                  connection.close();                      // (Alternatively, you might depend on the server                      //  to close the connection.)                }                catch (IOException e) {                }                m_label.setIcon(null);                m_label.setText("JWebcamPlayer - Connection closed");                                if (DEBUGGING)                {                    if (m_stop)                        System.out.println("** worker ending (m_stop==true)");                    else                            System.out.println("** worker ending (m_stop==false)");                }                                                                  m_stop = true;                                return null;            }        };               m_worker.start();  //required for SwingWorker 3    }        public void stop()    {        if (DEBUGGING)            System.out.println("--stopping worker (applet stop)");                m_stop = true;    }        private final int HDRLEN = 50;    private final int SZOFS = 29;    private JLabel m_label = null;    private String m_strServer = null;    private String m_strPort = null;    private String m_strColor = null;    private boolean m_stop = false;    private SwingWorker m_worker = null;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -