我想将图片从 Android 发送到用 C++ 编写的服务器。我希望用socket来发送图片,这样就不用担心C++和Java的区别了。但是,Android中的图像通常存储为Bitmap,这是Android中定义的一个类,而在C++中,该类是不存在的。所以我想知道如果我想以这种方式发送图像该怎么办。所以我来这里寻求你的帮助,谢谢。
问问题
936 次
3 回答
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();
}
}
}
}
于 2012-05-07T11:58:43.557 回答
0
将图像作为 .jpg 或 .png 等文件发送
于 2012-05-07T11:33:27.693 回答
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
于 2012-05-07T11:38:44.350 回答