有時候我們在項目中,需要把圖片加工一下,比如說加個圓角,或者打上馬賽克或者水印,雖然不是很難的東西,但是沒弄過,還是很糾結的,比如像我這樣的小白,現在根據網上一些大牛的一些方法,總結了下面這個圖片加工廠工具類,可能還有更多的需求,有需要的時候,再去網上找,繼續完善中,,直接上跟大家分享代碼,注釋比較多,而且簡單,我就不多說了,直接上跟大家分享代碼:
[java]
/**
* 圖片加工廠
*
* @author way
*
*/
public class ImageUtil {
/**
* 通過路徑獲取輸入流
*
* @param path
* 路徑
* @return 輸入流
* @throws Exception
* 異常
*/
public static InputStream getRequest(String path) throws Exception {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
if (conn.getResponseCode() == 200) {
return conn.getInputStream();
}
return null;
}
/**
* 從流中讀取二進制數據
*
* @param inStream
* 輸入流
* @return 二進制數據
* @throws Exception
* 異常
*/
public static byte[] readInputStream(InputStream inStream) throws Exception {
ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
byte[] buffer = new byte[4096];
int len = 0;
while ((len = inStream.read(buffer)) != -1) {
outSteam.write(buffer, 0, len);
}
outSteam.close();
inStream.close();
return outSteam.toByteArray();
}
/**
* 把一個路徑轉換成Drawable對象
*
* @param url
* 路徑
* @return Drawable對象
*/
public static Drawable loadImageFromUrl(String url) {
URL m;
InputStream i = null;
try {
m = new URL(url);
i = (InputStream) m.getContent();
} catch (MalformedURLException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Drawable d = Drawable.createFromStream(i, "src");
return d;
}
/**
* 把一個路徑轉換成Drawable對象
*
* @param url
* 字符串路徑
* @return Drawable對象
* @throws Exception
* 異常
*/
public static Drawable getDrawableFromUrl(String url) throws Exception {
return Drawable.createFromStream(getRequest(url), null);
}
/**
* 從路徑中得到位圖
*
* @param url
* 字符串路徑
* @return 位圖
* @throws Exception
* 異常
*/
public static Bitmap getBitmapFromUrl(String url) throws Exception {
byte[] bytes = getBytesFromUrl(url);
return byteToBitmap(bytes);
}
/**
* 從路徑中得到圓角位圖
*
* @param url
* 字符串路徑
* @param pixels
* 圓角弧度
* @return 圓角位圖
* @throws Exception
* 異常
*/
public static Bitmap getRoundBitmapFromUrl(String url, int pixels)
throws Exception {
byte[] bytes = getBytesFromUrl(url);
Bitmap bitmap = byteToBitmap(bytes);
return toRoundCorner(bitmap, pixels);
}
/**
* 從路徑中得到圓角Drawable對象
*
* @param url
* 字符串路徑
* @param pixels
* 圓角弧度
* @return 圓角Drawable對象
* @throws Exception
* 異常
*/
public static Drawable geRoundDrawableFromUrl(String url, int pixels)
throws Exception {
byte[] bytes = getBytesFromUrl(url);
BitmapDrawable bitmapDrawable = (BitmapDrawable) byteToDrawable(bytes);
return toRoundCorner(bitmapDrawable, pixels);
}
/**
* 從路徑中得到二進制數據
*
* @param url
* 字符串路徑
* @return 二進制數據
* @throws Exception
* 異常
*/
public static byte[] getBytesFromUrl(String url) throws Exception {
return readInputStream(getRequest(url));
}
/**
* 從二進制數據中得到位圖
*
* @param byteArray
* 二進制數據
* @return 位圖
*/
public static Bitmap byteToBitmap(byte[] byteArray) {
if (byteArray.length != 0) {
return BitmapFactory
.decodeByteArray(byteArray, 0, byteArray.length);
} else {
return null;
}
}
/**
* 從二進制數據中得到Drawable對象
*
* @param byteArray
* 二進制數據
* @return Drawable對象
*/
public static Drawable byteToDrawable(byte[] byteArray) {
ByteArrayInputStream ins = new ByteArrayInputStream(byteArray);
return Drawable.createFromStream(ins, null);
}
/**
* 把位圖轉換稱二進制數據
*
* @param bm
* 位圖
* @return 二進制數據
*/
public static byte[] Bitmap2Bytes(Bitmap bm) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
return baos.toByteArray();
}
/**
* 把Drawable對象轉換稱位圖
*
* @param drawable
* Drawable對象
* @return 位圖
*/
public static Bitmap drawableToBitmap(Drawable drawable) {
Bitmap bitmap = Bitmap
.createBitmap(
drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight(),
drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
: Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight());
drawable.draw(canvas);
return bitmap;
}
/**
* 圖片去色,返回灰度圖片
*
* @param bmpOriginal
* 傳入的圖片
* @return 去色後的圖片
*/
public static Bitmap toGrayscale(Bitmap bmpOriginal) {
int width, height;
height = bmpOriginal.getHeight();
width = bmpOriginal.getWidth();
Bitmap bmpGrayscale = Bitmap.createBitmap(width, height,
Bitmap.Config.RGB_565);
Canvas c = new Canvas(bmpGrayscale);
Paint paint = new Paint();
ColorMatrix cm = new ColorMatrix();
cm.setSaturation(0);
ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
paint.setColorFilter(f);
c.drawBitmap(bmpOriginal, 0, 0, paint);
return bmpGrayscale;
}
/**
* 去色同時加圓角
*
* @param bmpOriginal
* 原圖
* @param pixels
* 圓角弧度
* @return 修改後的圖片
*/
public static Bitmap toGrayscale(Bitmap bmpOriginal, int pixels) {
return toRoundCorner(toGrayscale(bmpOriginal), pixels);
}
/**
* 把位圖變成圓角位圖
*
* @param bitmap
* 需要修改的位圖
* @param pixels
* 圓角的弧度
* @return 圓角位圖
*/
public static Bitmap toRoundCorner(Bitmap bitmap, int pixels) {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = new RectF(rect);
final float roundPx = pixels;
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}
/**
* 將BitampDrawable轉換成圓角的BitampDrawable
*
* @param bitmapDrawable
* 原生BitampDrawable對象
* @param pixels
* 圓角弧度
* @return 圓角BitampDrawable對象
*/
public static BitmapDrawable toRoundCorner(BitmapDrawable bitmapDrawable,
int pixels) {
Bitmap bitmap = bitmapDrawable.getBitmap();
bitmapDrawable = new BitmapDrawable(toRoundCorner(bitmap, pixels));
return bitmapDrawable;
}
/**
* 圖片水印生成的方法
*
* @param src
* 源圖片位圖
* @param watermark
* 水印圖片位圖
* @return 返回一個加了水印的圖片
*/
public static Bitmap createBitmap(Bitmap src, Bitmap watermark) {
if (src == null)
return null;
int w = src.getWidth();
int h = src.getHeight();
int ww = watermark.getWidth();
int wh = watermark.getHeight();
Bitmap newb = Bitmap.createBitmap(w, h, Config.ARGB_8888);// 創建一個新的和SRC長度寬度一樣的位圖
Canvas cv = new Canvas(newb);// 初始化畫布
cv.drawBitmap(src, 0, 0, null);// 在 0,0坐標開始畫入src
cv.drawBitmap(watermark, w - ww + 5, h - wh + 5, null);// 在src的右下角畫入水印
cv.save(Canvas.ALL_SAVE_FLAG);// 保存,用來保存Canvas的狀態。save之後,可以調用Canvas的平移、放縮、旋轉、錯切、裁剪等操作。
cv.restore();// 存儲,用來恢復Canvas之前保存的狀態。防止save後對Canvas執行的操作對後續的繪制有影響。
return newb;
}