视频通话是现在的主流,但是你知道是如何实现的吗?如果想要实现这些技术,肯定是需要了解TCP协议,因为在数据传输时可能会存在粘包的问题,如果没有了解,那么是做不出来的。这里以socket示例,在netty中也会一通百通。
项目准备,导入读取相机流的jar包
<dependency>
<groupId>com.github.sarxos</groupId>
<artifactId>webcam-capture</artifactId>
<version>0.3.12</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.36</version>
</dependency>
客户端
import com.github.sarxos.webcam.Webcam;
import java.awt.*;
import java.awt.image.BufferedImage;
public class Client extends javax.swing.JFrame{
//获取客户端画布
Graphics g;
public void setUI() throws Exception {
this.setTitle("视频");
this.setSize(new Dimension(600,600));
this.setVisible(true);
this.setDefaultCloseOperation(3);
g = this.getGraphics();
//打开网络连接模块
ClientConn clientconn = new ClientConn(9999,"127.0.0.1");
clientconn.conn();
setWebcam(clientconn);
}
//获取摄像头权限并获得图片
public void setWebcam(ClientConn clientconn) throws Exception {
// get default webcam and open it获取网络摄像头设置并打开
Webcam webcam = Webcam.getDefault();
webcam.open();
while(true) {
// get image获取图片
BufferedImage image = webcam.getImage();
drawImage(g,image);
clientconn.trans(image);
}
}
public void drawImage(Graphics g,BufferedImage image) {
g.drawImage(image,10,10,null);
}
public static void main(String[] args) throws Exception {
Client c = new Client();
c.setUI();
}
}
客户端传输类
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
public class ClientConn {
private DataOutputStream out;
private int port;
private String host;
public ClientConn(int port,String host){
this.port = port;
this.host = host;
}
public void conn() {
try {
//创建客户端对象并连接服务器
Socket client = new Socket(host,port);
//初始化输入输出流
out = new DataOutputStream(client.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
System.out.println("程序出错了");
}
}
public void trans(BufferedImage image) throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(image,"jpeg",baos);
//这里进行拆包发送,防止对方读取错误
byte[] array = baos.toByteArray();
out.writeInt(array.length);//告诉服务器这个给包的大小
out.write(array);
out.flush();
}
}
服务器端(也就相当于客户端,点对点通信)文章来源:https://www.toymoban.com/news/detail-516831.html
import javax.swing.*;
import java.awt.*;
import java.io.IOException;
public class Server extends JFrame{
private Graphics g;
public void setUI(int port) throws IOException {
this.setTitle("服务器");
this.setSize(new Dimension(600,600));
this.setVisible(true);
this.setDefaultCloseOperation(3);
this.g = this.getGraphics();
//服务器通信板块,端口9999
ServerConn serverConn = new ServerConn(g);
serverConn.setServer(port);
serverConn.trans();
}
public static void main(String[] args) throws IOException {
Server server = new Server();
server.setUI(9999);
}
}
视频图片(视频是由图片合成的)文章来源地址https://www.toymoban.com/news/detail-516831.html
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
public class ServerConn {
private DataInputStream in;
java.io.OutputStream out;
Graphics g;
public ServerConn(Graphics g) {
this.g = g;
}
//建立服务器函数,port为端口号
public void setServer(int port) {
try {
//创建服务器,建立服务器对象
java.net.ServerSocket ss = new java.net.ServerSocket(port);
System.out.println("服务器创建成功" + port);
//等待客户端对象连接
java.net.Socket client = ss.accept();
System.out.println("有个客户机成功连接客户机");
//初始化输入输出流
in = new DataInputStream(client.getInputStream());
out = client.getOutputStream();
//数据流
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("连接失败");
e.printStackTrace();
}
}
// 读图片
public void trans() throws IOException {
while (true){
int size = in.readInt();//读取对方发送的包大小
byte[] bytes = new byte[size];
int read = in.read(bytes);
ByteArrayInputStream stream = new ByteArrayInputStream(bytes);
BufferedImage image = ImageIO.read(stream);
g.drawImage(image,0,0,null);//将读到的图片进行绘制
System.out.println(read == bytes.length);
}
}
}
到了这里,关于Java实现视频通话的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!