分類:
在網上調查了圖片壓縮的方法並實裝後,大體上能夠認爲有兩類壓縮:質量壓縮(不改變圖片的尺寸)和尺寸壓縮(至關因而像素上的壓縮);質量壓縮通常可用於上傳大圖前的處理,這樣就能夠節省必定的流量,畢竟如今的手機拍照都能達到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();
- }
- }
- }
-
- }
延伸閱讀
android圖片壓縮總結
一.圖片的存在形式
1.文件形式(即以二進制形式存在於硬盤上)
2.流的形式(即以二進制形式存在於內存中)
3.Bitmap形式
這三種形式的區別: 文件形式和流的形式對圖片體積大小並無影響,也就是說,若是你手機SD卡上的若是是100K,那 麼經過流的形式讀到內存中,也必定是佔100K的內存,注意是流的形式,不是Bitmap的形式,當圖片以Bitmap的形式存在時,其佔用的內存會瞬間 變大, 我試過500K文件形式的圖片加載到內存,以Bitmap形式存在時,佔用內存將近10M,固然這個增大的倍數並非固定的
檢測圖片三種形式大小的方法:
文件形式: file.length()
流的形式: 講圖片文件讀到內存輸入流中,看它的byte數
Bitmap: bitmap.getByteCount()
二.常見的壓縮方式
1. 將圖片保存到本地時進行壓縮, 即將圖片從Bitmap形式變爲File形式時進行壓縮,
特色是: File形式的圖片確實被壓縮了, 可是當你從新讀取壓縮後的file爲 Bitmap是,它佔用的內存並無改變
- publicstaticvoid new int;
- while > ) {
- ;
- try new catch }
方法說明: 該方法是壓縮圖片的質量, 注意它不會減小圖片的像素,比方說, 你的圖片是300K的, 1280*700像素的, 通過該方法壓縮後, File形式的圖片是在100如下, 以方便上傳服務器, 可是你BitmapFactory.decodeFile到內存中,變成Bitmap時,它的像素仍然是1280*700, 計算圖片像素的方法是 bitmap.getWidth()和bitmap.getHeight(), 圖片是由像素組成的, 每一個像素又包含什麼呢? 熟悉PS的人知道, 圖片是有色相,明度和飽和度構成的.
該方法的官方文檔也解釋說, 它會讓圖片從新構造, 可是有可能圖像的位深(即色深)和每一個像素的透明度會變化,JPEG onlysupports opaque(不透明), 也就是說以jpeg格式壓縮後, 原來圖片中透明的元素將消失.因此這種格式極可能形成失真
既然它是改變了圖片的顯示質量, 達到了對File形式的圖片進行壓縮, 圖片的像素沒有改變的話, 那從新讀取通過壓縮的file爲Bitmap時, 它佔用的內存並不會少.(不相信的能夠試試)
由於: bitmap.getByteCount() 是計算它的像素所佔用的內存, 請看官方解釋: Returns the number of bytes used to store this bitmap's pixels.
2. 將圖片從本地讀到內存時,進行壓縮 ,即圖片從File形式變爲Bitmap形式
特色: 經過設置採樣率, 減小圖片的像素, 達到對內存中的Bitmap進行壓縮
先看一個方法: 該方法是對內存中的Bitmap進行質量上的壓縮, 由上面的理論能夠得出該方法是無效的, 並且也是沒有必要的,由於你已經將它讀到內存中了,再壓縮畫蛇添足, 儘管在獲取系統相冊圖片時,某些手機會直接返回一個Bitmap,可是這種狀況下, 返回的Bitmap都是通過壓縮的, 它不可能直接返回一個原聲的Bitmap形式的圖片, 後果可想而知
- private new int;
- , baos);
- while > ) {
- ;
- new nullnull return }
再看一個方法:
- private new true
- false int int float
- float
- int;
- if int elseif int if)
- ;
-
-
- true
- true
- // return compressBmpFromBmp(bitmap);//原來的方法調用了這個方法企圖進行二次壓縮
- return }
方法說明: 該方法就是對Bitmap形式的圖片進行壓縮, 也就是經過設置採樣率, 減小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();
- }
- }