付費(fèi)下載
下載本文檔
版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
【移動(dòng)應(yīng)用開(kāi)發(fā)技術(shù)】Android中怎么實(shí)現(xiàn)一個(gè)圖片壓縮工具類
本篇文章為大家展示了Android中怎么實(shí)現(xiàn)一個(gè)圖片壓縮工具類,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。具體如下:package
com.sanweidu.TddPay.util2;
import
java.io.ByteArrayInputStream;
import
java.io.ByteArrayOutputStream;
import
android.graphics.Bitmap;
import
android.graphics.BitmapFactory;
public
class
ImaZipUtil
{
/**
*
壓縮圖片到指定寬高,并進(jìn)行質(zhì)量壓縮,最終大小保持在100K以下
*
*
@param
sourceBm
*
@param
targetWidth
*
@param
targetHeight
*
@return
*/
public
static
Bitmap
zipPic(Bitmap
sourceBm,
float
targetWidth,
float
targetHeight)
{
BitmapFactory.Options
newOpts
=
new
BitmapFactory.Options();
//
開(kāi)始讀入圖片,此時(shí)把options.inJustDecodeBounds
設(shè)回true了
newOpts.inJustDecodeBounds
=
true;
//
可刪除
newOpts.inPurgeable
=
true;
//
可共享
newOpts.inInputShareable
=
true;
//
轉(zhuǎn)成數(shù)組
ByteArrayOutputStream
baos
=
new
ByteArrayOutputStream();
sourceBpress(Bitmap.CompressFormat.JPEG,
100,
baos);
byte[]
temp
=
baos.toByteArray();
//
此時(shí)返回bm為空
Bitmap
bitmap
=
BitmapFactory.decodeByteArray(temp,
0,
temp.length,
newOpts);
newOpts.inJustDecodeBounds
=
false;
int
w
=
newOpts.outWidth;
int
h
=
newOpts.outHeight;
//
現(xiàn)在主流手機(jī)比較多是800*480分辨率,所以高和寬我們?cè)O(shè)置為
float
hh
=
targetHeight;
float
ww
=
targetWidth;
//
縮放比。由于是固定比例縮放,只用高或者寬其中一個(gè)數(shù)據(jù)進(jìn)行計(jì)算即可
int
be
=
1;//
be=1表示不縮放
//
如果寬度大的話根據(jù)寬度固定大小縮放
if
(w
>
h
&&
w
>
ww)
{
be
=
(int)
(newOpts.outWidth
/
ww);
}
else
if
(w
<
h
&&
h
>
hh)
{
//
如果高度高的話根據(jù)寬度固定大小縮放
be
=
(int)
(newOpts.outHeight
/
hh);
}
if
(be
<=
0)
{
be
=
1;
}
//
設(shè)置縮放比例
newOpts.inSampleSize
=
be;
//
重新讀入圖片,注意此時(shí)已經(jīng)把options.inJustDecodeBounds
設(shè)回false了
bitmap
=
BitmapFactory.decodeByteArray(temp,
0,
temp.length,
newOpts);
//
壓縮好比例大小后再進(jìn)行質(zhì)量壓縮
return
compressImage(bitmap);
}
/**
*
@Description
質(zhì)量壓縮方法
*
@author
XiongJie
*
@param
image
*
@return
*/
public
static
Bitmap
compressImage(Bitmap
image)
{
ByteArrayOutputStream
baos
=
new
ByteArrayOutputStream();
//
質(zhì)量壓縮方法,這里100表示不壓縮,把壓縮后的數(shù)據(jù)存放到baos中
press(Bitmap.CompressFormat.JPEG,
100,
baos);
int
options
=
100;
//
循環(huán)判斷如果壓縮后圖片是否大于100kb,大于繼續(xù)壓縮
while
(baos.toByteArray().length
/
1024
>
100)
{
//
重置baos即清空baos
baos.reset();
//
這里壓縮options%,把壓縮后的數(shù)據(jù)存放到baos中
press(Bitmap.CompressFormat.JPEG,
options,
baos);
//
每次都減少10
options
-=
10;
}
//
把壓縮后的數(shù)據(jù)baos存放到ByteArrayInputStream中
ByteArrayInputStream
isBm
=
new
ByteArrayInputStream(baos.toByteArray());
//
把ByteArrayInputStream數(shù)據(jù)生成圖片
Bitmap
bitmap
=
BitmapFactory.decodeStream(isBm,
null,
null);
return
bitmap;
}
/**
*
只進(jìn)行分辨率壓縮,不進(jìn)行圖片的質(zhì)量壓縮
*
*
@param
sourceBm
*
@param
targetWidth
*
@param
targetHeight
*
@return
*/
public
static
Bitmap
zipPicWithoutCompress(Bitmap
sourceBm,
float
targetWidth,
float
targetHeight)
{
BitmapFactory.Options
newOpts
=
new
BitmapFactory.Options();
//
開(kāi)始讀入圖片,此時(shí)把options.inJustDecodeBounds
設(shè)回true了
newOpts.inJustDecodeBounds
=
true;
//
可刪除
newOpts.inPurgeable
=
true;
//
可共享
newOpts.inInputShareable
=
true;
//
轉(zhuǎn)成數(shù)組
ByteArrayOutputStream
baos
=
new
ByteArrayOutputStream();
sourceBpress(Bitmap.CompressFormat.JPEG,
100,
baos);
byte[]
temp
=
baos.toByteArray();
//
此時(shí)返回bm為空
Bitmap
bitmap
=
BitmapFactory.decodeByteArray(temp,
0,
temp.length,
newOpts);
newOpts.inJustDecodeBounds
=
false;
int
w
=
newOpts.outWidth;
int
h
=
newOpts.outHeight;
//
現(xiàn)在主流手機(jī)比較多是800*480分辨率,所以高和寬我們?cè)O(shè)置為
float
hh
=
targetHeight;
float
ww
=
targetWidth;
//
縮放比。由于是固定比例縮放,只用高或者寬其中一個(gè)數(shù)據(jù)進(jìn)行計(jì)算即可
//
be=1表示不縮放
int
be
=
1;
if
(w
>
h
&&
w
>
ww)
{
//
如果寬度大的話根據(jù)寬度固定大小縮放
be
=
(int)
(newOpts.outWidth
/
ww);
}
else
if
(w
<
h
&&
h
>
hh)
{
//
如果高度高的話根據(jù)寬度固定大小縮放
be
=
(int)
(newOpts.outHeight
/
hh);
}
if
(be
<=
0)
{
be
=
1;
}
//
設(shè)置縮放比例
n
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年中職旅游(旅游文化常識(shí))試題及答案
- 2026年成本會(huì)計(jì)(費(fèi)用核算)試題及答案
- 2025年高職食品加工技術(shù)應(yīng)用(應(yīng)用實(shí)操訓(xùn)練)試題及答案
- 2025年中職數(shù)字媒體藝術(shù)設(shè)計(jì)(新媒體設(shè)計(jì)技巧)試題及答案
- 【歷史】中國(guó)特色社會(huì)主義事業(yè)取得新成就(課件)2025-2026學(xué)年統(tǒng)編版八年級(jí)歷史下冊(cè)
- 近五年甘肅中考物理試題及答案2025
- 養(yǎng)老院家屬溝通制度
- 信息保密制度
- 工行借記卡介紹
- 2026年公共關(guān)系基礎(chǔ)知識(shí)與實(shí)務(wù)考試題目含答案
- 材料進(jìn)場(chǎng)檢驗(yàn)記錄表
- 復(fù)方蒲公英注射液在銀屑病中的應(yīng)用研究
- 住培中醫(yī)病例討論-面癱
- 設(shè)備安裝施工方案范本
- 衛(wèi)生院副院長(zhǎng)先進(jìn)事跡材料
- 復(fù)發(fā)性抑郁癥個(gè)案查房課件
- 人類學(xué)概論(第四版)課件 第1、2章 人類學(xué)要義第一節(jié)何為人類學(xué)、人類學(xué)的理論發(fā)展過(guò)程
- 《功能性食品學(xué)》第七章-輔助改善記憶的功能性食品
- 幕墻工程竣工驗(yàn)收?qǐng)?bào)告2-2
- 1、工程竣工決算財(cái)務(wù)審計(jì)服務(wù)項(xiàng)目投標(biāo)技術(shù)方案
- 改進(jìn)維持性血液透析患者貧血狀況PDCA
評(píng)論
0/150
提交評(píng)論