0

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

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

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

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

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


                Bitmap signatureBitmap = mSignaturePad.getSignatureBitmap();



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




            }
        });
4

2 回答 2

0

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

            <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;
        }

服务器通过发布请求接收并使用 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
    }
}
于 2019-06-12T15:36:14.263 回答
0

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

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


    }
于 2018-01-27T08:02:13.743 回答