2012-05-07 24 views
0

我想從Android發送圖片到用C++編寫的服務器。我希望使用套接字發送圖像,以便不必擔心C++和Java之間的差異。但是,Android中的圖像通常以位圖的形式存儲,它是Android中定義的類,但在C++中,該類不存在。所以我想知道如果我想以這種方式發送圖像該怎麼做。所以我來這裏尋求幫助,謝謝。如何將圖像從Android發送到用C++編寫的服務器

+0

看到這個問題[鏈接](http://stackoverflow.com/questions/9501550/how-can-i-make-an-android-app-communicate-with-a-web-server-over - 網絡/ 9503995#9503995) –

+0

請注意,該問題的答案也壓縮圖像爲JPEG,這是一件好事(tm),因爲通過3G上傳原始圖像是...昂貴... – Torp

+0

我已閱讀鏈接,謝謝。但我仍然想知道php中的函數move_uploaded_file()是否與C中的printf()相同? – Timothy

回答

0

發送圖像像.jpg文件或.png

+0

將圖像壓縮爲.jpg格式並將其作爲字節數組發送?非常感謝您的建議。 – Timothy

0

我假定服務器的自定義系統,你提到了用C++編寫。在這種情況下,您可以簡單地通過每個tcp/ip的套接字發送大小(x,y)和rgba數據(每像素32位)。

您只需打開一個從客戶端(android)到服務器(C++)的tcp/ip連接,從位圖發送大小和數據並在服務器端重新構建它並保存或處理它任何你想要的方式。

你可以用「的getPixels」獲取Android位的RGBA數據:

http://developer.android.com/reference/android/graphics/Bitmap.html

您可以設置它取決於你把它太系統。如果您使用的是Windows系統,你可以用「setdibbits」

http://msdn.microsoft.com/en-us/library/dd162973%28v=vs.85%29.aspx

+0

我以前也想過這個。但我想知道當我發送圖像時應該考慮多少個問題。例如,通道的數量和像素的深度。我應該考慮多少個問題?此外,我在實踐中發現,函數getpixel()可能會變得效率低下,特別是當圖像的大小很大時。 – Timothy

+0

你當然可以處理16和32位圖像,它應該覆蓋大多數情況。您也可以簡單地將它們轉換爲32位或將它們轉換爲壓縮的jpeg文件。是的,「getpixel」非常慢,但是「getpixels」可以訪問整個圖像,而不僅僅是一個像素更快。您應該提供有關您實際想要執行的操作的更多信息:發送圖像的原因,圖像大小,傳輸的大小/速度限制等。 – HardCoder

+0

對不起,我誤以爲「getpixels」爲「getpixel」。但對於一些大的照片,由於內存限制,我未能使用「getpixels」方法。我現在正在考慮將一張大照片分成幾部分,併爲每個照片使用「getpixels」方法,但我還沒有嘗試過。 – Timothy

0

這裏是我用來發送參數也和圖像後設置!

public void send_data() throws IOException 
    { 


       HttpURLConnection connection = null; 
       DataOutputStream outputStream = null; 
       String lineEnd = "\r\n"; 
       String twoHyphens = "---"; 
       String boundary = "ABCADA"; 

       String urlServer = "http://yourwebsrvr.com"; 
       Log.w("DHA", urlServer); 
       URL url = null; 
       try { 
        url = new URL(urlServer); 
       } catch (MalformedURLException e1) { 

        Log.w("DHA", "PROTOCOL EXCEPTION"); 

        e1.printStackTrace(); 
       } 
       if (url != null) 
       { 
        Log.w("DHA", "Merge aici"); 
        try { 
         connection = (HttpURLConnection) url.openConnection(); 
        } catch (IOException e1) { 
         // TODO Auto-generated catch block 
         e1.printStackTrace(); 
        } 
        if (connection != null) 
        { 
         Log.w("DHA", "Si aici mere!"); 
         connection.setDoInput(true); 
         connection.setDoOutput(true); 
         connection.setUseCaches(false); 
         try { 
          connection.setRequestMethod("POST"); 
         } catch (ProtocolException e) { 

          Log.w("DHA", "PROTOCOL EXCEPTION"); 

          e.printStackTrace(); 
          return; 
         } 
         connection.setRequestProperty("Host", "yourhost.com"); 
         connection.setRequestProperty("Connection", "Keep-Alive"); 
         connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=ABCADA"); 
         try { 
          outputStream = new DataOutputStream(connection.getOutputStream()); 
         } catch (IOException e) { 

          Log.w("DHA", "PROTOCOL EXCEPTION"); 

          e.printStackTrace(); 

         } 
         try { 
          Log.w("DHA", "Val is + " + String.valueOf(bts.size())); 
          outputStream.writeBytes(twoHyphens + boundary + lineEnd); 
          outputStream.writeBytes("Content-Disposition: form-data; name=\"Lat\"" + lineEnd); 
          outputStream.writeBytes(lineEnd); 
          outputStream.writeBytes("0" + lineEnd); 

          outputStream.writeBytes(twoHyphens + boundary + lineEnd); 
          outputStream.writeBytes("Content-Disposition: form-data; name=\"IMEI\"" + lineEnd); 
          outputStream.writeBytes(lineEnd); 
          outputStream.writeBytes(getImei() + lineEnd); 

          outputStream.writeBytes(twoHyphens + boundary + lineEnd); 
          outputStream.writeBytes("Content-Disposition: form-data; name=\"Lon\"" + lineEnd); 
          outputStream.writeBytes(lineEnd); 
          outputStream.writeBytes("0" + lineEnd); 

          outputStream.writeBytes(twoHyphens + boundary + lineEnd); 
          outputStream.writeBytes("Content-Disposition: form-data; name=\"comment\"" + lineEnd); 
          outputStream.writeBytes(lineEnd); 
          outputStream.writeBytes("Incarcata la sincronizare" + lineEnd); 

          outputStream.writeBytes(twoHyphens + boundary + lineEnd); 
          outputStream.writeBytes("Content-Disposition: form-data; name=\"locatie_id\"" + lineEnd); 
          outputStream.writeBytes(lineEnd); 
          SharedPreferences pref = getSharedPreferences("data",MODE_WORLD_WRITEABLE); 
          String data_db = pref.getString(md5hash, "0"); 
          Log.d("DHA", "Poze e aici " + data_db); 
          outputStream.writeBytes(data_db + lineEnd); 

          outputStream.writeBytes(twoHyphens + boundary + lineEnd); 
          outputStream.writeBytes("Content-Disposition: form-data; name=\"hash\"" + lineEnd); 
          outputStream.writeBytes(lineEnd); 
          outputStream.writeBytes(md5hash + lineEnd); 

          outputStream.writeBytes(twoHyphens + boundary + lineEnd); 
          outputStream.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + "PICT0000" +"\"" + lineEnd); 
          outputStream.writeBytes(lineEnd); 
          outputStream.write(bts.toByteArray()); 
          outputStream.writeBytes(lineEnd); 
          outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); 
          Log.w("DHA", "Incep trimiterea pozei fraierului!"); 

          outputStream.flush(); 
          outputStream.close(); 
          Log.w("DHA", "response" + String.valueOf(connection.getResponseCode())); 

         } catch (IOException e) { 

          Log.w("DHA", "PROTOCOL EXCEPTION"); 

          e.printStackTrace(); 
         } 



      } 
       } 


      } 
+0

謝謝。但是,服務器應該如何從流中重建圖像? – Timothy

+0

它通過http發送後,您的web服務器需要知道如何處理http帖子,並用它做什麼腳本指示它做 – opc0de

+0

我明白了。當我開始編寫代碼時,我認爲套接字會更容易。看來我應該花更多的時間學習其他方法。非常感謝。 – Timothy

相關問題