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



任务

"使用http post请求在端点/图像上发送图像,并使用多部分编码或base64编码的数据参数发送图像。"

我正在为我的图像识别项目使用CloudSight API。refference http://docs.cloudsight.apiary.io/#reference/0/images-collection/send-an-image-for-isenefication

问题

我需要从编码为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,而在5-8之间的JPEG压缩级别。我们在内部进行调整大小,并且可以放慢请求过程。" <</em>

所以我需要在发送图像之前调整大小吗?您可以分享一些例子吗?

主要活动

              try {
                    Bitmap bitmap = photo();
                    ByteArrayOutputStream stream = new ByteArrayOutputStream();
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
                    bitmap.recycle();
                    byte[] array = stream.toByteArray();
                    encoded_String = Base64.encodeToString(array, 0);
                } catch (Exception e) {
                }
                new BackgroundWorker().execute(encoded_String);

背景活动

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

和PHP文件代码是

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

相关内容

  • 没有找到相关文章

最新更新