如何将图像转换为一个base64字符串,比如gzip压缩还是用安卓 [英] How to convert an image to a base64 string with gzip- android

查看:460
本文介绍了如何将图像转换为一个base64字符串,比如gzip压缩还是用安卓的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想转换和COM preSS的图片取自Android的文件路径来使用Base64的gzip的转换(我用这个,因为我的桌面版本,用Java编写的,是做相同的)。这是我目前的COM pression:

 位图BM = BitmapFactory.de codeFILE(的ImagePath);
ByteArrayOutputStream BAOS =新ByteArrayOutputStream();
bm.com preSS(Bitmap.Com pressFormat.JPEG,100,BAOS);
byte []的数据= baos.toByteArray();
字符串base64Str = NULL;

ByteArrayOutputStream out_bytes =新ByteArrayOutputStream();
的OutputStream OUT =新Base64.OutputStream(out_bytes);

尝试 {
    out.write(数据);
    out.close();
    byte []的EN codeD = out_bytes.toByteArray();

    base64Str = Base64.en codeBytes(EN codeD,Base64.GZIP);
    baos.close();
}赶上(例外五){}
 

解决方案

这是你的code目前的工作:

  // 1。从图像文件德code数据
位图BM = BitmapFactory.de codeFILE(的ImagePath);
...
// 2。 COM preSS德codeD图像数据为JPEG格式,最大质量
bm.com preSS(Bitmap.Com pressFormat.JPEG,100,BAOS);
...
// 3。恩code COM pressed图像数据为base64
out.write(数据);
...
// 4。 COM preSS为gzip格式,编码前的数据gzip压缩为base64
base64Str = Base64.en codeBytes(EN codeD,Base64.GZIP);
 

我不知道你的桌面版本做的,但第3步是不必要的,因为你正在做同样的事情,第4步中的一部分。

(答案删除部分)

编辑:下面code会从文件中读取的字节数,gzip压缩字节和连接code他们为base64。它适用于所有可读的文件小于2 GB。以 Base64.en codeBytes 传入的字节将是相同字节的文件,所以没有信息丢失(相对于code以上,您第一次将数据转换为JPEG格式)。

  / *
 *的ImagePath已更名为路径,作为文件不必是图像。
 * /
档案文件=新的文件(路径);
长长度= file.length();
的BufferedInputStream双= NULL;
尝试 {
    双=新的BufferedInputStream(新的FileInputStream(文件));
    如果(长度>为Integer.MAX_VALUE){
        抛出新的IOException异常(文件必须小于2 GB。);
    }
    byte []的数据=新的字节[(INT)长]。
    //从文件读取字节
    bis.read(数据);
}赶上(IOException异常E){
    e.printStackTrace();
} 最后 {
    如果(之二!= NULL)
        尝试{bis.close(); }
        赶上(IOException异常E){}
}
// gzip和EN code为base64
字符串base64Str = Base64.en codeBytes(数据,Base64.GZIP);
 

EDIT2:这个应该去$ C C中的Base64 $ 字符串,写去codeD数据到一个文件:

  // outputPath是路径目标文件。

    //德code的base64字符串(自动检测并DECOM presses GZIP)
    byte []的数据= Base64.de code(base64str);
    FileOutputStream中FOS = NULL;
    尝试 {
        FOS =新的FileOutputStream(outputPath);
        //写数据到文件
        fos.write(数据);
    }赶上(IOException异常E){
        e.printStackTrace();
    } 最后 {
        如果(FOS!= NULL)
            尝试{fos.close(); }
            赶上(IOException异常E){}
    }
 

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). 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.

(Removed part of answer)

EDIT: The following code will read the bytes from the file, gzip the bytes and encode them to base64. It works on all readable files smaller than 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).

/*
 * 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:

    //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) {}
    }

这篇关于如何将图像转换为一个base64字符串,比如gzip压缩还是用安卓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