2017-10-05 1 views
-2

작업

는 "데이터 파라미터 인코딩 된 다중 형태로 인코딩하거나,베이스 64와 엔드/이미지에서 HTTP POST 요청을 사용하여 이미지를 전송합니다."서버베이스 64를 부호화 영상 전송

내 이미지 인식 프로젝트에 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 이하의 이미지 해상도와 5-8 사이의 JPEG 압축 수준을 권장합니다. 내부적으로 이미지의 크기를 조정하면 요청 프로세스가 느려질 수 있습니다." 그래서 보내기 전에 이미지의 크기를 조정해야합니까? 몇 가지 예를 들려 줄 수 있습니까? –

+0

안녕하세요. 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

나는 이미지를 재조정하는 곳을 정말로 본적이 없다? –

+0

여전히 오류 500이 표시됩니다. 이미지가 인코딩 된대로 인코딩되지 않았을 수 있습니다. –