gpt4 book ai didi

java - ANDROID - 通过 tcp 套接字将屏幕截图从 java 发送到 android

转载 作者:可可西里 更新时间:2023-11-01 02:31:47 27 4
gpt4 key购买 nike

我通过套接字进行了客户端-服务器聊天,效果很好。现在我想为服务器(Android 手机)添加一个选项,以从客户端(PC 应用程序)抓取屏幕截图。创建屏幕截图工作正常,但从客户端到服务器的传输每次都失败。

客户端/发件人:

之前我直接将图像写入输出流,但是在服务器端出现错误,所以我尝试了这种方式,但它是一样的。

public class ClientScreenshotThread implements Runnable {

// - Software Init - //
private Socket transferSocket;
private BufferedImage screenshot;
private Robot robot;
private BufferedWriter outToServer;
private FileInputStream inStream;
private DataOutputStream outStream;

// - Var Init - //
private final int SERVER_TRANSFER_PORT = 65000;

private int screenWidth, screenHeight;

// -------------------------------------------------- //

public ClientScreenshotThread() {

}

@Override
public void run() {
try {
SocketAddress sockaddr = new InetSocketAddress(Client.SERVER_IP, SERVER_TRANSFER_PORT);
transferSocket = new Socket();
transferSocket.connect(sockaddr, 5000); // 5sec Timeout

Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
robot = new Robot();

screenWidth = dimension.width;
screenHeight = dimension.height;

Rectangle screen = new Rectangle(screenWidth, screenHeight);

screenshot = robot.createScreenCapture(screen);
ImageIO.write(screenshot, "png", new File("/Users/chris/Downloads/screenshot.png"));

File file = new File("/Users/chris/Downloads/screenshot.png");
inStream = new FileInputStream(file);
byte[] buffer = new byte[4096];

// prepare server for receiving the screenshot
outToServer = new BufferedWriter(new OutputStreamWriter(transferSocket.getOutputStream()));
outToServer.write("#!<cmd>screenshot");
outToServer.newLine();
outToServer.flush();

// send the screenshot to the server
outStream = new DataOutputStream(transferSocket.getOutputStream());

int n;
int i = 0;
while((n = inStream.read(buffer)) != -1) {
i++;
System.out.println(i + ". Byte[" + n + "]");
outStream.write(buffer, 0, n);
outStream.flush();
}

} catch(AWTException e1) {
System.out.println("AWT: " + e1.getMessage().toString());
} catch(IOException e2) {
System.out.println("IO: " + e2.getMessage().toString());
} finally {
try {
// close streams and socket
inStream.close();
outToServer.close();
transferSocket.close();
} catch(IOException e) {
System.out.println(e.getMessage().toString());
}
}
}
}

服务器端/接收器:

我总是在以下位置得到“NullPointerException”:

bmp.compress(Bitmap.CompressFormat.PNG, 100, fileOutStream);

