简体   繁体   English

如何使用gzip-android将图像转换为base64字符串

[英]How to convert an image to a base64 string with gzip- android

I am trying to convert and compress an image taken from a filepath on android to be converted with base64's gzip (i am using this because my desktop version, written in java, is doing the same). 我试图转换和压缩从android上的文件路径获取的图像 ,用base64的gzip转换(我正在使用这个,因为我的桌面版本,用java编写,也是这样做的)。 Here is what I have currently for compression: 这是我目前用于压缩的内容:

Bitmap bm = BitmapFactory.decodeFile(imagePath);              
ByteArrayOutputStream baos = new ByteArrayOutputStream();     
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos);           
byte[] data = baos.toByteArray();                                                               
String base64Str = null;                                      

ByteArrayOutputStream out_bytes = new ByteArrayOutputStream();
OutputStream out = new Base64.OutputStream(out_bytes);

try {
    out.write(data);
    out.close();                                                         
    byte[] encoded = out_bytes.toByteArray();                 

    base64Str = Base64.encodeBytes(encoded, Base64.GZIP);     
    baos.close();                                             
} catch (Exception e) {}

This is what your code currently does: 这是您的代码目前所做的:

//1. Decode data from image file
Bitmap bm = BitmapFactory.decodeFile(imagePath);
...
//2. Compress decoded image data to JPEG format with max quality
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos);
...
//3. Encode compressed image data to base64
out.write(data);
...
//4. Compress to gzip format, before encoding gzipped data to base64
base64Str = Base64.encodeBytes(encoded, Base64.GZIP);

I don't know how your desktop version does it, but step 3 is unnecessary since you are doing the same thing as part of step 4. 我不知道您的桌面版本是如何做到的,但是步骤3是不必要的,因为您正在执行与步骤4相同的操作。

(Removed part of answer) (删除部分答案)

EDIT: The following code will read the bytes from the file, gzip the bytes and encode them to base64. 编辑:以下代码将从文件中读取字节,gzip字节并将它们编码为base64。 It works on all readable files smaller than 2 GB. 它适用于小于2 GB的所有可读文件。 The bytes passed in to Base64.encodeBytes will be the same bytes as in the file, so no information is lost (as opposed to the code above, where you convert the data to JPEG format first). 传入Base64.encodeBytes的字节将与文件中的字节相同,因此不会丢失任何信息(与上面的代码相反,首先将数据转换为JPEG格式)。

/*
 * imagePath has changed name to path, as the file doesn't have to be an image.
 */
File file = new File(path);
long length = file.length();
BufferedInputStream bis = null;
try {
    bis = new BufferedInputStream(new FileInputStream(file));
    if(length > Integer.MAX_VALUE) {
        throw new IOException("File must be smaller than 2 GB.");
    }
    byte[] data = new byte[(int)length];
    //Read bytes from file
    bis.read(data);
} catch (IOException e) {
    e.printStackTrace();
} finally {
    if(bis != null)
        try { bis.close(); }
        catch(IOException e) {}
}
//Gzip and encode to base64
String base64Str = Base64.encodeBytes(data, Base64.GZIP);

EDIT2: This should decode the base64 String and write the decoded data to a file: EDIT2:这应解码base64 String并将解码后的数据写入文件:

    //outputPath is the path to the destination file.

    //Decode base64 String (automatically detects and decompresses gzip)
    byte[] data = Base64.decode(base64str);
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(outputPath);
        //Write data to file
        fos.write(data);
    } catch(IOException e) {
        e.printStackTrace();
    } finally {
        if(fos != null)
            try { fos.close(); }
            catch(IOException e) {}
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM