【问题标题】:Send image socket io and android发送图片socket io和android
【发布时间】:2017-10-23 04:34:07
【问题描述】:

我是 socket.io 的初学者。我用过一个图书馆

https://www.npmjs.com/package/socket.io-stream

我们已使用浏览器成功上传图片。但现在,我想从 android 应用程序上传图片。如果有人有安卓代码请给我..

https://github.com/socketio/socket.io-client-java/issues/29

我一直在谷歌上搜索,但没有找到任何合适的解决方案。

【问题讨论】:

    标签: javascript java android sockets socket.io


    【解决方案1】:
       var imageBuffer = customJs.decodeBase64Image(base64Data);
       var imageTypeDetected = imageBuffer.type.match(/\/(.*?)$/);
       var filename = 'profile-' + Date.now() + '.' + imageTypeDetected[1];
       // config.uploadImage --- Folder path where you want to save.
       var uploadedImagePath = config.uploadImage + filename;
       try {
           fs.writeFile(uploadedImagePath, imageBuffer.data, function () {
           dbMongo.updateImage({email: decoded.email, user_id: decoded.userId, 'profile_picture': config.showImagePath + filename}, function (res) {
           if (res.error) { 
           socket.emit('set_update_image', {'error': 1, 'message': 'Error!' + res.message, 'data': null, 'status': 400});
           } else {
             console.log(res);
             socket.emit('set_update_image', res);
           }
           });
         });
          } catch (e) {
               socket.emit('set_update_image', {'error': 1, 'message': 'Internal server error ' + e, 'data': null, 'status': 400});
          }
    

    从其他文件调用函数

    exports.decodeBase64Image = function decodeBase64Image(dataString) {
    var matches = dataString.match(/^data:([A-Za-z-+\/]+);base64,(.+)$/);
    var response = {};
    
    if (matches.length !== 3)
    {
        return new Error('Invalid input string');
    }
    
    response.type = matches[1];
    response.data = new Buffer(matches[2], 'base64');
    
    return response;
    }
    

    【讨论】:

    • 谢谢 yogesh。工作良好。但是base64不能上传5mb及以上的文件。有什么想法。
    【解决方案2】:

    在android中,您需要使用Base64对图像进行编码

     public void sendImage(String path)
     {
        JSONObject sendData = new JSONObject();
        try{
              sendData.put("imageData", encodeImage(path));
              socket.emit("image",sendData);
           }catch(JSONException e){
    
           }
     }
    
     private String encodeImage(String path)
     {
          File imagefile = new File(path);
          FileInputStream fis = null;
          try{
              fis = new FileInputStream(imagefile);
          }catch(FileNotFoundException e){
              e.printStackTrace();
          }
          Bitmap bm = BitmapFactory.decodeStream(fis);
          ByteArrayOutputStream baos = new ByteArrayOutputStream();
          bm.compress(Bitmap.CompressFormat.JPEG,100,baos);
          byte[] b = baos.toByteArray();
          String encImage = Base64.encodeToString(b, Base64.DEFAULT);
          //Base64.de
          return encImage;
    }
    

    在服务器端,接收图像并解码

    socket.on("image", function(info) {
        var img = new Image();
        img.src = 'data:image/jpeg;base64,' + info.imageData;
    
    });
    

    【讨论】:

      【解决方案3】:

      对于使用套接字从android上传图像,您需要将图像作为base64字符串发送,

      以下是将 Image 转换为 base64 的示例,然后发送与其他参数相同的数据。

      String base64Image = getBase64Data(dirPath + "/" + fileName);
      
      public String getBase64Data(String filePath) {
              try {
                  InputStream inputStream = new FileInputStream(filePath);//You can get an inputStream using any IO API
                  byte[] bytes;
                  byte[] buffer = new byte[8192];
                  int bytesRead;
                  ByteArrayOutputStream output = new ByteArrayOutputStream();
                  try {
                      while ((bytesRead = inputStream.read(buffer)) != -1) {
                          output.write(buffer, 0, bytesRead);
                      }
                  } catch (Exception e) {
                      e.printStackTrace();
                  }
                  bytes = output.toByteArray();
                  return "data:image/jpeg;base64," + Base64.encodeToString(bytes, Base64.DEFAULT);
              } catch (Exception e) {
                  e.printStackTrace();
              }
              return "";
          }
      

      【讨论】:

      • 请 socket-io-stream 库,这是 npm 包。您的代码是否适用于该库
      • 我没有做任何代码相关的套接字我在这里建议你,你可以发送图像数据只是转换成字符串。
      猜你喜欢
      • 2013-10-20
      • 2019-08-11
      • 1970-01-01
      • 2016-04-17
      • 2013-03-23
      • 2013-08-20
      • 2020-07-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多