2012-05-07 8 views
0

AndroidからC++で書かれたサーバに画像を送ろうと思います。ソケットを使用して画像を送信して、C++とJavaの違いについて心配する必要はありません。しかし、Androidの画像は通常、Androidで定義されたクラスであるBitmapとして保存されますが、C++ではそのクラスは存在しません。だから私はこのような方法で画像を送ろうとすれば、どうすればいいのだろう?だから私はあなたの助けのためにここに来て、ありがとう。AndroidからC++で書かれたサーバに画像を送るには

+0

この質問[リンク](http://stackoverflow.com/questions/9501550/how-can-i-make-an-android-app-communicate-with-a-web-server-overを参照してください-the-internet/9503995#9503995) –

+0

この質問への回答は、JPEGで画像を圧縮することに注意してください。3G上で生の画像をアップロードするのは良いことです... – Torp

+0

私は読んでいますリンク、ありがとうございます。しかし、私はまだPHPの関数move_uploaded_file()がCのprintf()と同じであるかどうか疑問に思っていますか? – Timothy

答えて

0

私は、サーバーを想定しています.JPGなどのファイルまたは

+0

イメージを.jpgの形式に圧縮し、バイト配列として送信しますか?あなたのアドバイスをありがとうございました。 – Timothy

0

を.pngの画像を送信しますが、そのC++で書かれた言及してカスタムシステムです。その場合は、サイズ(x、y)とrgbaデータ(1ピクセルあたり32ビット)をtcp/ipごとにソケットを通して送信するだけです。

クライアント(アンドロイド)からサーバー(C++)にtcp/ip接続を開き、ビットマップからサイズとデータを送信し、サーバー側で再構築して保存または処理するだけです。あなたが望むどんな方法でも。

あなたは「getPixels」とアンドロイドビットマップの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投稿を介して送信されます。ウェブサーバーはhttp投稿を処理する方法を知っていなければならず、スクリプトが指示するように指示します。 – opc0de

+0

私は、コード作成を始めたときにソケットがより簡単になると思った。私は他の方法を学ぶのにもっと時間を費やすべきだと思う。どうもありがとう。 – Timothy

関連する問題