客户端首先建立用户界面,使用Webcam-capture获取摄像头,使用线程将每一帧图片显示在用户界面上实现视频效果,同时使用BufferedImage获取图片数据,使用Socket与服务端连接,并使用DataOutputStream像服务端发送图片数据。
服务端首先建立用户界面,使用ServerSocket、Socket与客户端连接,在线程中使用InputStream和DataInputStream不断读取客户端发来的图片数据,并用画笔Graphics画在用户界面上。
代码实现:
1、客户端
package video;
import com.github.sarxos.webcam.Webcam;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.DataOutput;
import java.io.DataOutputStream;
import java.io.OutputStream;
import java.net.Socket;
public class VideoClient {
public static void main(String[] args) {
new VideoClient().videoUI();
}
public void videoUI(){
JFrame jf=new JFrame("视频通信_client");
jf.setSize(900,900);
jf.setLocationRelativeTo(null);
jf.setDefaultCloseOperation(3);
jf.setLayout(new FlowLayout());
JButton start=new JButton("视频");
jf.add(start);
jf.setVisible(true);
Graphics g=jf.getGraphics();
start.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//启动摄像头,获取视频数据
openVideo(g);
}
});
}
//开启摄像头
public void openVideo(Graphics g){
Webcam cam=Webcam.getDefault();
cam.open();
new Thread(new Runnable() {
@Override
public void run() {
Socket socket=null;
DataOutputStream dos=null;
try {
socket=new Socket("127.0.0.1",8899);
OutputStream os=socket.getOutputStream();
dos=new DataOutputStream(os);
} catch (Exception e) {
throw new RuntimeException(e);
}
while (true){
BufferedImage bufferedImage=cam.getImage();
g.drawImage(bufferedImage,30,100,null);
try {
Thread.sleep(10);
startVideo(bufferedImage,dos);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
}).start();
}
//与服务器建立连接,发送视频数据(像素点)
public void startVideo(BufferedImage image,DataOutputStream dos)throws Exception{
//获取图片的像素点
int w=image.getWidth();
int h=image.getHeight();
//发送图片大小
dos.writeInt(w);
dos.writeInt(h);
dos.flush();
for(int i=0;i<h;i++){
for(int j=0;j<w;j++){
int pixel=image.getRGB(j,i);
dos.writeInt(pixel);
dos.flush();
}
}
}
}
2、服务端
package video;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class VideoServer {
public static void main(String[] args) {
new VideoServer().videoUI();
}
private Graphics g;
public void videoUI(){
JFrame jf=new JFrame("视频通信_server");
jf.setSize(900,900);
jf.setLocationRelativeTo(null);
jf.setDefaultCloseOperation(3);
jf.setLayout(new FlowLayout());
jf.setVisible(true);
g=jf.getGraphics();
try {
readImage();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
//读取发来的视频数据
public void readImage() throws Exception{
ServerSocket server=new ServerSocket(8899);
System.out.println("启动服务器...");
Socket socket=server.accept();
InputStream is=socket.getInputStream();
DataInputStream dis=new DataInputStream(is);
new Thread(new Runnable() {
@Override
public void run() {
while (true){
try {
Thread.sleep(10);
int w=dis.readInt();
int h=dis.readInt();
BufferedImage image=new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB);
for(int i=0;i<h;i++){
for(int j=0;j<w;j++){
int pixel=dis.readInt();
image.setRGB(j,i,pixel);
}
}
g.drawImage(image,30,80,null);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
}).start();
}
}