簡體   English   中英

Java android將圖片發送到服務器,如何創建框架?

[英]Java android send picture to server, how I can create a frame?

我如何創建看起來像這樣的框架:

0xB (byte)
0xA (byte)
0xA (byte)
0xD (byte)
width img (short)
height picture (short)
size img in byte  (int)
Binary data PICTURE
0xB (byte)
0xE (byte)
0xE (byte)
0xF (byte)

我不知道如何創建此框架。

我得到的寬度和高度img如下所示:

BitmapDrawable bd=(BitmapDrawable) this.getResources().getDrawable(R.drawable.icon);
int height=bd.getBitmap().getHeight();
int width=bd.getBitmap().getWidth();

在這里我得到的大小:

Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(), 
    R.drawable.ic_launcher);
Bitmap bitmap = bitmapOrg;
ByteArrayOutputStream stream = new ByteArrayOutputStream();   
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);   
byte[] imageInByte = stream.toByteArray(); 
long lengthbmp = imageInByte.length;

似乎您需要Socket 連接 像這樣(在您的代碼之后):

Socket clientSocket;

clientSocket = new Socket(<address_of_server>, <number_of_port>);

DataOutputStream dos = new DataOutputStream(clientSocket.getOutputStream());
dos.writeByte((byte)0xB);
dos.writeByte((byte)0xA);
dos.writeByte((byte)0xA);
dos.writeByte((byte)0xD);
dos.writeShort(width);
dos.writeShort(height);
dos.writeInt(lengthbmp);
dos.write(imageInByte);
dos.writeByte((byte)0xB);
dos.writeByte((byte)0xE);
dos.writeByte((byte)0xE);
dos.writeByte((byte)0xF);
...
clientSocket.close();

您可以在Google上搜索更多Socket連接示例,例如this

更新

您還可以使用套接字將字符串發送到服務器:

clientSocket = new Socket(serverAddr, SOCKET_SERVERPORT);

dos = new DataOutputStream(clientSocket.getOutputStream());
dos.writeUTF("Hello");
dos.writeUTF("World!");
...

並且您應該在單獨的(不是UI)線程中執行此操作。 完整的例子是這樣的:

class SocketClientThread implements Runnable {
        DataInputStream dis;
        DataOutputStream dos;
        String strResponseData;

        @Override
        public void run() {
            try {
                InetAddress serverAddr = InetAddress.getByName("<address>");
                clientSocket = new Socket(serverAddr, <port_number>);
                dos = new DataOutputStream(clientSocket.getOutputStream());
                dis = new DataInputStream(clientSocket.getInputStream());

                // now you can write data to stream
                dos.writeUTF("Hello");
                dos.writeUTF("World!");

                // you can also read data from stream
                strResponseData = dis.readUTF();


            } catch (UnknownHostException ignore) {
            } catch (IOException ignore) {
            }

            finally{
                if (clientSocket != null){
                    try {
                        clientSocket.close();
                    } 
                    catch (IOException ignore) {
                    }
                }
            }
        }
}

然后您可以通過以下方式使用SocketClientThread

Thread socketClientThread;
socketClientThread = new Thread(new SocketClientThread());
socketClientThread.start();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM