【问题标题】:Uploading signaturepad bitmap to remote server将签名板位图上传到远程服务器
【发布时间】:2018-01-27 07:08:04
【问题描述】:

我正在开发一个包含签名板的 Android 应用。

我想将生成的位图上传到远程服务器。

我没有找到任何资源来说明如何管理此位图以及如何将其转换为可上传的格式。

这是获取签名板位图的函数,以及将其上传到远程服务器所需的函数:

  btnFirmar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


                Bitmap signatureBitmap = mSignaturePad.getSignatureBitmap();



                uploadBitmap(signatureBitmap);//WHAT TO DO WITH THIS...




            }
        });

【问题讨论】:

    标签: android signaturepad


    【解决方案1】:

    我使用 Volley 库解决了如下问题:

     private void uploadBitmap() {
    
            dialog = new ProgressDialog(getActivity());
            dialog.setMessage("Uploading Signature...");
            dialog.setCancelable(false);
    
            jsonObject = new JSONObject();
            Bitmap image = signatureBitmap;
           dialog.show();
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            image.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
            String encodedImage = Base64.encodeToString(byteArrayOutputStream.toByteArray(), Base64.DEFAULT);
            try {
                jsonObject.put(Utils.imageName, numero);
                jsonObject.put(Utils.image, encodedImage);
            } catch (JSONException e) {
                Log.e("JSONObject Here", e.toString());
            }
            JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, Utils.urlUpload, jsonObject,
                    new Response.Listener<JSONObject>() {
                        @Override
                        public void onResponse(JSONObject jsonObject) {
                            Log.e("Message from server", jsonObject.toString());
                          dialog.dismiss();
                        //    messageText.setText("Image Uploaded Successfully");
                            Toast.makeText(getActivity(), "Signature Uploaded Successfully", Toast.LENGTH_SHORT).show();
                        }
                    }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError volleyError) {
                    Log.e("Message from server", volleyError.toString());
                    dialog.dismiss();
                }
            });
            jsonObjectRequest.setRetryPolicy(new DefaultRetryPolicy(5000,
                    DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                    DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
            Volley.newRequestQueue(getActivity()).add(jsonObjectRequest);
    
    
        }
    

    【讨论】:

      【解决方案2】:

      流程是什么?对于 C#,我正在尝试通过 HTTP 客户端获取图像并发送,也许,在服务器上只得到一个空白图像,这是 Xamarin Forms 上的代码:

                  <forms:SignaturePadView 
                  BackgroundColor="Transparent"
                  StrokeColor="Blue"
                  StrokeWidth="3"
                  HeightRequest="250"
                  Name="Signature"
              />
      

      在视图模型中:

      Stream image = await Signature.GetImageStreamAsync(SignatureImageFormat.Png);
      

      发送:

      var bytes = new byte[image.Length];
                      await image.ReadAsync(bytes, 0, (int)image.Length);
                      string imageBase64 = Convert.ToBase64String(bytes);
      

      根据要求:

      try
              {
                  var client = new HttpClient();
                  var response = await client.PostAsync(urlBase,
                      new StringContent(string.Format(
                      "imgSign={0}",
                      imageBase64),
                      Encoding.UTF8, "application/x-www-form-urlencoded"));
      
                  if (!response.IsSuccessStatusCode)
                  {
                      return response.ToString();
                  }
                  else
                  {
                      var response = await response.Content.ReadAsStringAsync();
                      return response;
                  }
      
              }
              catch
              {
                  return null;
              }
      

      服务器通过 Post 请求接收并使用 file_puts_contents 将图像发送到文件夹:

      if (isset ($image = $_POST['imgSign'])) {
          $dateNow = date("d-m-Y");
          $imageName = 'Id'.$dateNow;
          $image = $_POST['imgSign'];
          $path = "../images/$imageName.png";
      
          if(file_put_contents($path,base64_decode($image))){
              ...update DB
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-12-01
        • 2010-11-23
        • 2016-03-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多