android bitmap compress(圖片壓縮)代碼
android的照相功能隨著手機(jī)硬件的發(fā)展,變得越來(lái)越強(qiáng)大,能夠找出很高分辨率的圖片。
有些場(chǎng)景中,需要照相并且上傳到服務(wù),但是由于圖片的大小太大,那么就上傳就會(huì)很慢(在有些網(wǎng)絡(luò)情況下),而且很耗流量,要想速度快,那么就需要減小圖片的大小。減少圖片的大小有兩種方法,1. 照小圖片; 2. 壓縮大圖片。 照相時(shí)獲取小圖片一般不太符合要求,因?yàn)椋瑘D片的清晰度會(huì)很差,但是這種情況有個(gè)好處就是應(yīng)用速度會(huì)快些; 壓縮圖片,就是把大圖片壓縮小,降低圖片的質(zhì)量,在一定范圍內(nèi),降低圖片的大小,并且滿足需求(圖片仍就清晰)。下面組要是介紹圖片的壓縮:
1. 照相請(qǐng)查看http://www.dhdzp.com/article/37760.htm ->想要保存圖片到制定目錄,啟動(dòng)Camera應(yīng)用時(shí),需要指定文件
2. 壓縮過(guò)程:
2.1 從圖片路徑中讀取圖片(圖片很大,不能全部加在到內(nèi)存中處理,要是全部加載到內(nèi)存中會(huì)內(nèi)存溢出)
[java]
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, 480, 800);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
Bitmap bm = BitmapFactory.decodeFile(filePath, options);
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, 480, 800);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
Bitmap bm = BitmapFactory.decodeFile(filePath, options);
2.2 處理圖片旋轉(zhuǎn)
[java]
int degree = readPictureDegree(filePath);
bm = rotateBitmap(bm,degree) ;
int degree = readPictureDegree(filePath);
bm = rotateBitmap(bm,degree) ;[java] view plaincopyprint?private static int readPictureDegree(String path) {
int degree = 0;
try {
ExifInterface exifInterface = new ExifInterface(path);
int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
degree = 90;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
degree = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
degree = 270;
break;
}
} catch (IOException e) {
e.printStackTrace();
}
return degree;
}
private static int readPictureDegree(String path) {
int degree = 0;
try {
ExifInterface exifInterface = new ExifInterface(path);
int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
degree = 90;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
degree = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
degree = 270;
break;
}
} catch (IOException e) {
e.printStackTrace();
}
return degree;
}
[java]
view plaincopyprint?private static Bitmap rotateBitmap(Bitmap bitmap, int rotate){
if(bitmap == null)
return null ;
int w = bitmap.getWidth();
int h = bitmap.getHeight();
// Setting post rotate to 90
Matrix mtx = new Matrix();
mtx.postRotate(rotate);
return Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, true);
}
private static Bitmap rotateBitmap(Bitmap bitmap, int rotate){
if(bitmap == null)
return null ;
int w = bitmap.getWidth();
int h = bitmap.getHeight();
// Setting post rotate to 90
Matrix mtx = new Matrix();
mtx.postRotate(rotate);
return Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, true);
}
2.3壓縮圖片
[java]
bm.compress(Bitmap.CompressFormat.JPEG, 30, baos);//30 是壓縮率,表示壓縮70%; 如果不壓縮是100,表示壓縮率為0
bm.compress(Bitmap.CompressFormat.JPEG, 30, baos);//30 是壓縮率,表示壓縮70%; 如果不壓縮是100,表示壓縮率為0
完整的方法代碼:
[java]
public static Bitmap getSmallBitmap(String filePath) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, 480, 800);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
Bitmap bm = BitmapFactory.decodeFile(filePath, options);
if(bm == null){
return null;
}
int degree = readPictureDegree(filePath);
bm = rotateBitmap(bm,degree) ;
ByteArrayOutputStream baos = null ;
try{
baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 30, baos);
}finally{
try {
if(baos != null)
baos.close() ;
} catch (IOException e) {
e.printStackTrace();
}
}
return bm ;
}
public static Bitmap getSmallBitmap(String filePath) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, 480, 800);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
Bitmap bm = BitmapFactory.decodeFile(filePath, options);
if(bm == null){
return null;
}
int degree = readPictureDegree(filePath);
bm = rotateBitmap(bm,degree) ;
ByteArrayOutputStream baos = null ;
try{
baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 30, baos);
}finally{
try {
if(baos != null)
baos.close() ;
} catch (IOException e) {
e.printStackTrace();
}
}
return bm ;
}
[java]
view plaincopyprint?private 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) {
// Calculate ratios of height and width to requested height and
// width
final int heightRatio = Math.round((float) height
/ (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);
// Choose the smallest ratio as inSampleSize value, this will
// guarantee
// a final image with both dimensions larger than or equal to the
// requested height and width.
inSampleSize = heightRatio < widthRatio ? widthRatio : heightRatio;
}
return inSampleSize;
}
private 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) {
// Calculate ratios of height and width to requested height and
// width
final int heightRatio = Math.round((float) height
/ (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);
// Choose the smallest ratio as inSampleSize value, this will
// guarantee
// a final image with both dimensions larger than or equal to the
// requested height and width.
inSampleSize = heightRatio < widthRatio ? widthRatio : heightRatio;
}
return inSampleSize;
}
相關(guān)文章
Android懸浮窗屏蔽懸浮窗外部所有的點(diǎn)擊事件的實(shí)例代碼
這篇文章主要介紹了Android懸浮窗屏蔽懸浮窗外部所有的點(diǎn)擊事件實(shí)例代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-03-03
Android 中ListView的Item點(diǎn)擊事件失效的快速解決方法
這篇文章主要介紹了Android 中ListView的Item點(diǎn)擊事件失效的快速解決方法的相關(guān)資料,需要的朋友可以參考下2016-09-09
MVVM和MVVMLight框架介紹及在項(xiàng)目中的使用詳解
這篇文章主要為大家介紹了MVVM和MVVMLight的介紹及在項(xiàng)目中的使用詳解有需要的朋友可以借鑒參考下,祝大家除夕快樂(lè)多多進(jìn)步2022-01-01
Android開發(fā)之?dāng)?shù)據(jù)的存儲(chǔ)方式詳解
本篇文章主要介紹了Android開發(fā)之?dāng)?shù)據(jù)的存儲(chǔ)方式,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧。2016-11-11
Android編程設(shè)計(jì)模式之Builder模式實(shí)例詳解
這篇文章主要介紹了Android編程設(shè)計(jì)模式之Builder模式,結(jié)合實(shí)例形式詳細(xì)分析了Android設(shè)計(jì)模式之Builder模式概念、功能、使用場(chǎng)景、用法及相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-12-12
Android與單片機(jī)通信常用數(shù)據(jù)轉(zhuǎn)換方法總結(jié)
本文主要介紹Android與單片機(jī)通信常用數(shù)據(jù)轉(zhuǎn)換方法,這里提供了代碼示例,有需要的小伙伴可以參考下2016-09-09
Android自定義View實(shí)現(xiàn)掃描效果
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)掃描效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-04-04
Android:Field can be converted to a local varible.的解決辦法
這篇文章主要介紹了Android:Field can be converted to a local varible.的解決辦法的相關(guān)資料,希望通過(guò)本文能幫助到大家,讓大家遇到這樣的問(wèn)題輕松解決,需要的朋友可以參考下2017-10-10

