将base64编码的图像发送到服务器

时间:2017-10-05 07:59:52

标签: android base64 http-post httprequest cloudsight

任务

“使用多部分格式编码或base64编码数据参数在端点/图像上使用HTTP POST请求发送图像。”

我正在使用Cloudsight API进行图像识别项目。参考http://docs.cloudsight.apiary.io/#reference/0/images-collection/send-an-image-for-identification

问题

我需要从编码到base64format的图库中发送图像,但是我收到服务器错误500.我似乎无法在我的代码中发现问题,可能图像编码不正确?

代码

    private String uploadData(String url) {
        String sResponse = "";
        try {
            HttpClient httpClient = new DefaultHttpClient();
            HttpContext localContext = new BasicHttpContext();
            HttpPost httpPost = new HttpPost(url+"/images");
            httpPost.addHeader("Content-Type", "application/json");
            httpPost.addHeader("Authorization", "CloudSight API_KEY");

                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                bitmapImage.compress(Bitmap.CompressFormat.JPEG, 100, bos);
                byte[] data = bos.toByteArray();

                String encImage = Base64.encodeToString(data, Base64.DEFAULT);

            JSONObject obj = new JSONObject();

            obj.put("remote_image_url",encImage );
            obj.put("locale", "en");
            httpPost.setEntity(new StringEntity(obj.toString()));

            HttpResponse response = httpClient.execute(httpPost, localContext);
            int responseCode = response.getStatusLine().getStatusCode();
            sResponse = inputStreamToString(
                    response.getEntity().getContent()).toString();
            Log.i("ResponseString", sResponse);
            Log.i("code", responseCode+"");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return sResponse;
    }

    private class uploadImage1 extends AsyncTask<String, String, String>{
        ProgressBar progressBar = new ProgressBar(getApplication(), null, android.R.attr.progressBarStyleSmall);

        protected void onPreExecute() {
            progressBar = (ProgressBar) findViewById(R.id.progressBar);
            progressBar.setVisibility(View.VISIBLE);
           super.onPreExecute();


        }

        @Override
        protected String doInBackground(String... params) {
            String url = params[0];
            String sResponse = uploadData(url);
            return sResponse;

        }
    }

修改

“注意:我们建议图像分辨率不高于1024x,JPEG压缩级别介于5-8之间。否则我们会在内部调整图像大小,这可能会降低请求过程。” < / p>

所以我需要在发送之前调整图片大小?你能分享一些例子吗?

1 个答案:

答案 0 :(得分:0)

主要活动

protected String doInBackground(String... params) {
    String login_url="Your link";
    try {
            String post;
            String number=params[0];
            String name=params[1];
            encoded_String=params[2];
            URL url = new URL(login_url);
            HttpURLConnection http = (HttpURLConnection) url.openConnection();
            http.setRequestMethod("POST");
            http.setDoInput(true);
            http.setDoOutput(true);
            OutputStream out = http.getOutputStream();
            BufferedWriter buffer = new BufferedWriter(new OutputStreamWriter(out, "UTF-8"));
            if(encoded_String.equals("null")) {
                post = URLEncoder.encode("number", "UTF-8") + "=" + URLEncoder.encode(number, "UTF-8") + "&" +
                        URLEncoder.encode("name", "UTF-8") + "=" + URLEncoder.encode(name, "UTF-8");
            }else{
                post = URLEncoder.encode("number", "UTF-8") + "=" + URLEncoder.encode(number, "UTF-8") + "&" +
                        URLEncoder.encode("name", "UTF-8") + "=" + URLEncoder.encode(name, "UTF-8") + "&" +
                        URLEncoder.encode("encoded_photo", "UTF-8") + "=" + URLEncoder.encode(encoded_String, "UTF-8");
            }
            buffer.write(post);
            buffer.flush();
            buffer.close();
            out.close();

背景活动

if(isset($_POST["encoded_photo"])){
    $encoded_string = $_POST["encoded_photo"];
    $image_name = rand().time().".jpg";

    $decoded_string = base64_decode($encoded_string);

    $path = 'image/'.$image_name;

    $file = fopen($path, 'wb');

    $is_written = fwrite($file, $decoded_string);
    fclose($file);
    $sql="INSERT INTO names(Number,Name,Refferel,photo) VALUES ('$number','$name','$Refferel','$path')";
    $result=mysqli_query($conn,$sql);
    if($result==true){
        echo "success";
    }

php文件代码是

{{1}}
相关问题