简体   繁体   English

Java android将图片发送到服务器,如何创建框架?

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

How I can create frame which look like this: 我如何创建看起来像这样的框架:

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)

I don't know how I can create this frame. 我不知道如何创建此框架。

A width and height img I get just like this: 我得到的宽度和高度img如下所示:

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

Here I get the size: 在这里我得到的大小:

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;

Seems You need Socket connection . 似乎您需要Socket 连接 Something like this (after your code ): 像这样(在您的代码之后):

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

You can google more Socket connection examples, for example this . 您可以在Google上搜索更多Socket连接示例,例如this

Update 更新

You also can use sockets to send strings to your server: 您还可以使用套接字将字符串发送到服务器:

clientSocket = new Socket(serverAddr, SOCKET_SERVERPORT);

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

And You should do it in separate (not UI) thread. 并且您应该在单独的(不是UI)线程中执行此操作。 Full example is something like that: 完整的例子是这样的:

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

And than You can use SocketClientThread this way: 然后您可以通过以下方式使用SocketClientThread

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM