欧美三级国产三级日韩三级_亚洲熟妇丰满大屁股熟妇_欧美亚洲成人一区二区三区_国产精品久久久久久模特

一個(gè)好用的android圖片壓縮工具類(lèi) - 新聞資訊 - 云南小程序開(kāi)發(fā)|云南軟件開(kāi)發(fā)|云南網(wǎng)站建設(shè)-昆明葵宇信息科技有限公司

159-8711-8523

云南網(wǎng)建設(shè)/小程序開(kāi)發(fā)/軟件開(kāi)發(fā)

知識(shí)

不管是網(wǎng)站,軟件還是小程序,都要直接或間接能為您產(chǎn)生價(jià)值,我們?cè)谧非笃湟曈X(jué)表現(xiàn)的同時(shí),更側(cè)重于功能的便捷,營(yíng)銷(xiāo)的便利,運(yùn)營(yíng)的高效,讓網(wǎng)站成為營(yíng)銷(xiāo)工具,讓軟件能切實(shí)提升企業(yè)內(nèi)部管理水平和效率。優(yōu)秀的程序?yàn)楹笃谏?jí)提供便捷的支持!

您當(dāng)前位置>首頁(yè) » 新聞資訊 » 技術(shù)分享 >

一個(gè)好用的android圖片壓縮工具類(lèi)

發(fā)表時(shí)間:2020-10-19

發(fā)布人:葵宇科技

瀏覽次數(shù):31




<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">用了良久圖片緊縮,之前人們一向應(yīng)用google的官方圖片緊縮辦法</span>

final BitmapFactory.Options options = new BitmapFactory.Options();
		options.inJustDecodeBounds = true;
		BitmapFactory.decodeResource(res, resId, options);
		
		options.inSampleSize = calculateInSampleSize(options, reqWidth,
				reqHeight);
		options.inJustDecodeBounds = false;
		return BitmapFactory.decodeResource(res, resId, options);

public static int calculateInSampleSize(
            BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {

        final int halfHeight = height / 2;
        final int halfWidth = width / 2;

        // Calculate the largest inSampleSize value that is a power of 2 and keeps both
        // height and width larger than the requested height and width.
        while ((halfHeight / inSampleSize) > reqHeight
                && (halfWidth / inSampleSize) > reqWidth) {
            inSampleSize *= 2;
        }
    }

    return inSampleSize;
}

代率攀來(lái)自google 
http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
細(xì)心看calucelateInSamplesize辦法,該算法返回的是一種緊縮比例,細(xì)心一看他的計(jì)算過(guò)程,你會(huì)發(fā)明,inSampleSize的變更過(guò)程是2-4-8,,而真正進(jìn)入wile輪回時(shí),寬高就已經(jīng)被算作是小了一笆攀來(lái)計(jì)算的了,所以,膳綾擎那個(gè)網(wǎng)站說(shuō)12M能緊縮到0.75M,就是因?yàn)椴罹嗵竽暌?,如不雅安卓手機(jī)內(nèi)部緊縮本身的圖片(大年夜概是2M緊縮到100K),所以此時(shí),這個(gè)辦法就實(shí)用于android編碼緊縮了。
所以不才針對(duì)于android編碼時(shí)緊縮,在此對(duì)它進(jìn)行了優(yōu)化,優(yōu)化后代碼如下:
運(yùn)行:   culculateInSampleSize(bm,200,300)效不雅:
<pre name="code" class="java"><span style="font-family: Arial, Helvetica, sans-serif;">/**</span>
* 盤(pán)似揭捉縮比例值(改進(jìn)版 by touch_ping)

* 原版2>4>8...倍緊縮
* 當(dāng)前2>3>4...倍緊縮

* @param options
*            解析圖片的設(shè)備信息
* @param reqWidth
*            所需圖片緊縮尺寸最小寬度
* @param reqHeight
*            所需圖片緊縮尺寸最小高度
* @return
*/
public static int calculateInSampleSize(BitmapFactory.Options options,
int reqWidth, int reqHeight) {

final int picheight = options.outHeight;
final int picwidth = options.outWidth;
Log.i("test", "原尺寸:" +  picwidth + "*" +picheight);

int targetheight = picheight;
int targetwidth = picwidth;
int inSampleSize = 1;

if (targetheight > reqHeight || targetwidth > reqWidth) {
while (targetheight  >= reqHeight
&& targetwidth>= reqWidth) {
Log.i("test","緊縮:" +inSampleSize + "倍");
inSampleSize += 1;
targetheight = picheight/inSampleSize;
targetwidth = picwidth/inSampleSize;
}
}

Log.i("test","最終緊縮比例:" +inSampleSize + "倍");
Log.i("test", "新尺寸:" +  targetwidth + "*" +targetheight);
return inSampleSize;
}


緊縮效不雅如下:
[img]
文件大年夜小大年夜1.12M變成81.75k
最終附上完全緊縮對(duì)象類(lèi):
package com.example.mqtest;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

/**
 * 圖片緊縮對(duì)象類(lèi)
 * @author touch_ping
 * 2015-1-5 下晝1:29:59
 */
public class BitmapCompressor {
	/**
	 * 質(zhì)量緊縮
	 * @author ping 2015-1-5 下晝1:29:58
	 * @param image
	 * @param maxkb
	 * @return
	 */
	public static Bitmap compressBitmap(Bitmap image,int maxkb) {
		//L.showlog("緊縮圖片");
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		image.compress(Bitmap.CompressFormat.JPEG, 50, baos);// 質(zhì)量緊縮辦法,這里100表示不緊縮,把緊縮后的數(shù)據(jù)存放到baos中
		int options = 100;
//		Log.i("test","原始大年夜小" + baos.toByteArray().length);
		while (baos.toByteArray().length / 1024 > maxkb) { // 輪回?cái)喽ㄈ绮谎啪o縮后圖片是否大年夜于(maxkb)50kb,大年夜于持續(xù)緊縮
//			Log.i("test","緊縮一次!");
			baos.reset();// 重置baos即清空baos
			options -= 10;// 每次都削減10
			image.compress(Bitmap.CompressFormat.JPEG, options, baos);// 這里緊縮options%,把緊縮后的數(shù)據(jù)存放到baos中
		}
//		Log.i("test","緊縮后大年夜小" + baos.toByteArray().length);
		ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());// 把緊縮后的數(shù)據(jù)baos存放到ByteArrayInputStream中
		Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);// 把ByteArrayInputStream數(shù)據(jù)生成圖片
		return bitmap;
	}
	
	/**
	 * http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
	 * 官網(wǎng):獲取緊縮后的圖片
	 * 
	 * @param res
	 * @param resId
	 * @param reqWidth
	 *            所需圖片緊縮尺寸最小寬度
	 * @param reqHeight
	 *            所需圖片緊縮尺寸最小高度
	 * @return
	 */
	public static Bitmap decodeSampledBitmapFromResource(Resources res,
			int resId, int reqWidth, int reqHeight) {
		final BitmapFactory.Options options = new BitmapFactory.Options();
		options.inJustDecodeBounds = true;
		BitmapFactory.decodeResource(res, resId, options);
		
		options.inSampleSize = calculateInSampleSize(options, reqWidth,
				reqHeight);
		options.inJustDecodeBounds = false;
		return BitmapFactory.decodeResource(res, resId, options);
	}

	/**
	 * 官網(wǎng):獲取緊縮后的圖片
	 * 
	 * @param res
	 * @param resId
	 * @param reqWidth
	 *            所需圖片緊縮尺寸最小寬度
	 * @param reqHeight
	 *            所需圖片緊縮尺寸最小高度
	 * @return
	 */
	public static Bitmap decodeSampledBitmapFromFile(String filepath,
			int reqWidth, int reqHeight) {
		final BitmapFactory.Options options = new BitmapFactory.Options();
		options.inJustDecodeBounds = true;
		BitmapFactory.decodeFile(filepath, options);

		options.inSampleSize = calculateInSampleSize(options, reqWidth,
				reqHeight);
		options.inJustDecodeBounds = false;
		return BitmapFactory.decodeFile(filepath, options);
	}

	public static Bitmap decodeSampledBitmapFromBitmap(Bitmap bitmap,
			int reqWidth, int reqHeight) {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		bitmap.compress(Bitmap.CompressFormat.PNG, 90, baos);
		byte[] data = http://www.sjsjw.com/100/000202MYM011683/baos.toByteArray();
		
		final BitmapFactory.Options options = new BitmapFactory.Options();
		options.inJustDecodeBounds = true;
		BitmapFactory.decodeByteArray(data, 0, data.length, options);
		options.inSampleSize = calculateInSampleSize(options, reqWidth,
				reqHeight);
		options.inJustDecodeBounds = false;
		return BitmapFactory.decodeByteArray(data, 0, data.length, options);
	}

	/**
	 * 盤(pán)似揭捉縮比例值(改進(jìn)版 by touch_ping)
	 * 
	 * 原版2>4>8...倍緊縮
	 * 當(dāng)前2>3>4...倍緊縮
	 * 
	 * @param options
	 *            解析圖片的設(shè)備信息
	 * @param reqWidth
	 *            所需圖片緊縮尺寸最小寬度O
	 * @param reqHeight
	 *            所需圖片緊縮尺寸最小高度
	 * @return
	 */
	public static int calculateInSampleSize(BitmapFactory.Options options,
			int reqWidth, int reqHeight) {
		
		final int picheight = options.outHeight;
		final int picwidth = options.outWidth;
		Log.i("test", "原尺寸:" +  picwidth + "*" +picheight);
		
		int targetheight = picheight;
		int targetwidth = picwidth;
		int inSampleSize = 1;
		
		if (targetheight > reqHeight || targetwidth > reqWidth) {
			while (targetheight  >= reqHeight
					&& targetwidth>= reqWidth) {
				Log.i("test","緊縮:" +inSampleSize + "倍");
				inSampleSize += 1;
				targetheight = picheight/inSampleSize;
				targetwidth = picwidth/inSampleSize;
			}
		}
		
		Log.i("test","最終緊縮比例:" +inSampleSize + "倍");
		Log.i("test", "新尺寸:" +  targetwidth + "*" +targetheight);
		return inSampleSize;
	}
}

public static int calculateInSampleSize(
            BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {

        final int halfHeight = height / 2;
        final int halfWidth = width / 2;

        // Calculate the largest inSampleSize value that is a power of 2 and keeps both
        // height and width larger than the requested height and width.
        while ((halfHeight / inSampleSize) > reqHeight
                && (halfWidth / inSampleSize) > reqWidth) {
            inSampleSize *= 2;
        }
    }

    return inSampleSize;
}

相關(guān)案例查看更多