Android图片压缩(质量压缩和尺寸压缩)
2017-05-12 14:11
218 查看
Android中图片是以bitmap形式存在的,那么bitmap所占内存,直接影响到了应用所占内存大小,首先要知道bitmap所占内存大小计算方式:
图片长度 x 图片宽度 x 一个像素点占用的字节数
以下是图片的压缩格式:
其中,A代表透明度;R代表红色;G代表绿色;B代表蓝色。
ALPHA_8
表示8位Alpha位图,即A=8,一个像素点占用1个字节,它没有颜色,只有透明度
ARGB_4444
表示16位ARGB位图,即A=4,R=4,G=4,B=4,一个像素点占4+4+4+4=16位,2个字节
ARGB_8888
表示32位ARGB位图,即A=8,R=8,G=8,B=8,一个像素点占8+8+8+8=32位,4个字节
RGB_565
表示16位RGB位图,即R=5,G=6,B=5,它没有透明度,一个像素点占5+6+5=16位,2个字节
我是用的小米手机2s来测试的,从sd卡取出一个照片,如下所示:
bit = BitmapFactory.decodeFile(Environment .getExternalStorageDirectory().getAbsolutePath() + "/DCIM/Camera/test.jpg"); Log.i("wechat", "压缩前图片的大小" + (bit.getByteCount() / 1024 / 1024) + "M宽度为" + bit.getWidth() + "高度为" + bit.getHeight());1
2
3
4
5
6
1
2
3
4
5
6
[/code]
出来的log是:
将取得的bitmap进行压缩,下面开始说,bitmap的几种压缩方式。
1.质量压缩
ByteArrayOutputStream baos = new ByteArrayOutputStream(); int quality = Integer.valueOf(editText.getText().toString()); bit.compress(CompressFormat.JPEG, quality, baos); byte[] bytes = baos.toByteArray(); bm = BitmapFactory.decodeByteArray(bytes, 0, bytes.length); Log.i("wechat", "压缩后图片的大小" + (bm.getByteCount() / 1024 / 1024) + "M宽度为" + bm.getWidth() + "高度为" + bm.getHeight() + "bytes.length= " + (bytes.length / 1024) + "KB" + "quality=" + quality);1
2
3
4
5
6
7
8
9
1
2
3
4
5
6
7
8
9
[/code]
其中quality是从edittext获取的数字,可以从0–100改变,这里出来的log是:
可以看到,图片的大小是没有变的,因为质量压缩不会减少图片的像素,它是在保持像素的前提下改变图片的位深及透明度等,来达到压缩图片的目的,这也是为什么该方法叫质量压缩方法。那么,图片的长,宽,像素都不变,那么bitmap所占内存大小是不会变的。
但是我们看到bytes.length是随着quality变小而变小的。这样适合去传递二进制的图片数据,比如微信分享图片,要传入二进制数据过去,限制32kb之内。
这里要说,如果是bit.compress(CompressFormat.PNG, quality, baos);这样的png格式,quality就没有作用了,bytes.length不会变化,因为png图片是无损的,不能进行压缩。
CompressFormat还有一个属性是,CompressFormat.WEBP格式,该格式是google自己推出来一个图片格式,更多信息,文末会贴出地址。
2.采样率压缩
BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = 2; bm = BitmapFactory.decodeFile(Environment .getExternalStorageDirectory().getAbsolutePath() + "/DCIM/Camera/test.jpg", options); Log.i("wechat", "压缩后图片的大小" + (bm.getByteCount() / 1024 / 1024) + "M宽度为" + bm.getWidth() + "高度为" + bm.getHeight());1
2
3
4
5
6
7
8
1
2
3
4
5
6
7
8
[/code]
出来的log是
设置inSampleSize的值(int类型)后,假如设为2,则宽和高都为原来的1/2,宽高都减少了,自然内存也降低了。
我上面的代码没用过options.inJustDecodeBounds = true; 因为我是固定来取样的数据,为什么这个压缩方法叫采样率压缩,是因为配合inJustDecodeBounds,先获取图片的宽、高【这个过程就是取样】,然后通过获取的宽高,动态的设置inSampleSize的值。
当inJustDecodeBounds设置为true的时候,BitmapFactory通过decodeResource或者decodeFile解码图片时,将会返回空(null)的Bitmap对象,这样可以避免Bitmap的内存分配,但是它可以返回Bitmap的宽度、高度以及MimeType。
3.缩放法压缩(martix)
Matrix matrix = new Matrix(); matrix.setScale(0.5f, 0.5f); bm = Bitmap.createBitmap(bit, 0, 0, bit.getWidth(), bit.getHeight(), matrix, true); Log.i("wechat", "压缩后图片的大小" + (bm.getByteCount() / 1024 / 1024) + "M宽度为" + bm.getWidth() + "高度为" + bm.getHeight());1
2
3
4
5
6
1
2
3
4
5
6
[/code]
出来的log是
可以看出来,bitmap的长度和宽度分别缩小了一半,图片大小缩小了四分之一。
关于martix更多信息,文末会有一个参考文章。
4.RGB_565法
BitmapFactory.Options options2 = new BitmapFactory.Options(); options2.inPreferredConfig = Bitmap.Config.RGB_565; bm = BitmapFactory.decodeFile(Environment .getExternalStorageDirectory().getAbsolutePath() + "/DCIM/Camera/test.jpg", options2); Log.i("wechat", "压缩后图片的大小" + (bm.getByteCount() / 1024 / 1024) + "M宽度为" + bm.getWidth() + "高度为" + bm.getHeight());1
2
3
4
5
6
7
8
1
2
3
4
5
6
7
8
[/code]
出来的log是:
我们看到图片大小直接缩小了一半,长度和宽度也没有变,相比argb_8888减少了一半的内存。
注意:由于ARGB_4444的画质惨不忍睹,一般假如对图片没有透明度要求的话,可以改成RGB_565,相比ARGB_8888将节省一半的内存开销。
5.createScaledBitmap
bm = Bitmap.createScaledBitmap(bit, 150, 150, true); Log.i("wechat", "压缩后图片的大小" + (bm.getByteCount() / 1024) + "KB宽度为" + bm.getWidth() + "高度为" + bm.getHeight());1
2
3
1
2
3
[/code]
出来的log是
这里是将图片压缩成用户所期望的长度和宽度,但是这里要说,如果用户期望的长度和宽度和原图长度宽度相差太多的话,图片会很不清晰。
总结
以上就是5种图片压缩的方法,这里需要强调,他们的压缩仅仅只是对android中的bitmap来说的。如果将这些压缩后的bitmap另存为sd中,他们的内存大小并不一样。android手机中,图片的所占的内存大小和很多因素相关,计算起来也很麻烦。为了计算出一个图片的内存大小,可以将图片当做一个文件来间接计算,用如下的方法:
File file = new File(Environment.getExternalStorageDirectory() .getAbsolutePath() + "/DCIM/Camera/test.jpg"); Log.i("wechat", "file.length()=" + file.length() / 1024);1
2
3
1
2
3
[/code]
或者
FileInputStream fis = null; try { fis = new FileInputStream(file); } catch (FileNotFoundException e) { e.printStackTrace(); } try { Log.i("wechat", "fis.available()=" + fis.available() / 1024); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }1
2
3
4
5
6
7
8
9
10
11
12
1
2
3
4
5
6
7
8
9
10
11
12
[/code]
上面两个方法计算的结果是一样的。
看完了这篇内容,其实说白了,Bitmap压缩都是围绕这个来做文章:Bitmap所占用的内存 = 图片长度 x 图片宽度 x 一个像素点占用的字节数。3个参数,任意减少一个的值,就达到了压缩的效果。
----------------------------------------------------------------------------------------------------------------------------------------------------
在网上调查了图片压缩的方法并实装后,大致上可以认为有两类压缩:质量压缩(不改变图片的尺寸)和尺寸压缩(相当于是像素上的压缩);质量压缩一般可用于上传大图前的处理,这样就可以节省一定的流量,毕竟现在的手机拍照都能达到3M左右了,尺寸压缩一般可用于生成缩略图。
两种方法都实装在了我的项目中,结果却发现在质量压缩的模块中,本来1.9M的图片压缩后反而变成3M多了,很是奇怪,再做了进一步调查终于知道原因了。下面这个博客说的比较清晰:
android图片压缩总结
总结来看,图片有三种存在形式:硬盘上时是file,网络传输时是stream,内存中是stream或bitmap,所谓的质量压缩,它其实只能实现对file的影响,你可以把一个file转成bitmap再转成file,或者直接将一个bitmap转成file时,这个最终的file是被压缩过的,但是中间的bitmap并没有被压缩(或者说几乎没有被压缩,我不确定),因为bigmap在内存中的大小是按像素计算的,也就是width * height,对于质量压缩,并不会改变图片的像素,所以就算质量被压缩了,但是bitmap在内存的占有率还是没变小,但你做成file时,它确实变小了;
而尺寸压缩由于是减小了图片的像素,所以它直接对bitmap产生了影响,当然最终的file也是相对的变小了;
最后把自己总结的工具类贴出来:
[java] view
plain copy
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
/**
* Image compress factory class
*
* @author
*
*/
public class ImageFactory {
/**
* Get bitmap from specified image path
*
* @param imgPath
* @return
*/
public Bitmap getBitmap(String imgPath) {
// Get bitmap through image path
BitmapFactory.Options newOpts = new BitmapFactory.Options();
newOpts.inJustDecodeBounds = false;
newOpts.inPurgeable = true;
newOpts.inInputShareable = true;
// Do not compress
newOpts.inSampleSize = 1;
newOpts.inPreferredConfig = Config.RGB_565;
return BitmapFactory.decodeFile(imgPath, newOpts);
}
/**
* Store bitmap into specified image path
*
* @param bitmap
* @param outPath
* @throws FileNotFoundException
*/
public void storeImage(Bitmap bitmap, String outPath) throws FileNotFoundException {
FileOutputStream os = new FileOutputStream(outPath);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, os);
}
/**
* Compress image by pixel, this will modify image width/height.
* Used to get thumbnail
*
* @param imgPath image path
* @param pixelW target pixel of width
* @param pixelH target pixel of height
* @return
*/
public Bitmap ratio(String imgPath, float pixelW, float pixelH) {
BitmapFactory.Options newOpts = new BitmapFactory.Options();
// 开始读入图片,此时把options.inJustDecodeBounds 设回true,即只读边不读内容
newOpts.inJustDecodeBounds = true;
newOpts.inPreferredConfig = Config.RGB_565;
// Get bitmap info, but notice that bitmap is null now
Bitmap bitmap = BitmapFactory.decodeFile(imgPath,newOpts);
newOpts.inJustDecodeBounds = false;
int w = newOpts.outWidth;
int h = newOpts.outHeight;
// 想要缩放的目标尺寸
float hh = pixelH;// 设置高度为240f时,可以明显看到图片缩小了
float ww = pixelW;// 设置宽度为120f,可以明显看到图片缩小了
// 缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可
int be = 1;//be=1表示不缩放
if (w > h && w > ww) {//如果宽度大的话根据宽度固定大小缩放
be = (int) (newOpts.outWidth / ww);
} else if (w < h && h > hh) {//如果高度高的话根据宽度固定大小缩放
be = (int) (newOpts.outHeight / hh);
}
if (be <= 0) be = 1;
newOpts.inSampleSize = be;//设置缩放比例
// 开始压缩图片,注意此时已经把options.inJustDecodeBounds 设回false了
bitmap = BitmapFactory.decodeFile(imgPath, newOpts);
// 压缩好比例大小后再进行质量压缩
// return compress(bitmap, maxSize); // 这里再进行质量压缩的意义不大,反而耗资源,删除
return bitmap;
}
/**
* Compress image by size, this will modify image width/height.
* Used to get thumbnail
*
* @param image
* @param pixelW target pixel of width
* @param pixelH target pixel of height
* @return
*/
public Bitmap ratio(Bitmap image, float pixelW, float pixelH) {
ByteArrayOutputStream os = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, os);
if( os.toByteArray().length / 1024>1024) {//判断如果图片大于1M,进行压缩避免在生成图片(BitmapFactory.decodeStream)时溢出
os.reset();//重置baos即清空baos
image.compress(Bitmap.CompressFormat.JPEG, 50, os);//这里压缩50%,把压缩后的数据存放到baos中
}
ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
BitmapFactory.Options newOpts = new BitmapFactory.Options();
//开始读入图片,此时把options.inJustDecodeBounds 设回true了
newOpts.inJustDecodeBounds = true;
newOpts.inPreferredConfig = Config.RGB_565;
Bitmap bitmap = BitmapFactory.decodeStream(is, null, newOpts);
newOpts.inJustDecodeBounds = false;
int w = newOpts.outWidth;
int h = newOpts.outHeight;
float hh = pixelH;// 设置高度为240f时,可以明显看到图片缩小了
float ww = pixelW;// 设置宽度为120f,可以明显看到图片缩小了
//缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可
int be = 1;//be=1表示不缩放
if (w > h && w > ww) {//如果宽度大的话根据宽度固定大小缩放
be = (int) (newOpts.outWidth / ww);
} else if (w < h && h > hh) {//如果高度高的话根据宽度固定大小缩放
be = (int) (newOpts.outHeight / hh);
}
if (be <= 0) be = 1;
newOpts.inSampleSize = be;//设置缩放比例
//重新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了
is = new ByteArrayInputStream(os.toByteArray());
bitmap = BitmapFactory.decodeStream(is, null, newOpts);
//压缩好比例大小后再进行质量压缩
// return compress(bitmap, maxSize); // 这里再进行质量压缩的意义不大,反而耗资源,删除
return bitmap;
}
/**
* Compress by quality, and generate image to the path specified
*
* @param image
* @param outPath
* @param maxSize target will be compressed to be smaller than this size.(kb)
* @throws IOException
*/
public void compressAndGenImage(Bitmap image, String outPath, int maxSize) throws IOException {
ByteArrayOutputStream os = new ByteArrayOutputStream();
// scale
int options = 100;
// Store the bitmap into output stream(no compress)
image.compress(Bitmap.CompressFormat.JPEG, options, os);
// Compress by loop
while ( os.toByteArray().length / 1024 > maxSize) {
// Clean up os
os.reset();
// interval 10
options -= 10;
image.compress(Bitmap.CompressFormat.JPEG, options, os);
}
// Generate compressed image file
FileOutputStream fos = new FileOutputStream(outPath);
fos.write(os.toByteArray());
fos.flush();
fos.close();
}
/**
* Compress by quality, and generate image to the path specified
*
* @param imgPath
* @param outPath
* @param maxSize target will be compressed to be smaller than this size.(kb)
* @param needsDelete Whether delete original file after compress
* @throws IOException
*/
public void compressAndGenImage(String imgPath, String outPath, int maxSize, boolean needsDelete) throws IOException {
compressAndGenImage(getBitmap(imgPath), outPath, maxSize);
// Delete original file
if (needsDelete) {
File file = new File (imgPath);
if (file.exists()) {
file.delete();
}
}
}
/**
* Ratio and generate thumb to the path specified
*
* @param image
* @param outPath
* @param pixelW target pixel of width
* @param pixelH target pixel of height
* @throws FileNotFoundException
*/
public void ratioAndGenThumb(Bitmap image, String outPath, float pixelW, float pixelH) throws FileNotFoundException {
Bitmap bitmap = ratio(image, pixelW, pixelH);
storeImage( bitmap, outPath);
}
/**
* Ratio and generate thumb to the path specified
*
* @param image
* @param outPath
* @param pixelW target pixel of width
* @param pixelH target pixel of height
* @param needsDelete Whether delete original file after compress
* @throws FileNotFoundException
*/
public void ratioAndGenThumb(String imgPath, String outPath, float pixelW, float pixelH, boolean needsDelete) throws FileNotFoundException {
Bitmap bitmap = ratio(imgPath, pixelW, pixelH);
storeImage( bitmap, outPath);
// Delete original file
if (needsDelete) {
File file = new File (imgPath);
if (file.exists()) {
file.delete();
}
}
}
}
如果上面的工具类不满足你,那么看看下面的方法。
一、图片质量压缩
[java] view
plain copy
/**
* 质量压缩方法
*
* @param image
* @return
*/
public static Bitmap compressImage(Bitmap image) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, baos);// 质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中
int options = 90;
while (baos.toByteArray().length / 1024 > 100) { // 循环判断如果压缩后图片是否大于100kb,大于继续压缩
baos.reset(); // 重置baos即清空baos
image.compress(Bitmap.CompressFormat.JPEG, options, baos);// 这里压缩options%,把压缩后的数据存放到baos中
options -= 10;// 每次都减少10
}
ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());// 把压缩后的数据baos存放到ByteArrayInputStream中
Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);// 把ByteArrayInputStream数据生成图片
return bitmap;
}
二、按比例大小压缩 (路径获取图片)
[java] view
plain copy
/**
* 图片按比例大小压缩方法
*
* @param srcPath (根据路径获取图片并压缩)
* @return
*/
public static Bitmap getimage(String srcPath) {
BitmapFactory.Options newOpts = new BitmapFactory.Options();
// 开始读入图片,此时把options.inJustDecodeBounds 设回true了
newOpts.inJustDecodeBounds = true;
Bitmap bitmap = BitmapFactory.decodeFile(srcPath, newOpts);// 此时返回bm为空
newOpts.inJustDecodeBounds = false;
int w = newOpts.outWidth;
int h = newOpts.outHeight;
// 现在主流手机比较多是800*480分辨率,所以高和宽我们设置为
float hh = 800f;// 这里设置高度为800f
float ww = 480f;// 这里设置宽度为480f
// 缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可
int be = 1;// be=1表示不缩放
if (w > h && w > ww) {// 如果宽度大的话根据宽度固定大小缩放
be = (int) (newOpts.outWidth / ww);
} else if (w < h && h > hh) {// 如果高度高的话根据宽度固定大小缩放
be = (int) (newOpts.outHeight / hh);
}
if (be <= 0)
be = 1;
newOpts.inSampleSize = be;// 设置缩放比例
// 重新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了
bitmap = BitmapFactory.decodeFile(srcPath, newOpts);
return compressImage(bitmap);// 压缩好比例大小后再进行质量压缩
}
三、按比例大小压缩 (Bitmap)
[java] view
plain copy
/**
* 图片按比例大小压缩方法
*
* @param image (根据Bitmap图片压缩)
* @return
*/
public static Bitmap compressScale(Bitmap image) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, baos);
// 判断如果图片大于1M,进行压缩避免在生成图片(BitmapFactory.decodeStream)时溢出
if (baos.toByteArray().length / 1024 > 1024) {
baos.reset();// 重置baos即清空baos
image.compress(Bitmap.CompressFormat.JPEG, 80, baos);// 这里压缩50%,把压缩后的数据存放到baos中
}
ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());
BitmapFactory.Options newOpts = new BitmapFactory.Options();
// 开始读入图片,此时把options.inJustDecodeBounds 设回true了
newOpts.inJustDecodeBounds = true;
Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, newOpts);
newOpts.inJustDecodeBounds = false;
int w = newOpts.outWidth;
int h = newOpts.outHeight;
Log.i(TAG, w + "---------------" + h);
// 现在主流手机比较多是800*480分辨率,所以高和宽我们设置为
// float hh = 800f;// 这里设置高度为800f
// float ww = 480f;// 这里设置宽度为480f
float hh = 512f;
float ww = 512f;
// 缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可
int be = 1;// be=1表示不缩放
if (w > h && w > ww) {// 如果宽度大的话根据宽度固定大小缩放
be = (int) (newOpts.outWidth / ww);
} else if (w < h && h > hh) { // 如果高度高的话根据高度固定大小缩放
be = (int) (newOpts.outHeight / hh);
}
if (be <= 0)
be = 1;
newOpts.inSampleSize = be; // 设置缩放比例
// newOpts.inPreferredConfig = Config.RGB_565;//降低图片从ARGB888到RGB565
// 重新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了
isBm = new ByteArrayInputStream(baos.toByteArray());
bitmap = BitmapFactory.decodeStream(isBm, null, newOpts);
return compressImage(bitmap);// 压缩好比例大小后再进行质量压缩
//return bitmap;
}
--------------------------------------------------------------------------------------------------------------------------------
分享个按照图片尺寸压缩:
[java] view
plain copy
public static void compressPicture(String srcPath, String desPath) {
FileOutputStream fos = null;
BitmapFactory.Options op = new BitmapFactory.Options();
// 开始读入图片,此时把options.inJustDecodeBounds 设回true了
op.inJustDecodeBounds = true;
Bitmap bitmap = BitmapFactory.decodeFile(srcPath, op);
op.inJustDecodeBounds = false;
// 缩放图片的尺寸
float w = op.outWidth;
float h = op.outHeight;
float hh = 1024f;//
float ww = 1024f;//
// 最长宽度或高度1024
float be = 1.0f;
if (w > h && w > ww) {
be = (float) (w / ww);
} else if (w < h && h > hh) {
be = (float) (h / hh);
}
if (be <= 0) {
be = 1.0f;
}
op.inSampleSize = (int) be;// 设置缩放比例,这个数字越大,图片大小越小.
// 重新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了
bitmap = BitmapFactory.decodeFile(srcPath, op);
int desWidth = (int) (w / be);
int desHeight = (int) (h / be);
bitmap = Bitmap.createScaledBitmap(bitmap, desWidth, desHeight, true);
try {
fos = new FileOutputStream(desPath);
if (bitmap != null) {
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
需要注意两个问题:
一、调用getDrawingCache()前先要测量,否则的话得到的bitmap为null,这个我在OnCreate()、OnStart()、OnResume()方法里都试验过。
二、当调用bitmap.compress(CompressFormat.JPEG, 100, fos);保存为图片时发现图片背景为黑色,如下图:
这时只需要改成用png保存就可以了,bitmap.compress(CompressFormat.PNG, 100, fos);,如下图:
在实际开发中,有时候我们需求将文件转换为字符串,然后作为参数进行上传。
必备工具类图片bitmap转成字符串string与String字符串转换为bitmap图片格式
[java] view
plain copy
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Base64;
import java.io.ByteArrayOutputStream;
/**
*
*
* 功能描述:Android开发之常用必备工具类图片bitmap转成字符串string与String字符串转换为bitmap图片格式
*/
public class BitmapAndStringUtils {
/**
* 图片转成string
*
* @param bitmap
* @return
*/
public static String convertIconToString(Bitmap bitmap)
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();// outputstream
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] appicon = baos.toByteArray();// 转为byte数组
return Base64.encodeToString(appicon, Base64.DEFAULT);
}
/**
* string转成bitmap
*
* @param st
*/
public static Bitmap convertStringToIcon(String st)
{
// OutputStream out;
Bitmap bitmap = null;
try
{
// out = new FileOutputStream("/sdcard/aa.jpg");
byte[] bitmapArray;
bitmapArray = Base64.decode(st, Base64.DEFAULT);
bitmap =
BitmapFactory.decodeByteArray(bitmapArray, 0,
bitmapArray.length);
// bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
return bitmap;
}
catch (Exception e)
{
return null;
}
}
}
如果你的图片是File文件,可以用下面代码:
[java] view
plain copy
/**
* 图片文件转换为指定编码的字符串
*
* @param imgFile 图片文件
*/
public static String file2String(File imgFile) {
InputStream in = null;
byte[] data = null;
//读取图片字节数组
try{
in = new FileInputStream(imgFile);
data = new byte[in.available()];
in.read(data);
in.close();
} catch (IOException e){
e.printStackTrace();
}
//对字节数组Base64编码
BASE64Encoder encoder = new BASE64Encoder();
String result = encoder.encode(data);
return result;//返回Base64编码过的字节数组字符串
}
转自:
http://blog.csdn.net/jdsjlzx/article/details/44228935 http://blog.csdn.net/harryweasley/article/details/51955467
相关文章推荐
- Android图片压缩(质量压缩和尺寸压缩)&Bitmap转成字符串上传
- Android图片压缩(质量压缩和尺寸压缩)
- Android图片压缩(质量压缩和尺寸压缩)
- 图片尺寸、质量压缩
- Android质量压缩和尺寸压缩
- Android图片压缩(质量压缩和尺寸压缩)&Bitmap转成字符串上传
- Android图片压缩(质量压缩和尺寸压缩)
- Android图片压缩(质量压缩和尺寸压缩)
- Android图片压缩(质量压缩和尺寸压缩)&Bitmap转成字符串上传
- Android图片压缩(质量压缩和尺寸压缩)
- Android图片压缩(质量压缩和尺寸压缩)
- 一种使用GDI+对图片尺寸和质量的压缩方法
- Android图片压缩(质量压缩和尺寸压缩 File, Stream和Bitmap)
- 传统基础图片压缩:先尺寸压缩后质量压缩
- java 程序实现对图片的压缩生成缩略图并可设定长宽、尺寸压缩率、图片质量
- Android进阶——图片优化之质量压缩、尺寸压缩、采样率压缩、LibJpeg压缩
- 同等尺寸下为何android压缩出来的图片质量不如iphone
- Android图片压缩(质量压缩和尺寸压缩)
- Android图片压缩(质量压缩和尺寸压缩)
- Android图片压缩(质量压缩和尺寸压缩)