Android圖片處理工具類BitmapUtils
Android圖片的處理工具類BitmapUtils,供大家參考,具體內(nèi)容如下
項(xiàng)目中經(jīng)常會(huì)用到圖片,所以在這先簡(jiǎn)單的總結(jié)一下。閑言少敘,上代碼。
package com.lvstudio.myapp.utils;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.PixelFormat;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.DisplayMetrics;
import android.view.WindowManager;
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 java.io.InputStream;
/**
* Created by LvStudio on 2016/11/7.
*/
public class BitmapUtils {
/**
* 屏幕分辨率和指定清晰度的圖片壓縮方法
*
* @param context
* @param image Bitmap圖片
* @return
*/
public static Bitmap comp(Context context, Bitmap image) {
int maxLength = 1024 * 1024; // 預(yù)定的圖片最大內(nèi)存,單位byte
// 壓縮大小
ByteArrayOutputStream baos = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, baos);// 質(zhì)量壓縮方法,這里100表示不壓縮,把壓縮后的數(shù)據(jù)存放到baos中
int options = 100;
while (baos.toByteArray().length > maxLength) { // 循環(huán)判斷,大于繼續(xù)壓縮
options -= 10;// 每次都減少10
baos.reset();// 重置baos即清空baos
image.compress(Bitmap.CompressFormat.JPEG, options, baos);//PNG 壓縮options%
}
// 壓縮尺寸
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
BitmapFactory.Options opts = new BitmapFactory.Options(); // 選項(xiàng)對(duì)象(在加載圖片時(shí)使用)
opts.inJustDecodeBounds = true; // 修改選項(xiàng), 只獲取大小
BitmapFactory.decodeStream(bais, null, opts);// 加載圖片(只得到圖片大小)
// 獲取屏幕大小,按比例壓縮
WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
int scaleX = opts.outWidth / manager.getDefaultDisplay().getWidth(); // X軸縮放比例(圖片寬度/屏幕寬度)
int scaleY = opts.outHeight / manager.getDefaultDisplay().getHeight(); // Y軸縮放比例
int scale = scaleX > scaleY ? scaleX : scaleY; // 圖片的縮放比例(X和Y哪個(gè)大選哪個(gè))
opts.inJustDecodeBounds = false; // 修改選項(xiàng), 不只解碼邊界
opts.inSampleSize = scale > 1 ? scale : 1; // 修改選項(xiàng), 加載圖片時(shí)的縮放比例
return BitmapFactory.decodeStream(bais, null, opts); // 加載圖片(得到壓縮后的圖片)
}
/**
* 屏幕分辨率和指定清晰度的圖片壓縮方法
*
* @param context
* @param path 圖片的路徑
* @return
*/
public static Bitmap comp(Context context, String path) {
return compressImage(getUsableImage(context, path));
}
/**
* 獲取屏幕分辨率的Bitmap
*
* @param context
* @param path 圖片的路徑
* @return
*/
public static Bitmap getUsableImage(Context context, String path) {
BitmapFactory.Options opts = new BitmapFactory.Options(); // 選項(xiàng)對(duì)象(在加載圖片時(shí)使用)
opts.inJustDecodeBounds = true; // 修改選項(xiàng), 只獲取大小
BitmapFactory.decodeFile(path, opts); // 加載圖片(只得到圖片大小)
DisplayMetrics metrics = new DisplayMetrics();
metrics = context.getApplicationContext().getResources().getDisplayMetrics();
int scaleX = opts.outWidth / metrics.widthPixels; // X軸縮放比例(圖片寬度/屏幕寬度)
int scaleY = opts.outHeight / metrics.heightPixels; // Y軸縮放比例
int scale = scaleX > scaleY ? scaleX : scaleY; // 圖片的縮放比例(X和Y哪個(gè)大選哪個(gè))
opts.inJustDecodeBounds = false; // 修改選項(xiàng), 不只解碼邊界
opts.inSampleSize = scale > 1 ? scale : 1; // 修改選項(xiàng), 加載圖片時(shí)的縮放比例
return BitmapFactory.decodeFile(path, opts); // 加載圖片(得到縮放后的圖片)
}
/**
* 壓縮圖片清晰度,到指定大小
*
* @param image
* @return
*/
public static Bitmap compressImage(Bitmap image) {
int maxLength = 1024 * 1024; // (byte)
ByteArrayOutputStream baos = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, baos);// 質(zhì)量壓縮方法,這里100表示不壓縮,把壓縮后的數(shù)據(jù)存放到baos中
int options = 100;
while (baos.toByteArray().length > maxLength) { // 循環(huán)判斷如果壓縮后圖片是否大于1mb,大于繼續(xù)壓縮
options -= 10;// 每次都減少10
baos.reset();// 重置baos即清空baos
image.compress(Bitmap.CompressFormat.JPEG, options, baos);// 這里壓縮options%,把壓縮后的數(shù)據(jù)存放到baos中
}
ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());// 把壓縮后的數(shù)據(jù)baos存放到ByteArrayInputStream中
Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);// 把ByteArrayInputStream數(shù)據(jù)生成圖片
return bitmap;
}
/**
* 指定分辨率和清晰度的圖片壓縮方法
*
* @param fromFile
* @param toFile
* @param reqWidth
* @param reqHeight
* @param quality
*/
public static void transImage(String fromFile, String toFile, int reqWidth, int reqHeight, int quality) {
Bitmap bitmap = BitmapFactory.decodeFile(fromFile);
int bitmapWidth = bitmap.getWidth();
int bitmapHeight = bitmap.getHeight();
// 縮放的尺寸
float scaleWidth = (float) reqWidth / bitmapWidth;
float scaleHeight = (float) reqHeight / bitmapHeight;
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
// 產(chǎn)生縮放后的Bitmap對(duì)象
Bitmap resizeBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmapWidth, bitmapHeight, matrix, false);
// 保存到文件
bitmap2File(toFile, quality, resizeBitmap);
if (!bitmap.isRecycled()) {
// 釋放資源,以防止OOM
bitmap.recycle();
}
if (!resizeBitmap.isRecycled()) {
resizeBitmap.recycle();
}
}
/**
* Bitmap轉(zhuǎn)換為文件
*
* @param toFile
* @param quality
* @param bitmap
* @return
*/
public static File bitmap2File(String toFile, int quality, Bitmap bitmap) {
File captureFile = new File(toFile);
FileOutputStream out = null;
try {
out = new FileOutputStream(captureFile);
if (bitmap.compress(Bitmap.CompressFormat.JPEG, quality, out)) {
out.flush();
out.close();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return captureFile;
}
/**
* Drawable轉(zhuǎn)換為Bitmap
*
* @param drawable
* @return
*/
public static Bitmap drawableToBitamp(Drawable drawable) {
int w = drawable.getIntrinsicWidth();
int h = drawable.getIntrinsicHeight();
Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
: Bitmap.Config.RGB_565;
Bitmap bitmap = Bitmap.createBitmap(w, h, config);
// 注意,下面三行代碼要用到,否在在View或者surfaceview里的canvas.drawBitmap會(huì)看不到圖
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, w, h);
drawable.draw(canvas);
return bitmap;
}
// Bitmap、Drawable、InputStream、byte[] 之間轉(zhuǎn)換
/**********************************************************/
// 1. Bitmap to InputStream
public static InputStream bitmap2Input(Bitmap bitmap, int quality) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, quality, baos);
return new ByteArrayInputStream(baos.toByteArray());
}
public static InputStream bitmap2Input(Bitmap bitmap) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
return new ByteArrayInputStream(baos.toByteArray());
}
// 2. Bitmap to byte[]
public static byte[] bitmap2ByteArray(Bitmap bitmap, int quality) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, quality, baos);
return baos.toByteArray();
}
public static byte[] bitmap2ByteArray(Bitmap bitmap) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
return baos.toByteArray();
}
// 3. Drawable to byte[]
public static byte[] drawable2ByteArray(Drawable drawable) {
Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
ByteArrayOutputStream out = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
return out.toByteArray();
}
// 4. byte[] to Bitmap
public static Bitmap byteArray2Bitmap(byte[] bytes) {
return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android高級(jí)組件Gallery畫廊視圖使用方法詳解
這篇文章主要為大家詳細(xì)介紹了Android高級(jí)組件Gallery畫廊視圖的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
Android App中讀取XML與JSON格式數(shù)據(jù)的基本方法示例
這篇文章主要介紹了Android App中讀取XML與JSON格式數(shù)據(jù)的基本方法示例,Android中自帶的JSONObject非常好用,需要的朋友可以參考下2016-03-03
Android view自定義實(shí)現(xiàn)動(dòng)態(tài)進(jìn)度條
這篇文章主要介紹了Android view自定義實(shí)現(xiàn)動(dòng)態(tài)進(jìn)度條的相關(guān)資料,這里提供實(shí)例代碼及實(shí)現(xiàn)效果圖,需要的朋友可以參考下2016-12-12
Android中搜索圖標(biāo)和文字居中的EditText實(shí)例
本篇文章主要介紹了Android中搜索圖標(biāo)和文字居中的EditText實(shí)例,具有一定的參考價(jià)值,有興趣的可以了解一下2017-06-06
Android自定義view實(shí)現(xiàn)左滑刪除的RecyclerView詳解
RecyclerView是Android一個(gè)更強(qiáng)大的控件,其不僅可以實(shí)現(xiàn)和ListView同樣的效果,還有優(yōu)化了ListView中的各種不足。其可以實(shí)現(xiàn)數(shù)據(jù)縱向滾動(dòng),也可以實(shí)現(xiàn)橫向滾動(dòng)(ListView做不到橫向滾動(dòng))。接下來(lái)講解RecyclerView的用法2022-11-11
Android自定義view繪制圓環(huán)占比動(dòng)畫
這篇文章主要為大家詳細(xì)介紹了Android自定義view繪制圓環(huán)占比動(dòng)畫,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01
Android自定義View實(shí)現(xiàn)垂直時(shí)間軸布局
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)垂直時(shí)間軸布局的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03
Android實(shí)現(xiàn)與Apache Tomcat服務(wù)器數(shù)據(jù)交互(MySql數(shù)據(jù)庫(kù))
本篇文章主要介紹了Android實(shí)現(xiàn)與Apache Tomcat服務(wù)器數(shù)據(jù)交互(MySql數(shù)據(jù)庫(kù)),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
android使用service和activity獲取屏幕尺寸的方法
這篇文章主要介紹了android使用service和activity獲取屏幕尺寸的方法,實(shí)例分析了基于service和activity兩種方法獲取屏幕尺寸的相關(guān)技巧,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下2015-08-08

