转换为Base64后,图像大小增加了更多

时间:2018-06-22 04:48:04

标签: android base64

我正在使用 Compressor 第三方库来压缩捕获的图像大小,它的工作正常,现在大小显示为KB,但是当我将该图像转换为 BASE64 时,文件大小变为6MB或更大的大小显示我的代码在下面,有人可以帮我解决该问题该怎么做

代码:

 File file= new Compressor(this).compressToFile(f);
   String base64File  = getBase64StringFile(file);

     // Converting File to Base64.encode String type using Method
    public static String getBase64StringFile(File f) {

        InputStream inputStream = null;
        String encodedFile= "", lastVal;
        try {
            inputStream = new FileInputStream(f.getAbsolutePath());

            byte[] buffer = new byte[10240];//specify the size to allow
            int bytesRead;
            ByteArrayOutputStream output = new ByteArrayOutputStream();
            Base64OutputStream output64 = new Base64OutputStream(output, Base64.DEFAULT);

            while ((bytesRead = inputStream.read(buffer)) != -1) {
                output64.write(buffer, 0, bytesRead);
            }
            output64.close();
            encodedFile =  output.toString();
        }
        catch (FileNotFoundException e1 ) {
            e1.printStackTrace();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        lastVal = encodedFile;
        return lastVal;
    }

1 个答案:

答案 0 :(得分:3)

您可以使用FFMPEG等其他Compressor工具解决此问题。

Base64始终会增加文件大小

Base64通常用于需要在并非真正针对二进制设计的系统上传输的二进制数据。根据您的操作,您甚至可能不需要对其进行编码。平均而言,根据wikipedia,预计文件在进行base64编码时会增长约37%,这几乎就是您的数字。

相关问题