一些操作图片的方法,直接上代码:
/**
* 将彩色图转换为灰度图
*
* @param img
* 位图
* @return 返回转换好的位图
*/
public static Bitmap convertGreyImg(Bitmap img) {
int width = img.getWidth(); // 获取位图的宽
int height = img.getHeight(); // 获取位图的高
int[] pixels = new int[width * height]; // 通过位图的大小创建像素点数组
img.getPixels(pixels, 0, width, 0, 0, width, height);
int alpha = 0xFF << 24;
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
int grey = pixels[width * i + j];
int red = ((grey & 0x00FF0000) >> 16);
int green = ((grey & 0x0000FF00) >> 8);
int blue = (grey & 0x000000FF);
grey = (int) ((float) red * 0.3 + (float) green * 0.59 + (float) blue * 0.11);
grey = alpha | (grey << 16) | (grey << 8) | grey;
pixels[width * i + j] = grey;
}
}
Bitmap result = Bitmap.createBitmap(width, height, Config.RGB_565);
result.setPixels(pixels, 0, width, 0, 0, width, height);
return result;
}
// 将一个图片切割成多个图片
public static List<ImagePiece> split(Bitmap bitmap, int xPiece, int yPiece) {
List<ImagePiece> pieces = new ArrayList<ImagePiece>(xPiece * yPiece);
int width = bitmap.getWidth();
int height = bitmap.getHeight();
int pieceWidth = width / 3;
int pieceHeight = height / 3;
for (int i = 0; i < yPiece; i++) {
for (int j = 0; j < xPiece; j++) {
ImagePiece piece = new ImagePiece();
piece.index = j + i * xPiece;
int xValue = j * pieceWidth;
int yValue = i * pieceHeight;
piece.bitmap = Bitmap.createBitmap(bitmap, xValue, yValue, pieceWidth, pieceHeight);
pieces.add(piece);
}
}
return pieces;
}
// 图片Url保存为位图并进行缩放操作
// 通过传入图片url获取位图方法
public static Bitmap returnBitMap(String url) {
URL myFileUrl = null;
Bitmap bitmap = null;
try {
myFileUrl = new URL(url);
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
bitmap = BitmapFactory.decodeStream(is);
is.close();
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}
// 通过传入位图,新的宽.高比进行位图的缩放操作
public static Drawable resizeImage(Bitmap bitmap, int w, int h) {
// load the origial Bitmap
Bitmap BitmapOrg = bitmap;
int width = BitmapOrg.getWidth();
int height = BitmapOrg.getHeight();
int newWidth = w;
int newHeight = h;
// calculate the scale
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// create a matrix for the manipulation
Matrix matrix = new Matrix();
// resize the Bitmap
matrix.postScale(scaleWidth, scaleHeight);
// if you want to rotate the Bitmap
// matrix.postRotate(45);
// recreate the new Bitmap
Bitmap resizedBitmap = Bitmap.createBitmap(BitmapOrg, 0, 0, width, height, matrix, true);
// make a Drawable from Bitmap to allow to set the Bitmap
// to the ImageView, ImageButton or what ever
return new BitmapDrawable(resizedBitmap);
}
public static Bitmap zoomBitmap(Bitmap bitmap, int w, int h) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
Matrix matrix = new Matrix();
float scaleWidht = ((float) w / width);
float scaleHeight = ((float) h / height);
matrix.postScale(scaleWidht, scaleHeight);
Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
return newbmp;
}
/**
* 将Drawable转化为Bitmap
*/
public static Bitmap drawableToBitmap(Drawable drawable) {
int width = drawable.getIntrinsicWidth();
int height = drawable.getIntrinsicHeight();
Bitmap bitmap = Bitmap.createBitmap(width, height,
drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, width, height);
drawable.draw(canvas);
return bitmap;
}
/**
* 获得圆角图片的方法
*/
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx) {
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);
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;
}
/**
* 获得带倒影的图片方法
*/
public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap) {
final int reflectionGap = 4;
int width = bitmap.getWidth();
int height = bitmap.getHeight();
Matrix matrix = new Matrix();
matrix.preScale(1, -1);
Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, height / 2, width, height / 2, matrix, false);
Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height / 2), Config.ARGB_8888);
Canvas canvas = new Canvas(bitmapWithReflection);
canvas.drawBitmap(bitmap, 0, 0, null);
Paint deafalutPaint = new Paint();
canvas.drawRect(0, height, width, height + reflectionGap, deafalutPaint);
canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);
Paint paint = new Paint();
LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0, bitmapWithReflection.getHeight() + reflectionGap,
0x70ffffff, 0x00ffffff, TileMode.CLAMP);
paint.setShader(shader);
// Set the Transfer mode to be porter duff and destination in
paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
// Draw a rectangle using the paint with our linear gradient
canvas.drawRect(0, height, width, bitmapWithReflection.getHeight() + reflectionGap, paint);
return bitmapWithReflection;
}
private byte[] Bitmap2Bytes(Bitmap bm) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
return baos.toByteArray();
}
private Bitmap Bytes2Bimap(byte[] b) {
if (b.length != 0) {
return BitmapFactory.decodeByteArray(b, 0, b.length);
} else {
return null;
}
}
/**
* create the bitmap from a byte array 生成水印图片
*
* @param src
* the bitmap object you want proecss
* @param watermark
* the water mark above the src
* @return return a bitmap object ,if paramter's length is 0,return null
*/
private Bitmap createBitmap(Bitmap src, Bitmap watermark) {
String tag = "createBitmap";
if (src == null) {
return null;
}
int w = src.getWidth();
int h = src.getHeight();
int ww = watermark.getWidth();
int wh = watermark.getHeight();
// create the new blank bitmap
Bitmap newb = Bitmap.createBitmap(w, h, Config.ARGB_8888);// 创建一个新的和SRC长度宽度一样的位图
Canvas cv = new Canvas(newb);
// draw src into
cv.drawBitmap(src, 0, 0, null);// 在 0,0坐标开始画入src
// draw watermark into
cv.drawBitmap(watermark, w - ww + 5, h - wh + 5, null);// 在src的右下角画入水印
// save all clip
cv.save(Canvas.ALL_SAVE_FLAG);// 保存
// store
cv.restore();// 存储
return newb;
}
/**
* 重新编码Bitmap
*
* @param src
* 需要重新编码的Bitmap
*
* @param format
* 编码后的格式(目前只支持png和jpeg这两种格式)
*
* @param quality
* 重新生成后的bitmap的质量
*
* @return 返回重新生成后的bitmap
*/
private static Bitmap codec(Bitmap src, Bitmap.CompressFormat format, int quality) {
ByteArrayOutputStream os = new ByteArrayOutputStream();
src.compress(format, quality, os);
byte[] array = os.toByteArray();
return BitmapFactory.decodeByteArray(array, 0, array.length);
}
// Stream转换成Byte
static byte[] streamToBytes(InputStream is) {
ByteArrayOutputStream os = new ByteArrayOutputStream(1024);
byte[] buffer = new byte[1024];
int len;
try {
while ((len = is.read(buffer)) >= 0) {
os.write(buffer, 0, len);
}
} catch (java.io.IOException e) {
}
return os.toByteArray();
}
// 把View转换成Bitmap
/**
* 把一个View的对象转换成bitmap
*/
static Bitmap getViewBitmap(View v) {
v.clearFocus();
v.setPressed(false);
// 能画缓存就返回false
boolean willNotCache = v.willNotCacheDrawing();
v.setWillNotCacheDrawing(false);
int color = v.getDrawingCacheBackgroundColor();
v.setDrawingCacheBackgroundColor(0);
if (color != 0) {
v.destroyDrawingCache();
}
v.buildDrawingCache();
Bitmap cacheBitmap = v.getDrawingCache();
if (cacheBitmap == null) {
return null;
}
Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);
// Restore the view
v.destroyDrawingCache();
v.setWillNotCacheDrawing(willNotCache);
v.setDrawingCacheBackgroundColor(color);
return bitmap;
}
/***
* 图片切圆角,方向自由.
*/
public static final int ALL = 347120;
public static final int TOP = 547120;
public static final int LEFT = 647120;
public static final int RIGHT = 747120;
public static final int BOTTOM = 847120;
public static Bitmap fillet(int type,Bitmap bitmap,int roundPx) {
try {
// 其原理就是:先建立一个与图片大小相同的透明的Bitmap画板
// 然后在画板上画出一个想要的形状的区域。
// 最后把源图片帖上。
final int width = bitmap.getWidth();
final int height = bitmap.getHeight();
Bitmap paintingBoard = Bitmap.createBitmap(width,height, Config.ARGB_8888);
Canvas canvas = new Canvas(paintingBoard);
canvas.drawARGB(Color.TRANSPARENT, Color.TRANSPARENT, Color.TRANSPARENT, Color.TRANSPARENT);
final Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.BLACK);
if( TOP == type ){
clipTop(canvas,paint,roundPx,width,height);
}else if( LEFT == type ){
clipLeft(canvas,paint,roundPx,width,height);
}else if( RIGHT == type ){
clipRight(canvas,paint,roundPx,width,height);
}else if( BOTTOM == type ){
clipBottom(canvas,paint,roundPx,width,height);
}else{
clipAll(canvas,paint,roundPx,width,height);
}
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
//帖子图
final Rect src = new Rect(0, 0, width, height);
final Rect dst = src;
canvas.drawBitmap(bitmap, src, dst, paint);
return paintingBoard;
} catch (Exception exp) {
return bitmap;
}
}
private static void clipLeft(final Canvas canvas,final Paint paint,int offset,int width,int height){
final Rect block = new Rect(offset,0,width,height);
canvas.drawRect(block, paint);
final RectF rectF = new RectF(0, 0, offset * 2 , height);
canvas.drawRoundRect(rectF, offset, offset, paint);
}
private static void clipRight(final Canvas canvas,final Paint paint,int offset,int width,int height){
final Rect block = new Rect(0, 0, width-offset, height);
canvas.drawRect(block, paint);
final RectF rectF = new RectF(width - offset * 2, 0, width , height);
canvas.drawRoundRect(rectF, offset, offset, paint);
}
private static void clipTop(final Canvas canvas,final Paint paint,int offset,int width,int height){
final Rect block = new Rect(0, offset, width, height);
canvas.drawRect(block, paint);
final RectF rectF = new RectF(0, 0, width , offset * 2);
canvas.drawRoundRect(rectF, offset, offset, paint);
}
private static void clipBottom(final Canvas canvas,final Paint paint,int offset,int width,int height){
final Rect block = new Rect(0, 0, width, height - offset);
canvas.drawRect(block, paint);
final RectF rectF = new RectF(0, height - offset * 2 , width , height);
canvas.drawRoundRect(rectF, offset, offset, paint);
}
private static void clipAll(final Canvas canvas,final Paint paint,int offset,int width,int height){
final RectF rectF = new RectF(0, 0, width , height);
canvas.drawRoundRect(rectF, offset, offset, paint);
}
1、从资源中获取bitmap
Resources res = getResources();
Bitmap bmp = BitmapFactory.decodeResource(res, R.drawable.icon);
2.Bitmap 转byte[]
public static byte[] Bitmap2Bytes(Bitmap bm) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
return baos.toByteArray();
}
3.byte[] 转Bitmap
public static Bitmap Bytes2Bimap(byte[] b) {
if (b.length != 0) {
return BitmapFactory.decodeByteArray(b, 0, b.length);
} else {
return null;
}
}
4.Drawable转Bitmap
public static Bitmap drawableToBitmap(Drawable drawable) {
// 取 drawable 的长宽
int w = drawable.getIntrinsicWidth();
int h = drawable.getIntrinsicHeight();
// 取 drawable 的颜色格式
Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
: Bitmap.Config.RGB_565;
// 建立对应 bitmap
Bitmap bitmap = Bitmap.createBitmap(w, h, config);
// 建立对应 bitmap 的画布
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, w, h);
// 把 drawable 内容画到画布中
drawable.draw(canvas);
return bitmap;
}
原创: http://dyh7077063.iteye.com/blog/970672