2017-10-05 109 views
-2

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

“通过使用HTTP POST请求的端点/图片包含有任一多部分形式的编码,或者编码数据的base64参数发送的图像”。

我为我的图像识别项目使用了Cloudsight API。 REFFERENCE 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; 

     } 
    } 

编辑

。“注意:我们推荐的图像分辨率不低于1024×高,5-8之间的JPEG压缩级别,我们调整图像,否则内部,并且可能会减慢请求过程。“

所以我需要调整我的形象之前我发送它?你能分享一些例子吗?

+0

服务器500是内部服务器错误。请问服务器团队解决问题。 –

+0

“注意:我们推荐图像分辨率不高于1024x,JPEG压缩等级在5-8之间,否则我们会调整图像大小,否则会降低请求过程的速度。” 所以我需要调整我的形象之前我发送它?你能分享一些例子吗? –

+0

欢迎来到SO。 1)更加努力地设置代码的格式2)为什么使用代码片段? 3)这是一项任务吗?下一次,请正确格式化,以便我们了解源任务是什么以及您提出的问题是什么4)提出具体问题,而不是多个随机和宽泛问题 - 没有人会与您分享完整解决方案的示例,这不是这个网站是为。 5)请在发帖前仔细阅读:https://stackoverflow.com/help/asking –

回答

-1

主要活动

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

得到500错误我真的看不到你调整图像的位置? –

+0

我仍然收到错误500,也许图像不是应该编码的 –

相关问题