Android客户端到python服务器图像发送(不支持)

2024-10-05 12:19:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在将我的相机捕获图像从android客户端发送到python服务器进行进一步的图像处理 我做了一些代码,它从android发送图像,但当我从python接收图像时,它不支持格式或可能已损坏 请帮帮我

Android Client:

    protected Void doInBackground(Void... params) {
                try {
                    s = new Socket("192.168.1.200", 8000); //Connects to IP address - enter your IP here
                   // File directory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); //Gets information about a said directory on your device - currently downloads
                   // File photoPath = new File(mCurrentPhotoPath);
                   // File photoPath = new File(mCurrentPhotoPath); //Define your image name I used png but other formats should also work - make sure to specify file extension on server
                    //InputStream input = new FileInputStream(photoPath.getAbsolutePath()); //Gets the true path of your image
                    try {
                        try {

                            DataOutputStream dos = new DataOutputStream(s.getOutputStream());

                            FileInputStream fis = new FileInputStream(mCurrentPhotoPath);
                            int size = fis.available();

                            byte[] data = new byte[size];

                            fis.read(data);
                            dos.writeInt(size);
                            dos.write(data);

                            dos.flush();
                            dos.close();
                            fis.close();


                        } finally {
                            //Flushes and closes socket
                            s.getOutputStream().flush();
                            s.close();
                        }
                    } finally {
                     //   input.close();
                    }


                } catch (IOException e) {
                    e.printStackTrace();
                }




 Python server:

import socket
    import struct
    address = ("192.168.1.200", 8000)
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.bind(address)
    s.listen(1000)
    print("Started Listening")

    client, addr = s.accept()
    print ("got connected from", addr)

    #buf = 0
    #while len(buf)<4:
    #     buf += client.recv(4-len(buf))
    #size = struct.unpack('!i', buf)
    print ("receiving %s bytes")

    with open('data.jpg', 'wb') as img:
        while True:
            data = client.recv(1024)
            if not data:
                break
            img.write(data)
    print ("received, yay!")

    client.close()

在我的电脑中以图像形式写入数据,但不打开/不支持格式化


Tags: 图像clientnewcloseyourdatasizeaddress

热门问题