這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)?lái)有關(guān)Android中有哪些圖片壓縮工具類,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
創(chuàng)新互聯(lián)是一家專注于網(wǎng)站制作、網(wǎng)站設(shè)計(jì)與策劃設(shè)計(jì),天寧網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設(shè)10余年,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:天寧等地區(qū)。天寧做網(wǎng)站價(jià)格咨詢:13518219792
處理策略:
1.使用縮略圖(Thumbnails);
Android系統(tǒng)會(huì)給檢測(cè)到的圖片創(chuàng)建縮略圖;可以操作Media內(nèi)容提供者中的Image對(duì)圖片進(jìn)行操作;
2.手動(dòng)壓縮:
(1)根據(jù)圖片和屏幕尺寸,等比壓縮,完美顯示;
(2)降低圖片質(zhì)量,壓縮圖片大??;
以下是自己整理的小工具類(對(duì)于按比例縮放后,在此并未再進(jìn)行質(zhì)量縮放,此時(shí)圖片大小有可能超出我們期望的限制;假如我們有嚴(yán)格的大小限制需求,可先進(jìn)行按比例縮放后,判斷此時(shí)圖片大小是否超出限制;如果超出限制,對(duì)其再進(jìn)行質(zhì)量縮放即可。建議使用按比例縮放,按質(zhì)量縮放很有可能導(dǎo)致圖片失真。)
</pre><p><pre name="code" class="java">package com.util; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import android.graphics.Bitmap; import android.graphics.Matrix; import android.graphics.Bitmap.CompressFormat; import android.graphics.BitmapFactory; import android.media.ExifInterface; /** * 圖片壓縮工具類 * @author 丶Life_ */ public class ImageCompressUtil { /** * 通過(guò)降低圖片的質(zhì)量來(lái)壓縮圖片 * @param bmp * 要壓縮的圖片位圖對(duì)象 * @param maxSize * 壓縮后圖片大小的最大值,單位KB * @return 壓縮后的圖片位圖對(duì)象 */ public static Bitmap compressByQuality(Bitmap bitmap, int maxSize) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); int quality = 100; bitmap.compress(CompressFormat.JPEG, quality, baos); System.out.println("圖片壓縮前大?。?quot; + baos.toByteArray().length + "byte"); boolean isCompressed = false; while (baos.toByteArray().length / 1024 > maxSize) { quality -= 10; baos.reset(); bitmap.compress(CompressFormat.JPEG, quality, baos); System.out.println("質(zhì)量壓縮到原來(lái)的" + quality + "%時(shí)大小為:" + baos.toByteArray().length + "byte"); isCompressed = true; } System.out.println("圖片壓縮后大?。?quot; + baos.toByteArray().length + "byte"); if (isCompressed) { Bitmap compressedBitmap = BitmapFactory.decodeByteArray( baos.toByteArray(), 0, baos.toByteArray().length); recycleBitmap(bitmap); return compressedBitmap; } else { return bitmap; } } /** * 傳入圖片url,通過(guò)壓縮圖片的尺寸來(lái)壓縮圖片大小 * @param pathName 圖片的完整路徑 * @param targetWidth 縮放的目標(biāo)寬度 * @param targetHeight 縮放的目標(biāo)高度 * @return 縮放后的圖片 */ public static Bitmap compressBySize(String pathName, int targetWidth, int targetHeight) { BitmapFactory.Options opts = new BitmapFactory.Options(); opts.inJustDecodeBounds = true;// 不去真的解析圖片,只是獲取圖片的頭部信息,包含寬高等; Bitmap bitmap = BitmapFactory.decodeFile(pathName, opts); // 得到圖片的寬度、高度; int imgWidth = opts.outWidth; int imgHeight = opts.outHeight; // 分別計(jì)算圖片寬度、高度與目標(biāo)寬度、高度的比例;取大于等于該比例的最小整數(shù); int widthRatio = (int) Math.ceil(imgWidth / (float) targetWidth); int heightRatio = (int) Math.ceil(imgHeight / (float) targetHeight); if (widthRatio > 1 || heightRatio > 1) { if (widthRatio > heightRatio) { opts.inSampleSize = widthRatio; } else { opts.inSampleSize = heightRatio; } } // 設(shè)置好縮放比例后,加載圖片進(jìn)內(nèi)容; opts.inJustDecodeBounds = false; bitmap = BitmapFactory.decodeFile(pathName, opts); return bitmap; } /** * 傳入bitmap,通過(guò)壓縮圖片的尺寸來(lái)壓縮圖片大小 * @param bitmap 要壓縮圖片 * @param targetWidth 縮放的目標(biāo)寬度 * @param targetHeight 縮放的目標(biāo)高度 * @return 縮放后的圖片 */ public static Bitmap compressBySize(Bitmap bitmap, int targetWidth, int targetHeight) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); bitmap.compress(CompressFormat.JPEG, 100, baos); BitmapFactory.Options opts = new BitmapFactory.Options(); opts.inJustDecodeBounds = true; bitmap = BitmapFactory.decodeByteArray(baos.toByteArray(), 0, baos.toByteArray().length, opts); // 得到圖片的寬度、高度; int imgWidth = opts.outWidth; int imgHeight = opts.outHeight; // 分別計(jì)算圖片寬度、高度與目標(biāo)寬度、高度的比例;取大于該比例的最小整數(shù); int widthRatio = (int) Math.ceil(imgWidth / (float) targetWidth); int heightRatio = (int) Math.ceil(imgHeight / (float) targetHeight); if (widthRatio > 1 || heightRatio > 1) { if (widthRatio > heightRatio) { opts.inSampleSize = widthRatio; } else { opts.inSampleSize = heightRatio; } } // 設(shè)置好縮放比例后,加載圖片進(jìn)內(nèi)存; opts.inJustDecodeBounds = false; Bitmap compressedBitmap = BitmapFactory.decodeByteArray( baos.toByteArray(), 0, baos.toByteArray().length, opts); recycleBitmap(bitmap); return compressedBitmap; } /** * 通過(guò)壓縮圖片的尺寸來(lái)壓縮圖片大小,通過(guò)讀入流的方式,可以有效防止網(wǎng)絡(luò)圖片數(shù)據(jù)流形成位圖對(duì)象時(shí)內(nèi)存過(guò)大的問(wèn)題; * @param InputStream 要壓縮圖片,以流的形式傳入 * @param targetWidth 縮放的目標(biāo)寬度 * @param targetHeight 縮放的目標(biāo)高度 * @return 縮放后的圖片 * @throws IOException 讀輸入流的時(shí)候發(fā)生異常 */ public static Bitmap compressBySize(InputStream is, int targetWidth, int targetHeight) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buff = new byte[1024]; int len = 0; while ((len = is.read(buff)) != -1) { baos.write(buff, 0, len); } byte[] data = baos.toByteArray(); BitmapFactory.Options opts = new BitmapFactory.Options(); opts.inJustDecodeBounds = true; Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, opts); // 得到圖片的寬度、高度; int imgWidth = opts.outWidth; int imgHeight = opts.outHeight; // 分別計(jì)算圖片寬度、高度與目標(biāo)寬度、高度的比例;取大于該比例的最小整數(shù); int widthRatio = (int) Math.ceil(imgWidth / (float) targetWidth); int heightRatio = (int) Math.ceil(imgHeight / (float) targetHeight); if (widthRatio > 1 || heightRatio > 1) { if (widthRatio > heightRatio) { opts.inSampleSize = widthRatio; } else { opts.inSampleSize = heightRatio; } } // 設(shè)置好縮放比例后,加載圖片進(jìn)內(nèi)存; opts.inJustDecodeBounds = false; bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, opts); return bitmap; } /** * 旋轉(zhuǎn)圖片擺正顯示 * @param srcPath * @param bitmap * @return */ public static Bitmap rotateBitmapByExif(String srcPath, Bitmap bitmap) { ExifInterface exif; Bitmap newBitmap = null; try { exif = new ExifInterface(srcPath); if (exif != null) { // 讀取圖片中相機(jī)方向信息 int ori = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); int digree = 0; switch (ori) { case ExifInterface.ORIENTATION_ROTATE_90: digree = 90; break; case ExifInterface.ORIENTATION_ROTATE_180: digree = 180; break; case ExifInterface.ORIENTATION_ROTATE_270: digree = 270; break; } if (digree != 0) { Matrix m = new Matrix(); m.postRotate(digree); newBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), m, true); recycleBitmap(bitmap); return newBitmap; } } } catch (IOException e) { e.printStackTrace(); } return bitmap; } /** * 回收位圖對(duì)象 * @param bitmap */ public static void recycleBitmap(Bitmap bitmap) { if (bitmap != null && !bitmap.isRecycled()) { bitmap.recycle(); System.gc(); bitmap = null; } } }
上述就是小編為大家分享的Android中有哪些圖片壓縮工具類了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
當(dāng)前標(biāo)題:Android中有哪些圖片壓縮工具類
URL標(biāo)題:http://chinadenli.net/article40/ppgheo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站維護(hù)、網(wǎng)站制作、網(wǎng)站策劃、App設(shè)計(jì)、用戶體驗(yàn)、微信公眾號(hào)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)