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

ANDROID-通过tcp套接字从java向ANDROID发送屏幕截图,java,android,image,sockets,tcp,Java,Android,Image,Sockets,Tcp,我通过sockets进行了一次客户机-服务器聊天,效果很好。现在我想为服务器(Android手机)添加一个选项,以便从客户端(pc应用程序)抓取屏幕截图。创建屏幕截图工作正常,但从客户端到服务器的传输每次都会失败 客户端/发送方: 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

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

客户端/发送方:

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();
        }
在我将图像直接写入输出流之前,我在服务器端遇到了一个错误,所以我尝试了这种方法,但结果是一样的

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());
        }
    }
}
}

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


最终服务器端:

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();
        }
最终客户端:

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转换为位图有时会导致问题

是否需要在接收器端创建位图对象

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

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);
}

我还没有测试代码。

NullPointerException是因为
位图工厂。decodeByteArray
返回:解码的位图,如果图像无法解码,则返回null

服务器端(receiveScreenshot()方法内部),将InputStream字节写入ByteArrayOutputStream“content”变量的部分不正确,因为
inStream.read()
应该是
inStream.read(buffer)

内存不足错误可以解释,因为
read()
方法一次只读取一个字节,然后您总是为每个字节写入4096字节的完整缓冲区

编辑:在写入fileOutputStream中的所有字节后,如果您不想直接提供字节,则将OutputStream压缩为png(因为在您的情况下,它不起作用):


这应该可以满足您的需要,但是解码文件很遗憾,因为您应该已经知道文件中的字节了

不,我不需要在接收器端转换位图。你是说我应该用ObjectOutputStream来处理这个问题?你能解释一下吗?我从谷歌上读了很多主题,但没有什么能解决我的问题:/它可以将输入数据写入一个文件(不知道数据是否正确)-但那只是一个文件.png。。我必须将此文件转换为图像格式,但如何转换?我试过使用BitmapFactory.decodeFile(“path/to/screenshot”)-这会抛出一个nullpointerexception。谢谢,我想我忘了。但这并没有解决问题。nullpointerexception仍然发生,因为我收到了真正的废话。发送方和接收方现在显示字节[]的数量以及它们发送/接收的大小,我注意到我发送的字节与我接收的字节[]完全不匹配。我发送了87字节[]-86,大小为[4096]和87。使用[771],但我收到214字节[]-前3个字节还行,但随后开始变得疯狂。。[1448]至[3352]的长度。。wtf——最后一个字节[](214.oô)是正确的->[771]。我似乎无法重现您的问题。我测试了你的代码,它对我来说很好(一个客户端从SD卡发送图像)。即使您读取较小的数据块,您收到的总字节数是否与您发送的总字节数相匹配?