电脑上的java代码
import java.awt.Graphics;
import java.awt.Image;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
public class MainFrame extends JFrame {
Graphics g ;
String ipt = "192.168.42.238";//修改绑定ip地址
public MainFrame() {
setSize(500, 800);
setTitle("手机投屏");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
g = this.getContentPane().getGraphics();
}
//获取不同位置的图片,把拆分的图片进行合成,上中下3部分
//1.绘制顶部图片,图片高度为,图片实际高度/3,(480, 720)
public void paintTop(Image img) {
g.drawImage(img, 0, 0, this);
}
public void paintCent(Image img) {
g.drawImage(img, 0, 240, this);
}
public void paintDown(Image img) {
g.drawImage(img, 0, 480, this);
}
//使用udp获取图片
public Image getIm(int port) throws IOException {
Image img =null;
DatagramSocket dts = new DatagramSocket(port , InetAddress.getByName(ipt));
byte[] b = new byte[1024 * 64];//缓存64k
DatagramPacket dtp = new DatagramPacket(b , b.length);
//读取数据
dts.receive(dtp);
//把字节数据转换为图片
img = new ImageIcon(b).getImage();
dts.close();
return img;
}
public static void main(String[] args) {
MainFrame mf = new MainFrame();
while(true) {
Thread th1 = new Thread(new Runnable() {
public void run() {
try {
mf.paintTop(mf.getIm(8080));
} catch (IOException e) {
e.printStackTrace();
}
}
});
th1.start();
Thread th2 = new Thread(new Runnable() {
public void run() {
try {
mf.paintCent(mf.getIm(8081));
} catch (IOException e) {
e.printStackTrace();
}
}
});
th2.start();
Thread th3 = new Thread(new Runnable() {
public void run() {
try {
mf.paintDown(mf.getIm(8082));
} catch (IOException e) {
e.printStackTrace();
}
}
});
th3.start();
try {
Thread.sleep(17);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
手机上的脚本代码
importClass(java.net.DatagramPacket);
importClass(java.net.DatagramSocket);
importClass(java.net.InetAddress);
importClass(java.io.IOException);
//获取截屏权限
if(!requestScreenCapture()){
toast("请求截图失败");
exit();
}
sleep(1000);
console.show();//打开控制台
//修改绑定ip地址
vaript="192.168.42.238";
//截屏,获取当前屏幕
functionxme2(){
varimg=captureScreen();//截屏
varimg1=images.resize(img, [480,720]);//改变图片尺寸
returnimg1;//返回图片字节数组
}
//使用 UDP 发送图片数据
functionxem1(arr,port){//参数arr要发送的数据
try{
vardts=newDatagramSocket();
vardtp=newDatagramPacket(arr,arr.length,InetAddress.getByName(ipt) ,port);//指定发送的数据 , 发送到指定的ip , 端口 ,
dts.send(dtp);//发送数据
log("发送");
dts.close();
}catch(IOException){
log("io异常,发送失败");
log(IOException);
}
}
//程序运行逻辑
while(1){//循环执行 1截屏 , 2发送
//1.截屏
log("截屏");
varimg3=xme2() ;//返回的是一张图片
//把图片3分
varimga=images.clip(img3,0,0,480,240);
varimgb=images.clip(img3,0,240,480,240);
varimgc=images.clip(img3,0,480,480,240);
imga=images.toBytes(imga,"jpg",80);
imgb=images.toBytes(imgb,"jpg",80);
imgc=images.toBytes(imgc,"jpg",80);
if(imga.length<1024*64){
xem1(imga,8080);
}
if(imgb.length<1024*64){
xem1(imgb,8081);
}
if(imgc.length<1024*64){
xem1(imgc,8082);
}
sleep(17);
}