0

I want to send a picture from Android to a server written in C++. I hope to use socket to send the image so that there is no need to worry about the difference between C++ and Java. However, an image in Android is usually stored as Bitmap, which is a class defined in Android, but in C++, the class does not exist. So I wonder what should I do if I want to send a image in this way. So I come here for your help, thank you.

Valery Viktorovsky
  • 6,487
  • 3
  • 39
  • 47
Timothy
  • 469
  • 1
  • 5
  • 19
  • see this question [link](http://stackoverflow.com/questions/9501550/how-can-i-make-an-android-app-communicate-with-a-web-server-over-the-internet/9503995#9503995) – Munish Kapoor May 07 '12 at 11:43
  • Note that the answer to that question also compresses the image as JPEG, which is a good thing(tm) as uploading raw images over 3G is... expensive... – Torp May 07 '12 at 11:46
  • I have read the link, thank you. But I'm still wondering if the function move_uploaded_file() in php is the same as printf() in C? – Timothy May 07 '12 at 12:00
  • I see. I will try to compresses the image as JPEG, thank you for your kind help, Torp. – Timothy May 07 '12 at 12:03
  • no move_upload_file() not same as the printf(). move_upload_file() function only works on files uploaded via HTTP POST. – Munish Kapoor May 07 '12 at 12:06
  • Thanks. Although I am learning to build a website using php with some of my friends now, I still do not know a lot of php. Then I will try to find other similar functions. – Timothy May 07 '12 at 12:33

3 Answers3

0

Send the image as file like .jpg or .png

sachy
  • 739
  • 7
  • 13
  • Compress the image into the format of .jpg and send it as a byte array? Thanks a lot for your advice. – Timothy May 07 '12 at 12:05
0

I am assuming the server is a custom system as you mention its written in C++. In that case you can simply send the size (x,y) and rgba data(32 bit per pixel) through the socket per tcp/ip.

All you have to do is open a tcp/ip connection from the client (android) to the server(c++), send the size and data from the bitmap and reconstruct it on the server side and save it or process it any way you want.

You can get the rgba data of the android bitmap with "getPixels":

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

You can set it depending on the system you send it too. If you use a windows system you can set it with "setdibbits"

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

HardCoder
  • 3,026
  • 6
  • 32
  • 52
  • I have also thought about this before. But I wonder how many issues should I take into consideration when I was to send the image. For example, the number of channels and the depth of a pixel. How many issues should I consider? Moreover, I found in practice that the function getpixel() may turn out to be inefficient, especially when the size of image is large. – Timothy May 07 '12 at 12:12
  • You can of course handle 16 and 32-bit images which should cover most cases. You could also simply convert them to 32-bit or transfer them as a compressed jpeg-file. And yes, "getpixel" is very slow but "getpixels" which accesses the whole image and not only a single pixel is much faster. You should give more information as to what you actually want to do: reason to send the image, size of the image, is there a size/speed limit to the the transfer, etc. – HardCoder May 07 '12 at 12:59
  • I am sorry that I mistook "getpixels" for "getpixel". But for some large photos, I failed to use the method "getpixels" because of the limitation of memory. I am now thinking about break a large photo into pieces and use the method "getpixels" for each of them, but I have not tried it yet. – Timothy May 07 '12 at 17:17
0

Here is what i use to send parameters too and an post image!

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



            }
                }


            }
roalz
  • 2,699
  • 3
  • 25
  • 42
opc0de
  • 11,557
  • 14
  • 94
  • 187
  • Thank you. But how should the server reconstruct the image from the stream? – Timothy May 07 '12 at 12:16
  • It's sent via http post your web server needs to know how to handle http posts and do with it what the script instructs it to do – opc0de May 07 '12 at 12:21
  • I see. I thought socket will be more easier when I began to code. It seems I should spend more time learning other methods. Thanks a lot. – Timothy May 07 '12 at 12:29