简体   繁体   English

将一堆图像从Android手机发送到PC

[英]Sending a bunch of images from an android phone to a PC

I'm working on an app that suppose to transfer images from an android phone to a PC in a server - client architecture (the java (android) code is added below).The photos suppose to pass through a bytearray. 我正在开发一个应用程序,该应用程序旨在将图像从Android手机传输到服务器中的PC-客户端体系结构(下面添加了Java(android)代码)。照片应该通过字节数组传递。 I'm having a hard time to figure out how to create a protocol over TCP that will be able to pass my images without loss of information and also be able to pass the META-DATA of the images such as the image name, extension, size, maybe META-DATA size (if needed?). 我很难弄清楚如何通过TCP创建一个协议,该协议将能够传递我的图像而不会丢失信息,还能够传递图像的META-DATA,例如图像名称,扩展名,大小,也许是META-DATA大小(如果需要?)。 I would really appreciate your help becuase I'm kind of new to C# and writing a client-server, especially one that suppose to transfer images of some sorts of extensions. 我非常感谢您的帮助,因为我是C#的新手,并且正在编写客户端服务器,尤其是那些假设要传输某种扩展名的图像的客户端服务器。

private void makeTCPConnection() {
        try {
            InetAddress serverAddr = InetAddress.getByName("10.0.2.2");
            //create a socket to make the connection with the server
            Socket socket = new Socket(serverAddr, 8000);

            try {
                //Sends the message to the server
                OutputStream output = socket.getOutputStream();
                File dcim = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
                if(dcim == null)
                    return;
                File[] pics=dcim.listFiles();
                int count=0;
                if(pics != null){
                    for(File pic:pics){

                    FileInputStream fis = new FileInputStream(pic);
                    Bitmap bm = BitmapFactory.decodeStream(fis);
                    byte[] imgbyte = getBytesFromBitmap(bm);
                    output.write(imgbyte);
                    output.flush();
                    }
                }
            }catch (Exception e){
             Log.e("TCP","S:Error",e);
            }finally {
                socket.close();
            }
        }catch (Exception e){
            Log.e("TCP","C:Error",e);
        }
    }

    public byte[] getBytesFromBitmap(Bitmap bitmap){
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG,70,stream);
        return stream.toByteArray();
    }

Have you considered to write a small REST web service ? 您是否考虑过编写小型REST Web服务

This approach would have some advantages: 这种方法将具有一些优点:

  • HTTP (human-readable) is used as a protocol, so you don't have to come up with an own protocol. HTTP(人类可读)用作协议,因此您不必提出自己的协议。
  • JSON (human-readable) can be used transmit data. JSON(人类可读)可用于传输数据。 (Maybe not for photos, but for metadata.) (也许不是用于照片,而是用于元数据。)
  • There are plenty libraries for the client and server side. 客户端和服务器端有很多库。
  • Client and server can be written in different languages. 客户端和服务器可以用不同的语言编写。
  • In some languages a REST web service can be created with just a few lines of code. 在某些语言中,仅需几行代码即可创建REST Web服务。
  • The REST web service can be written first and can be tested (with a REST client), before writing the client application. 可以在编写客户端应用程序之前先编写REST Web服务,并可以对其进行测试(使用REST客户端)。
  • More REST endpoints can be added easily. 可以轻松添加更多REST端点。 (Maybe you build a simple upload first and later add more endpoints to upload metadata.) (也许您先构建一个简单的上传,然后再添加更多终结点来上传元数据。)

For Android I can recommend Retrofit as a REST client. 对于Android,我可以建议将Retrofit作为REST客户端。 And if you want to use Java on the server side, I would recommend Spring/Spring Boot . 而且,如果您想在服务器端使用Java,则建议使用Spring / Spring Boot (How-to guide for a REST web service in Spring Boot: https://spring.io/guides/gs/rest-service/ ) (Spring Boot中的REST Web服务的操作指南: https : //spring.io/guides/gs/rest-service/

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

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