【发布时间】:2018-11-09 07:07:00
【问题描述】:
我正在尝试通过转换为 Base64 字符串将图像发送到服务器,但压缩后图像在服务器端变得太小。
代码:
public String getEncoded64ImageStringFromBitmap(Bitmap bitmap) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteFormat = stream.toByteArray();
// get the base 64 string
String imgString = Base64.encodeToString(byteFormat, Base64.DEFAULT);
return imgString;
}
服务器端代码:
[AllowAnonymous]
[HttpPost]
public IHttpActionResult UploadFile()
{
string filePath = "No File Uploaded";
HttpRequest httpRequest = HttpContext.Current.Request;
var fileData = httpRequest.Form["fileData"];
try
{
string lUniqueId = DateTime.Now.ToString("ddMMyyyyHHmmssff");
string lFileName = lUniqueId + ".jpeg";
filePath = "~/Uploads/AadharCards/" + lFileName;
byte[] imageBytes = Convert.FromBase64String(fileData);
//Save the Byte Array as File.
File.WriteAllBytes(System.Web.Hosting.HostingEnvironment.MapPath(filePath), imageBytes);
return Ok(filePath);
}
catch (Exception)
{
return Ok(false);
}
}
【问题讨论】:
-
您是否尝试在编码后对图像进行解码?也许这是服务器问题,因为您的代码似乎正确?
-
trying to send image to server by converting to Base64 string, but after compression image becomes too small at server side.请告诉原图的分辨率。然后在客户端上调整大小的图像的分辨率。然后是服务器上的图像分辨率。 -
Image size become too small after compression我认为您的位图在压缩之前已经太小了。请告诉 bmp.getWidth() 和 bmp.getHeight()。你是从哪里得到那个位图的? -
原始图像 2448 x 3264 服务器图像 427 x 60
-
我要求三个分辨率。
标签: android image file-upload android-volley