手机上的aotujs代码,主要利用截图手机屏幕转换byte[]数据用tcpip传输到电脑配合java框架jframe显示手机实时界面等
importClass(java.net.InetAddress);
importClass(java.net.Socket);
importClass(java.net.ServerSocket);
importClass(java.io.OutputStream);
importClass(java.io.InputStream);
importClass(java.io.IOException);
importClass(java.io.OutputStreamWriter);
importClass(java.io.InputStreamReader);
importClass(android.content.Context);
importClass(android.net.wifi.WifiInfo);
importClass(android.net.wifi.WifiManager);
importClass(java.io.ByteArrayOutputStream);
/**
* 自动获取截屏权限
*/
auto.waitFor();
threads.start(function(){
let bu = text("立即开始").findOne(1000);
if(bu != null){
bu.click();
}else if(bu = text("允许").findOne(1000)){
bu.click();
}
});
if(!requestScreenCapture()){//截屏权限
exit();
}
sleep(2000);
//一直保持屏幕常亮
// device.keepScreenOn();
var ser1 = null;
var soc1 = null;
var output = null;
var input = null;
var boo = false;
let dimg = (new Canvas(device.width, device.height)).toImage();//对比图,意思上一张图片
let bx = 0;
let by = 0;
let bw = device.width/4;
let bh = device.height/8;
let bi = 0;
let ds = 0;
let setw = device.width;
let seth = device.height;
let bunisboo = 0;//是否按下
let bunxyarr = new Array(1000);
while(true){
let tk = new Date().getTime();
if(!boo){
linknet();
threads.start(function(){
while(boo){
try {
var str4 = input.read();
var yg = String.fromCharCode(str4);
if(yg=="\n"){
getcaozuo(input);
}
} catch (error) {
log(error);
output.close();
input.close();
soc1.close();
ser1.close();
log("操作线程错误");
}
}
});
}
if(ds==0){
let th = threads.start(function(){
let timg = captureScreen();
try {
ds = 1;
timg = images.resize(timg, [setw, seth]);//480,960
} catch (error) {
}
sendimg(timg, 0, 0);
ds = 0;
timg.recycle();
});
let ttime = new Date().getTime();
th.join(20);
let tb = new Date().getTime() - ttime;
if(tb<20){
sleep(20-tb);
}
}
}
//发送图片
function sendimg(simg, mx,my){
try {
let imgt = images.toBytes(simg, "jpg", 90);
output.write(xm(imgt.length+","+ mx +","+ my +"\r\n\r\n"));//图像大小,位置
output.write(imgt);
imgt = [0];
} catch (error) {
log("发送失败");
boo = false;
}
}
//链接网络wifi
function linknet(){
var ip = getIP();
log("服务已准备,等待链接端口 3587 ip="+ip);
log("屏幕大小:" + device.width + "*" + device.height);
try {
if(ser1 != null){
try {
(new Socket(InetAddress.getByName(ip),3587)).close();
log("尝试断开,释放");
} catch (error) {
log(error);
log("尝试断开,释放");
}
ser1.close();
log("关闭服务器");
}
ser1 = new ServerSocket(3587, 1,InetAddress.getByName(ip));//端口被占用没有释放 "192.168.107.227"
} catch (error) {
log(error);
sleep(3000);
toastLog("服务器创建失败,3秒后重试")
return 0 ;
}
toastLog("服务器重建成功,等待链接");
try {
if(soc1 != null){
soc1.close();
}
soc1 = ser1.accept();//等待客户接入服务器
} catch (error) {
log(error);
toastLog( "ser1.accept(),异常");
sleep(10*1000);
return 0 ;
}
output = soc1.getOutputStream();
input = new InputStreamReader(soc1.getInputStream(),"utf-8");
boo = true;
toastLog("链接成功");
}
//接收操作指令数据
function getcaozuo(inr){
log("操作指令运行,等待链接");
try {
var stt ="";
var bl = 0 ;
while(bl!=-1){
bl = inr.read();
if(bl!=-1){
var yg = String.fromCharCode(bl);
// log("接收字符:"+yg);
if(yg=="\n"){
log("接收指令:"+stt);
var tb = stt.split(",");
if(tb[0]==101){
press(tb[1], tb[2], tb[5]);
}
if(tb[0]==102){
swipe(tb[1],tb[2],tb[3],tb[4],tb[5]);
}
if(tb[0]==103 ){
// threads.start(function(){
// let a = parseInt(tb[3]) , b = parseInt(tb[4]);
// swipe(tb[1],tb[2],tb[3],tb[4],tb[5]);
// });
}
if(tb[0]==105){
setw = tb[1];
seth = tb[2];
}
if(tb[0]==107){
log("按下");
bunisboo=1;
}
if(tb[0]==108){
log("释放");
bunisboo=0;
}
if(tb[0]==110){
window.setPosition(tb[1],parseInt(tb[2])-110);
}
// log("接收指令:"+stt);
return ;
}
stt += yg;
}
}
} catch (error) {
log(error);
log("操作错误");
}
}
//字符串转字节数组
function xm(str) {
var ch, st, re = [];
for (var i = 0; i < str.length; i++ ) {
ch = str.charCodeAt(i); // get char
st = []; // set up "stack"
do {
st.push( ch & 0xFF ); // push byte to stack
ch = ch >> 8; // shift value down by 1 byte
} while ( ch );
re = re.concat( st.reverse() );
}
// return an array of bytes
return re;
}
//获取本机ip
function getIP(){
var ip = "";
// 获取WiFi服务
var wifiManager = context.getSystemService(Context.WIFI_SERVICE);
// 判断WiFi是否开启
if (wifiManager.isWifiEnabled()) {
// 已经开启了WiFi
var wifiInfo = wifiManager.getConnectionInfo();
var ipAddress = wifiInfo.getIpAddress();
ip = (ipAddress & 0xFF) + "." +
((ipAddress >> 8) & 0xFF) + "." +
((ipAddress >> 16) & 0xFF) + "." +
(ipAddress >> 24 & 0xFF);;
return ip;
}
return null ;
}
电脑
代码实现接受图片进行显示,监听鼠标操作发送手机执行点击,滑动操作文章来源:https://www.toymoban.com/news/detail-613080.html
package frame;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Date;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class AFrame extends JFrame implements MouseListener,MouseMotionListener,WindowListener{
/*
*手机上创建服务器
*电脑去链接手机的这个服务器套接字,进行接收图片,端口3587
*手机上创建服务器接收电脑发送过去的点击操作指令,端口3587
*端口套接字在电脑操作手机有数据时进行短暂链接
* */
String ip = "192.168.1.95";//本机ip,无用
int port = 3587;/
String deviceip ="";
Socket socket =null;
InputStream input = null;
PaintImg pi;//绘制图片
long time1 = new Date().getTime();//鼠标按下时间
long time2 = new Date().getTime();//鼠标按下时间
int ex = 0, ey =0;//鼠标按下坐标
int eb = 0 ; //鼠标按下的建
double w = 1080 , h = 2340;//设备尺寸
double rx = w/(AFrame.this.getContentPane().getWidth());
double ry = h/(AFrame.this.getContentPane().getHeight());
boolean boo = true;
OutputStreamWriter out = null;
int tu = 0;
long ttm = new Date().getTime();
boolean bok;//正在转码显示中
int ti=0;//记录掉帧
String tb = "";//显示掉帧
String str = "";
String[] stra;
long tl1 = 0;
boolean bgk;//横竖
ByteArrayOutputStream bn = new ByteArrayOutputStream(1024*500);
byte[] bk = new byte[1024*500];
Image age = null;
ImageIcon iicon = null;
BufferedImage bimg = new BufferedImage(480, 960, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2img = bimg.createGraphics();
//鼠标按下标志
boolean boo_mousepress = false;
double tcm = 0;
boolean tuotuo;
public AFrame(String deviceip , String w, String h, boolean bgk) {
this.deviceip = deviceip;
this.w = Integer.parseInt(w);
this.h = Integer.parseInt(h);
this.bgk = bgk;
setIconImage(new ImageIcon("src/frame/999.jpg").getImage());
setTitle("设备:"+deviceip);
if(bgk) {
setSize(360, 720);
}else {
setSize(900, 480);
}
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
getContentPane().add(pi=new PaintImg());
pi.addMouseListener(this);
pi.addMouseMotionListener(this);
addWindowListener(this);
new Thread(new Runnable() {
public void run() {
while(boo) {
try {
System.out.println("正在链接"+deviceip);
socket = new Socket(InetAddress.getByName(deviceip), port);
System.out.println("链接成功");
input = socket.getInputStream();
out = new OutputStreamWriter(socket.getOutputStream(), "utf-8");
} catch (UnknownHostException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
soc();
}
}
}).start();
}
//链接服务器,接收图像
public void soc() {
try {
while(boo) {
tl1 = new Date().getTime();
byte[] b = new byte[1024];
int a = 0;
while(a<b.length) {//
a += input.read(b, a, b.length-a);
}
str = new String(b);
// System.out.println("接受的数据头: "+str);
//\r\n\r\n,区分图片大小,头文件数据
str = str.substring(0,str.indexOf("\r\n\r\n"));
stra = str.split(",");
// System.out.println("大小"+stra[0] + "\n");
// System.out.println("位置" + stra[1] + "," + stra[2]);
int nx = Integer.parseInt(stra[1]), ny = Integer.parseInt(stra[2]);
int l = Integer.parseInt(stra[0]);//设备发送过来的图片大小
// System.out.println("图像大小:"+l);
// ByteArrayOutputStream bn = new ByteArrayOutputStream();
int u = str.getBytes().length+4;//图片大小占用大小
int lw = b.length-u;//剪掉图片大小信息占用的剩余图文信息
bn = new ByteArrayOutputStream();
bn.write(b,u,b.length-u);//写入剩余图文信息
while(lw<l && a!=-1) {
b = new byte[l-lw];
a = input.read(b,0 , b.length);
if(a!=-1) {
bn.write(b,0,a);
lw += a;
// System.out.println("大小"+l +"\t写入"+lw);
}
b = null;
}
// System.out.println("实际写入" + lw);
lw = 0;
a = 0;
bk = bn.toByteArray();
iicon = new ImageIcon(bk);
age = iicon.getImage();
// System.out.println(bimg.getWidth()+","+age.getHeight(null));
if(bimg.getWidth() != age.getWidth(null) && bimg.getWidth()!= age.getHeight(null)) {
bimg = new BufferedImage(age.getWidth(null), age.getHeight(null), Image.SCALE_DEFAULT);
g2img = bimg.createGraphics();
}
g2img.drawImage(age, nx, ny, null);//绘制图片
pi.setImg();//绘制图片age
age = null;
iicon = null;
str = null;
stra = null;
bk = null;
bn = null;
}
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
//绘制图片
class PaintImg extends JPanel{
// Image img = null;
long tm = 0;
long tm1 = 0;
int tm2 = 0;
int tm3 = 0;
String zs = "";//显示帧数
public void setImg() {//Image img
long j = new Date().getTime()-tm;
tm = new Date().getTime();
zs = "间隔"+j+"/ms";
j = 0;
this.repaint();
}
@Override
public void paint(Graphics g) {
super.paint(g);
if(bgk) {
//竖屏age
g.drawImage(bimg, 0, 0, AFrame.this.getContentPane().getWidth(), AFrame.this.getContentPane().getHeight(), this);
g.setColor(Color.yellow);
g.setFont(new Font("黑体", Font.BOLD, 20));
g.drawString(zs+tb, 10, 20);
}else {
//横屏
g.setColor(Color.yellow);
g.setFont(new Font("黑体", Font.BOLD, 20));
Graphics2D g2= (Graphics2D)g;
int centerx = 0;
int centery = AFrame.this.getContentPane().getHeight();
g.drawString(zs+tb, centerx, centery);
g2.rotate((-Math.PI/2)*1,centerx, centery);//坐标原点旋转90度
// g.drawString(zs+tb, centerx, centery);
g2.drawImage(bimg, centerx,centery, AFrame.this.getContentPane().getHeight(), AFrame.this.getContentPane().getWidth(), AFrame.this);
// System.out.println(AFrame.this.getContentPane().getHeight() + "," + AFrame.this.getContentPane().getWidth());
g2.rotate((Math.PI/2)*1,centerx, centery);//坐标原点旋转90度
g.drawString(zs+tb, 10, 20);
// g.drawString(zs+tb, centerx, centery);
}
}
}
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
if(e.getButton()==1) {
// sendtxt("107,"+getWidth() + "," + getHeight() + "\n");
time1 = new Date().getTime();//鼠标按下时间
time2 = new Date().getTime();//鼠标按下时间
if(bgk) {
rx = this.w/(AFrame.this.getContentPane().getWidth());
ry = this.h/(AFrame.this.getContentPane().getHeight());
ex = (int)((e.getX())*rx);
ey = (int)((e.getY())*ry);
}else {
rx = this.h/(AFrame.this.getContentPane().getWidth());
ry = this.w/(AFrame.this.getContentPane().getHeight());
ex = (int)((e.getX())*rx);
ey = (int)w - (int)((e.getY())*ry);
}
boo_mousepress = true;
tcm = 0;
if(bimg.getWidth() != getWidth()) {
sendtxt("105,"+getWidth() + "," + getHeight() + "\n");
}
}
}
@Override
public void mouseReleased(MouseEvent e) {
int x = (int)((e.getX())*rx);
int y = (int)((e.getY())*ry);
if(bgk) {
rx = this.w/(AFrame.this.getContentPane().getWidth());
ry = this.h/(AFrame.this.getContentPane().getHeight());
x = (int)((e.getX())*rx);
y = (int)((e.getY())*ry);
}else {
rx = this.h/(AFrame.this.getContentPane().getWidth());
ry = this.w/(AFrame.this.getContentPane().getHeight());
x = (int)((e.getX())*rx);
y = (int)w - (int)((e.getY())*ry);
}
if(e.getButton()==1) {
// sendtxt("108,"+getWidth() + "," + getHeight() + "\n");
if(x==ex && y==ey) {
tcm = new Date().getTime()-time1;
if(bgk) {
String tr = "101,"+ x+","+y+","+x+","+y+","+(tcm)+",\n";
sendtxt(tr);
}else {
String tr = "101,"+ y+","+x+","+y+","+x+","+(tcm)+",\n";
sendtxt(tr);
}
}else {
tcm = new Date().getTime()-time1;
if(bgk) {
String tr = "102,"+ ex+","+ey+","+x+","+y+","+(tcm)+",\n";
sendtxt(tr);
}else {
String tr = "102,"+ ey+","+ex+","+y+","+x+","+(tcm)+",\n";
sendtxt(tr);
}
}
boo_mousepress = false;
tuotuo = false;
}
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
@Override
public void windowOpened(WindowEvent e) {
// TODO 自动生成的方法存根
}
@Override
public void windowClosing(WindowEvent e) {
System.out.println("关闭该通道");
boo = false;
System.gc();
}
@Override
public void windowClosed(WindowEvent e) {
// TODO 自动生成的方法存根
}
@Override
public void windowIconified(WindowEvent e) {
// TODO 自动生成的方法存根
}
@Override
public void windowDeiconified(WindowEvent e) {
// TODO 自动生成的方法存根
}
@Override
public void windowActivated(WindowEvent e) {
// TODO 自动生成的方法存根
}
@Override
public void windowDeactivated(WindowEvent e) {
// TODO 自动生成的方法存根
}
@Override
public void mouseDragged(MouseEvent e) {
}
@Override
public void mouseMoved(MouseEvent e) {
int x = (int)((e.getX())*rx);
int y = (int)((e.getY())*ry);
//横屏竖屏切换
if(bgk) {
rx = this.w/(AFrame.this.getContentPane().getWidth());
ry = this.h/(AFrame.this.getContentPane().getHeight());
}else {
rx = this.h/(AFrame.this.getContentPane().getHeight());
ry = this.w/(AFrame.this.getContentPane().getWidth());
x = (int)((e.getY())*rx);
y = (int)((e.getX())*ry);
}
//
}
//从套接字中获取输出流,写入操作数据
public void sendtxt(String datstr) {
try {
out.write("\n"+datstr);
out.flush();
System.out.println("发送操作数据;\n"+datstr);
} catch (IOException e) {
e.printStackTrace();
}
}
}
主窗体类文章来源地址https://www.toymoban.com/news/detail-613080.html
package frame;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class MFrame extends JFrame {
JTextField jt1 = new JTextField("192.168.248.153x1080x2400");//手机ip
JButton jb = new JButton("竖屏链接");
JButton jb1 = new JButton("横屏链接");
public MFrame() {
setTitle("输入设备ip");
setIconImage(new ImageIcon("src/frame/999.jpg").getImage());
setSize(650, 120);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(null);
jt1.setBounds(10, 10, 400, 50);
jt1.setFont(new Font("黑体", Font.BOLD, 25));
c.add(jt1);
jb.setBounds(420, 10, 100, 50);
c.add(jb);
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new Thread(new Runnable() {
public void run() {
String[] tr = jt1.getText().split("x");
new AFrame(tr[0], tr[1], tr[2], true).setVisible(true);;
}
}).start();
}
});
jb1.setBounds(530, 10, 100, 50);
c.add(jb1);
jb1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new Thread(new Runnable() {
public void run() {
String[] tr = jt1.getText().split("x");
new AFrame(tr[0], tr[1], tr[2], false).setVisible(true);;
}
}).start();
}
});
}
public static void main(String[] args) {
new MFrame().setVisible(true);
}
}
到了这里,关于autojs与java开发的手机投屏,可在电脑操作手机的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!