Android图片压缩(质量压缩和尺寸压缩)&Bitmap转成字符串上传
来源:互联网 发布:淘宝商家联系方式提取 编辑:程序博客网 时间:2024/05/15 18:31
在网上调查了图片压缩的方法并实装后,大致上可以认为有两类压缩:质量压缩(不改变图片的尺寸)和尺寸压缩(相当于是像素上的压缩);质量压缩一般可用于上传大图前的处理,这样就可以节省一定的流量,毕竟现在的手机拍照都能达到3M左右了,尺寸压缩一般可用于生成缩略图。
两种方法都实装在了我的项目中,结果却发现在质量压缩的模块中,本来1.9M的图片压缩后反而变成3M多了,很是奇怪,再做了进一步调查终于知道原因了。下面这个博客说的比较清晰:
android图片压缩总结
总结来看,图片有三种存在形式:硬盘上时是file,网络传输时是stream,内存中是stream或bitmap,所谓的质量压缩,它其实只能实现对file的影响,你可以把一个file转成bitmap再转成file,或者直接将一个bitmap转成file时,这个最终的file是被压缩过的,但是中间的bitmap并没有被压缩(或者说几乎没有被压缩,我不确定),因为bigmap在内存中的大小是按像素计算的,也就是width * height,对于质量压缩,并不会改变图片的像素,所以就算质量被压缩了,但是bitmap在内存的占有率还是没变小,但你做成file时,它确实变小了;
而尺寸压缩由于是减小了图片的像素,所以它直接对bitmap产生了影响,当然最终的file也是相对的变小了;
最后把自己总结的工具类贴出来:
- 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;
-
-
-
-
-
-
-
- public class ImageFactory {
-
-
-
-
-
-
-
- public Bitmap getBitmap(String imgPath) {
-
- BitmapFactory.Options newOpts = new BitmapFactory.Options();
- newOpts.inJustDecodeBounds = false;
- newOpts.inPurgeable = true;
- newOpts.inInputShareable = true;
-
- newOpts.inSampleSize = 1;
- newOpts.inPreferredConfig = Config.RGB_565;
- return BitmapFactory.decodeFile(imgPath, newOpts);
- }
-
-
-
-
-
-
-
-
- public void storeImage(Bitmap bitmap, String outPath) throws FileNotFoundException {
- FileOutputStream os = new FileOutputStream(outPath);
- bitmap.compress(Bitmap.CompressFormat.JPEG, 100, os);
- }
-
-
-
-
-
-
-
-
-
-
- public Bitmap ratio(String imgPath, float pixelW, float pixelH) {
- BitmapFactory.Options newOpts = new BitmapFactory.Options();
-
- newOpts.inJustDecodeBounds = true;
- newOpts.inPreferredConfig = Config.RGB_565;
-
- Bitmap bitmap = BitmapFactory.decodeFile(imgPath,newOpts);
-
- newOpts.inJustDecodeBounds = false;
- int w = newOpts.outWidth;
- int h = newOpts.outHeight;
-
- float hh = pixelH;
- float ww = pixelW;
-
- int 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;
-
- bitmap = BitmapFactory.decodeFile(imgPath, newOpts);
-
-
- return bitmap;
- }
-
-
-
-
-
-
-
-
-
-
- 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) {
- os.reset();
- image.compress(Bitmap.CompressFormat.JPEG, 50, os);
- }
- ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
- BitmapFactory.Options newOpts = new BitmapFactory.Options();
-
- 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;
- float ww = pixelW;
-
- int 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;
-
- is = new ByteArrayInputStream(os.toByteArray());
- bitmap = BitmapFactory.decodeStream(is, null, newOpts);
-
-
- return bitmap;
- }
-
-
-
-
-
-
-
-
-
- public void compressAndGenImage(Bitmap image, String outPath, int maxSize) throws IOException {
- ByteArrayOutputStream os = new ByteArrayOutputStream();
-
- int options = 100;
-
- image.compress(Bitmap.CompressFormat.JPEG, options, os);
-
- while ( os.toByteArray().length / 1024 > maxSize) {
-
- os.reset();
-
- options -= 10;
- image.compress(Bitmap.CompressFormat.JPEG, options, os);
- }
-
-
- FileOutputStream fos = new FileOutputStream(outPath);
- fos.write(os.toByteArray());
- fos.flush();
- fos.close();
- }
-
-
-
-
-
-
-
-
-
-
- public void compressAndGenImage(String imgPath, String outPath, int maxSize, boolean needsDelete) throws IOException {
- compressAndGenImage(getBitmap(imgPath), outPath, maxSize);
-
-
- if (needsDelete) {
- File file = new File (imgPath);
- if (file.exists()) {
- file.delete();
- }
- }
- }
-
-
-
-
-
-
-
-
-
-
- public void ratioAndGenThumb(Bitmap image, String outPath, float pixelW, float pixelH) throws FileNotFoundException {
- Bitmap bitmap = ratio(image, pixelW, pixelH);
- storeImage( bitmap, outPath);
- }
-
-
-
-
-
-
-
-
-
-
-
- public void ratioAndGenThumb(String imgPath, String outPath, float pixelW, float pixelH, boolean needsDelete) throws FileNotFoundException {
- Bitmap bitmap = ratio(imgPath, pixelW, pixelH);
- storeImage( bitmap, outPath);
-
-
- if (needsDelete) {
- File file = new File (imgPath);
- if (file.exists()) {
- file.delete();
- }
- }
- }
-
- }
如果上面的工具类不满足你,那么看看下面的方法。
一、图片质量压缩
-
-
-
-
-
-
- public static Bitmap compressImage(Bitmap image) {
-
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- image.compress(Bitmap.CompressFormat.JPEG, 100, baos);
- int options = 90;
-
- while (baos.toByteArray().length / 1024 > 100) {
- baos.reset();
- image.compress(Bitmap.CompressFormat.JPEG, options, baos);
- options -= 10;
- }
- ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());
- Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);
- return bitmap;
- }
二、按比例大小压缩 (路径获取图片)
-
-
-
-
-
-
- public static Bitmap getimage(String srcPath) {
-
- BitmapFactory.Options newOpts = new BitmapFactory.Options();
-
- newOpts.inJustDecodeBounds = true;
- Bitmap bitmap = BitmapFactory.decodeFile(srcPath, newOpts);
-
- newOpts.inJustDecodeBounds = false;
- int w = newOpts.outWidth;
- int h = newOpts.outHeight;
-
- float hh = 800f;
- float ww = 480f;
-
- int 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;
-
- bitmap = BitmapFactory.decodeFile(srcPath, newOpts);
- return compressImage(bitmap);
- }
三、按比例大小压缩 (Bitmap)
-
-
-
-
-
-
- public static Bitmap compressScale(Bitmap image) {
-
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- image.compress(Bitmap.CompressFormat.JPEG, 100, baos);
-
-
- if (baos.toByteArray().length / 1024 > 1024) {
- baos.reset();
- image.compress(Bitmap.CompressFormat.JPEG, 80, baos);
- }
- ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());
- BitmapFactory.Options newOpts = new BitmapFactory.Options();
-
- 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);
-
-
-
- float hh = 512f;
- float ww = 512f;
-
- int 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;
-
-
-
- isBm = new ByteArrayInputStream(baos.toByteArray());
- bitmap = BitmapFactory.decodeStream(isBm, null, newOpts);
-
- return compressImage(bitmap);
-
-
- }
--------------------------------------------------------------------------------------------------------------------------------
分享个按照图片尺寸压缩:
- public static void compressPicture(String srcPath, String desPath) {
- FileOutputStream fos = null;
- BitmapFactory.Options op = new BitmapFactory.Options();
-
-
- 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;
-
- 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;
-
- 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图片格式
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.util.Base64;
-
- import java.io.ByteArrayOutputStream;
-
-
-
-
-
-
- public class BitmapAndStringUtils {
-
-
-
-
-
-
- public static String convertIconToString(Bitmap bitmap)
- {
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
- byte[] appicon = baos.toByteArray();
- return Base64.encodeToString(appicon, Base64.DEFAULT);
-
- }
-
-
-
-
-
-
- public static Bitmap convertStringToIcon(String st)
- {
-
- Bitmap bitmap = null;
- try
- {
-
- byte[] bitmapArray;
- bitmapArray = Base64.decode(st, Base64.DEFAULT);
- bitmap =
- BitmapFactory.decodeByteArray(bitmapArray, 0,
- bitmapArray.length);
-
- return bitmap;
- }
- catch (Exception e)
- {
- return null;
- }
- }
- }
如果你的图片是File文件,可以用下面代码:
-
-
-
-
-
- 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();
- }
-
- BASE64Encoder encoder = new BASE64Encoder();
- String result = encoder.encode(data);
- return result;
- }
0 0
- Android图片压缩(质量压缩和尺寸压缩)&Bitmap转成字符串上传
- Android图片压缩(质量压缩和尺寸压缩)&Bitmap转成字符串上传
- Android图片压缩(质量压缩和尺寸压缩)&Bitmap转成字符串上传
- Android图片压缩(质量压缩和尺寸压缩)&Bitmap转成字符串上传
- Android图片压缩(质量压缩和尺寸压缩)&Bitmap转成字符串上传
- Android图片压缩(质量压缩和尺寸压缩)&Bitmap转成字符串上传
- Android图片压缩(质量压缩和尺寸压缩)&Bitmap转成字符串上传
- Android图片压缩(质量压缩和尺寸压缩)&Bitmap转成字符串上传
- Android图片压缩(质量压缩和尺寸压缩)&Bitmap转成字符串上传
- Android图片压缩(质量压缩和尺寸压缩)&Bitmap转成字符串上传
- Android图片压缩(质量压缩和尺寸压缩)&Bitmap转成字符串上传
- Android图片压缩(质量压缩和尺寸压缩)&Bitmap转成字符串上传
- Android图片压缩(质量压缩和尺寸压缩)&Bitmap转成字符串上传
- Android图片压缩(质量压缩和尺寸压缩)&Bitmap转成字符串上传
- Android图片压缩(质量压缩和尺寸压缩)&Bitmap转成字符串上传
- Android图片压缩(质量压缩和尺寸压缩)&Bitmap转成字符串上传
- Android图片压缩(质量压缩和尺寸压缩 File, Stream和Bitmap)
- Android图片压缩(质量压缩和尺寸压缩 File, Stream和Bitmap)
- 设置UILabel的内边距
- jks(私钥)keystore(私钥)truststore(受信任的公钥)cer(公钥)pfx(浏览器);密钥对,公钥,证书,私钥,jks,keystore,truststore,cer,pfx说明
- Linux-磁盘管理基本命令
- c++ Primer Plus(第六版)第十二章习题,写代码之路
- Django list view
- Android图片压缩(质量压缩和尺寸压缩)&Bitmap转成字符串上传
- Java动态代理学习
- linux基础知识(三)
- Android BigDecime的巧妙用法
- 多线程基础
- CCS3属性之text-overflow:ellipsis;的用法和注意之处
- ASP.NET MVC中的Global.asax文件
- php使用GD库
- MySQL--数据库操作