Android 将base64编码的映像发送到服务器
任务 通过使用HTTP POST请求发送具有多部分表单编码或base64编码数据参数的端点/图像 我正在使用Cloudsight API进行图像识别项目。参考文献 问题 我需要从gallery发送一张编码为Base64格式的图像,但我收到服务器错误500。我似乎在我的代码中找不到问题可能图像编码不正确 代码Android 将base64编码的映像发送到服务器,android,base64,http-post,httprequest,cloudsight,Android,Base64,Http Post,Httprequest,Cloudsight,任务 通过使用HTTP POST请求发送具有多部分表单编码或base64编码数据参数的端点/图像 我正在使用Cloudsight API进行图像识别项目。参考文献 问题 我需要从gallery发送一张编码为Base64格式的图像,但我收到服务器错误500。我似乎在我的代码中找不到问题可能图像编码不正确 代码 private String uploadData(String url) { String sResponse = ""; try {
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;
}
}
私有字符串上传数据(字符串url){
字符串sResponse=“”;
试一试{
HttpClient HttpClient=新的DefaultHttpClient();
HttpContext localContext=新的BasicHttpContext();
HttpPost-HttpPost=新的HttpPost(url+“/images”);
addHeader(“内容类型”、“应用程序/json”);
httpPost.addHeader(“授权”、“CloudSight API_密钥”);
ByteArrayOutputStream bos=新建ByteArrayOutputStream();
bitmapImage.compress(位图.CompressFormat.JPEG,100,bos);
字节[]数据=bos.toByteArray();
字符串encImage=Base64.encodeToString(数据,Base64.DEFAULT);
JSONObject obj=新的JSONObject();
obj.put(“远程图像url”,encImage);
obj.put(“区域设置”、“en”);
setEntity(新的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(“代码”,响应代码+”);
}捕获(例外情况除外){
例如printStackTrace();
}
返回响应;
}
私有类uploadImage1扩展异步任务{
ProgressBar ProgressBar=新的ProgressBar(getApplication(),null,android.R.attr.progressBarStyleSmall);
受保护的void onPreExecute(){
progressBar=(progressBar)findViewById(R.id.progressBar);
progressBar.setVisibility(View.VISIBLE);
super.onPreExecute();
}
@凌驾
受保护的字符串doInBackground(字符串…参数){
字符串url=params[0];
String sResponse=上传数据(url);
返回响应;
}
}
编辑
“注意:我们建议图像分辨率不高于1024x,JPEG压缩级别在5-8之间。否则,我们会在内部调整图像大小,这可能会减慢请求过程。” 所以我需要在发送之前调整图像的大小?你能分享一些例子吗?主要活动
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";
}
服务器500出现内部服务器错误。请向服务器团队询问分辨率。“注意:我们建议图像分辨率不高于1024x,JPEG压缩级别在5-8之间。否则,我们会在内部调整图像大小,这可能会减慢请求过程。”因此,我需要在发送图像之前调整图像大小?你能分享一些例子吗?欢迎来到SO。1) 把更多的精力放在格式化代码上2)为什么要使用代码段?3) 这是作业吗?下一次请正确格式化,以便我们了解源任务是什么以及您提出的问题是什么4)提出一个特定的问题,而不是多个随机和广泛的问题-没有人会与您分享完整解决方案的示例,这不是本网站的目的。5) 请在发帖前仔细阅读:您好,谢谢您的建议。很抱歉问题结构不好,我更新了我正在使用的API的引用HI-
remote\u image\u url
用于web上图像的url。如果您自己以base64编码方式发送图像,那么您需要的参数就是image
;我真的看不出你在哪里调整了图像的大小?我仍然收到错误500,可能图像没有按应有的方式编码