public class ServerTransferThread implements Runnable {

// - Software Init - //
private ServerSocket serverTransferSocket;
private Handler handler;
private BufferedReader inFromClient;
private DataInputStream inStream;
private ByteArrayOutputStream content;
private FileOutputStream fileOutStream;

// - Var Init - //
private final String TAG = "xxx";

private final int SERVER_TRANSFER_PORT = 65000;

// -------------------------------------------------- //

public ServerTransferThread(Handler _handler) {
this.handler = _handler;
}

@Override
public void run() {
Log.d(TAG, "ServerTransferThread: run()");

try {
serverTransferSocket = new ServerSocket(SERVER_TRANSFER_PORT);

while(ServerActivity.SERVER_STATE == true) {
Socket socket = serverTransferSocket.accept();
Log.d(TAG, "ServerTransferThread: accepted()");

inFromClient = new BufferedReader(new InputStreamReader(socket.getInputStream()));
Log.d(TAG, "ServerTransferThread: bufferedReader()");

String message = "";
if((message = inFromClient.readLine()) != null) {
if(message.equals("#!<cmd>screenshot")) {
receiveScreenshot(socket);
}
}
}
} catch(IOException e) {
Log.e(TAG, "ServerTransferThread 1: " + e.getMessage().toString());
} finally {
try {
inFromClient.close();
serverTransferSocket.close();
} catch (IOException e) {
Log.e(TAG, "ServerTransferThread 2: " + e.getMessage().toString());
}
}
}

private void receiveScreenshot(Socket socketX) {
Log.d(TAG, "ServerTransferThread: receiveScreenshot()");

try {
handler.sendMessage(buildMessage("> Receiving screenshot.."));
inStream = new DataInputStream(socketX.getInputStream());

byte[] buffer = new byte[4096];
content = new ByteArrayOutputStream();

inStream = new DataInputStream(socketX.getInputStream());

int n;
while((n = inStream.read()) != -1) {
content.write(buffer, 0, n); // HERE I "OUT OF MEMORY"
content.flush();
}

File directory = new File(ServerActivity.APP_FOLDER_PATH);
File screenshot = new File(ServerActivity.APP_FOLDER_PATH + "/" + "screenshot.png");

if(!directory.exists())
directory.mkdirs();

if(!screenshot.exists()) {
screenshot.createNewFile();
}
else {
screenshot.delete();
screenshot.createNewFile();
}

fileOutStream = new FileOutputStream(screenshot);
Bitmap bmp = BitmapFactory.decodeByteArray(content.toByteArray(), 0, content.size());
bmp.compress(Bitmap.CompressFormat.PNG, 100, fileOutStream);

handler.sendMessage(buildMessage("> Screenshot received sucessfully!"));

} catch(IOException e1) {
Log.e(TAG, "ServerTransferThread 3: " + e1.getMessage().toString());
} finally {
try {
inStream.close();
content.close();
fileOutStream.close();
socketX.close();
} catch (IOException e) {
Log.e(TAG, "ServerTransferThread 4: " + e.getMessage().toString());
}
}
}

private Message buildMessage(String text) {
Log.d(TAG, "ServerTransferThread: buildMessage()");

Message msg = handler.obtainMessage();
Bundle bundle = new Bundle();
bundle.putString("MESSAGE", text);
msg.setData(bundle);
return msg;
}

这是我的 Logcat 输出:

08-20 19:01:18.285: D/skia(5383): --- SkImageDecoder::Factory returned null
08-20 19:01:18.295: W/dalvikvm(5383): threadid=12: thread exiting with uncaught exception (group=0x40c6b1f8)
08-20 19:01:18.295: E/AndroidRuntime(5383): FATAL EXCEPTION: Thread-3051
08-20 19:01:18.295: E/AndroidRuntime(5383): java.lang.NullPointerException
08-20 19:01:18.295: E/AndroidRuntime(5383): at net.api.speak.wifi.ServerTransferThread.receiveScreenshot(ServerTransferThread.java:114)
08-20 19:01:18.295: E/AndroidRuntime(5383): at net.api.speak.wifi.ServerTransferThread.run(ServerTransferThread.java:58)
08-20 19:01:18.295: E/AndroidRuntime(5383): at java.lang.Thread.run(Thread.java:856)
08-20 19:01:27.820: D/Speak WiFi(5383): Server: onDestroy()
08-20 19:01:27.830: E/Speak WiFi(5383): Server: Socket closed
08-20 19:01:27.830: E/Speak WiFi(5383): ServerThread: Socket closed

编辑: 经过一些麻烦后,我找到了文件传输问题的最终解决方案!就是这样:


最终服务器端:

            int bytecount = 2048;
byte[] buf = new byte[bytecount];

OutputStream OUT = socket.getOutputStream();
BufferedOutputStream BuffOUT = new BufferedOutputStream(OUT, bytecount);
FileInputStream in = new FileInputStream(itemPath);

int i = 0;
while ((i = in.read(buf, 0, bytecount)) != -1) {
BuffOUT.write(buf, 0, i);
BuffOUT.flush();
}

最终客户端:

        FileOutputStream outToFile = new FileOutputStream(FileName);

int bytecount = 2048;
byte[] buf = new byte[bytecount];

// Create an inputstream for the file data to arrive
InputStream IN = socket.getInputStream();
BufferedInputStream BuffIN = new BufferedInputStream(IN, bytecount);

// Receiving file..
int i = 0;
int filelength = 0;
while((i = BuffIN.read(buf, 0, bytecount)) != -1) {
filelength += i;
outToFile.write(buf, 0, i);
outToFile.flush();
}

最佳答案

您能否将接收到的字节直接写入 FileOutputStream。在智能手机等相当有限的设备上将 png 转换为位图有时会导致问题。

是否需要在接收端创建Bitmap对象?

如果没有,你可以这样做:

InputStream is = socketX.getInputStream();
FileOutputStream outputFile = new FileOutputStream(screenshot);

int n;
while ((n = is.read(data, 0, data.length)) != -1) {
outputFile.write(data, 0, n);
}

我还没有测试代码。

关于java - ANDROID - 通过 tcp 套接字将屏幕截图从 java 发送到 android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12041986/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com