【发布时间】:2019-06-24 20:23:22
【问题描述】:
我已经编写了用于从相机捕获图像并发送到服务器的代码。但有时它有时会因错误凌空超时错误而起作用。几乎所有情况下,当我使用使用 IPv6 IP 的 JIO 网络时(我不知道是什么影响了这一点),它都不起作用。
我正在捕获图像并转换为 base64 并发送到 POST 到 PHP 服务器。
启动相机:
btn_cam.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
captureImage();
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent,CAMERA_REQUEST_CODE);
} catch (Exception e){
e.printStackTrace();
}
}
});
捕获图像后:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST_CODE && resultCode == RESULT_OK) {
Bitmap bitmap = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream.toByteArray();
encoded = Base64.encodeToString(byteArray, Base64.DEFAULT);
}
}
然后将编码后的字符串发送到 APACHE 服务器:
btn_submit.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
_house_no = house_number.getText().toString();
_state = state.getText().toString();
_streetName = locality.getText().toString();
_city = city.getText().toString();
_postalCode = postcode.getText().toString();
_state = state.getText().toString();
_district = district.getText().toString();
_tahsil = "0";
final ProgressDialog loading = ProgressDialog.show(MainActivity.this, "", "Please wait...", false, false);
try {
RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
String URL = ServerLinks.SUBMIT;
JSONObject jObj = new JSONObject();
jObj.put("userID",userID);
jObj.put("house",_house_no);
jObj.put("street",_streetName);
jObj.put("city",_city);
jObj.put("post_code",_postalCode);
jObj.put("state",_state);
jObj.put("district",_district);
jObj.put("lat",lat);
jObj.put("long",longi);
jObj.put("image",encoded);
final String requestBody = jObj.toString();
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d("FINAL_SUBMIT_RESPONSE", response.toString());
loading.dismiss();
try {
JSONObject object = new JSONObject(response);
int response_code = object.getInt("code");
String message = object.getString("Message");
if (response_code == 400) {
Snackbar.make(rootLinearLayout, message, Snackbar.LENGTH_SHORT).show();
house_number.setText("");
} else {
Snackbar.make(rootLinearLayout, message, Snackbar.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
loading.dismiss();
VolleyLog.d("RESULT_ERROR", "Error: " + error.getMessage());
Toast.makeText(getApplicationContext(), "Something Went Wrong!! Please submit it again", Toast.LENGTH_SHORT).show();
}
}) {
@Override
public byte[] getBody() throws AuthFailureError {
try {
return requestBody == null ? null : requestBody.getBytes("utf-8");
} catch (UnsupportedEncodingException uee) {
VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", requestBody, "utf-8");
return null;
}
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("Content-Type", "application/json");
params.put("x-api-key", OAuth);
return params;
}
};
stringRequest.setShouldCache(false);
requestQueue.add(stringRequest);
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}`
您能帮我如何提高图像表单的效率吗?提前谢谢你
【问题讨论】:
标签: android performance image-processing