如何将捕获的图像发送到Apache服务器?

时间:2019-01-31 11:23:11

标签: android performance image-processing

我已经编写了用于从相机捕获图像并将其发送到服务器的代码。但是有时它可以正常工作,但有时不会出现错误截距超时错误。几乎在所有情况下,当我使用使用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();
            }

        }
    });
}`

能帮我个忙,如何提高图像的表格效率?预先谢谢你

1 个答案:

答案 0 :(得分:0)

在我的代码中,我这样做是这样的:

 HttpClient httpclient;
    httpclient = HttpClientSingalTon.getHttpClienttest();
    HttpPost httpPostRequest = new HttpPost(URL);
    // Try This
    httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
    MultipartEntity mpEntity = new MultipartEntity();
    ContentBody cbFile = new FileBody(yourimagefile, "image/jpeg");
    mpEntity.addPart("file", cbFile); 
    httpPostRequest.setEntity(mpEntity);
    HttpResponse response = (HttpResponse) httpclient.execute(httpPostRequest);