我正在开发包含签名板的Android应用程序。
我想将生成的位图上传到远程服务器。
我还没有找到任何资源来说明如何管理这个位图以及如何将其转换为可上传的格式。
这是获取签名板位图的函数,以及将其上传到远程服务器所需的功能:
btnFirmar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Bitmap signatureBitmap = mSignaturePad.getSignatureBitmap();
uploadBitmap(signatureBitmap);//WHAT TO DO WITH THIS...
}
});
答案 0 :(得分: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);
}
答案 1 :(得分:0)
什么过程?对于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;
}
服务器通过发布请求接收并使用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
}
}