【问题标题】:Image size become too small after compression压缩后图像尺寸变得太小
【发布时间】: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


【解决方案1】:

代码 sn-p 看起来非常好。这可以将图像转换为base64。

我认为你可能有 3 个可能的问题。

  1. 转换时出现包装问题(您尝试过)

使用Base64.NO_WRAP 而不是Base64.DEFAULT

  1. 您的服务器代码在将 base64 转换为图像时可能会出现问题。

您可以控制或共享您的服务器端代码。

  1. 当函数工作时,您的位图已经太小了。

【讨论】:

  • 静止图像太小。
【解决方案2】:

您可以使用以下代码。我已经实现了相同的功能。我从图库中获取图像并将路径转换为字节数组。将图像路径传递给您的服务器。 selectedImage 是 Gallery 的 URI 路径。

    public static byte[] bitmapdata;
    private static String imagepath;


 Bitmap bitmap = BitmapFactory.decodeFile(selectedImage.toString());
                    ByteArrayOutputStream stream = new ByteArrayOutputStream();
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
                    bitmapdata = resizeImage(stream.toByteArray());

                    imagepath = Base64.encodeToString(bitmapdata, Base64.DEFAULT);

/* Resize Image */
        byte[] resizeImage(byte[] input) {
            Bitmap original = BitmapFactory.decodeByteArray(input, 0, input.length);
            Bitmap resized = Bitmap.createScaledBitmap(original, 400, 480, true);

            ByteArrayOutputStream blob = new ByteArrayOutputStream();
            resized.compress(Bitmap.CompressFormat.JPEG, 50, blob);

            return blob.toByteArray();
        }

【讨论】:

  • 如何将这张图片保存为原始尺寸?
  • 要保存图像,您必须在服务器端执行代码,然后他们会创建 imageurl。使用 imageurl 使用 Picaso 或 glide 库从服务器下载。
  • 不不,我的意思是你提到了 400,480 大小。我想保存原始图像。
  • 从 resizeImage 方法中删除以下行并传递原始的位图对象,而不是在 compress 方法中调整大小。位图调整大小 = Bitmap.createScaledBitmap(original, 400, 480, true);
  • 但这是我要上传的图像变得太小的实际问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-12-28
  • 2023-02-04
  • 1970-01-01
  • 2012-09-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多