Bitmap操作
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.Base64;
public class BitmapUtils {
/**
* convert Bitmap to byte array
*
* @param b
* @return
*/
public static byte[] bitmapToByte(Bitmap b) {
if (b == null) {
return null;
}
ByteArrayOutputStream o = new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.PNG, 100, o);
return o.toByteArray();
}
/**
* convert byte array to Bitmap
*
* @param b
* @return
*/
public static Bitmap byteToBitmap(byte[] b) {
return (b == null || b.length == 0) ? null : BitmapFactory
.decodeByteArray(b, 0, b.length);
}
/**
* convert Drawable to Bitmap
*
* @param d
* @return
*/
public static Bitmap drawableToBitmap(Drawable d) {
return d == null ? null : ((BitmapDrawable) d).getBitmap();
}
/**
* convert Bitmap to Drawable
*
* @param b
* @return
*/
public static Drawable bitmapToDrawable(Bitmap b) {
return b == null ? null : new BitmapDrawable(b);
}
/**
* convert Drawable to byte array
*
* @param d
* @return
*/
public static byte[] drawableToByte(Drawable d) {
return bitmapToByte(drawableToBitmap(d));
}
/**
* convert byte array to Drawable
*
* @param b
* @return
*/
public static Drawable byteToDrawable(byte[] b) {
return bitmapToDrawable(byteToBitmap(b));
}
/**
* 获得圆角图片
*
* @author huxj
* @param bitmap
* Bitmap资源
* @param roundPx
* 弧度
* @return Bitmap 圆角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 void saveBitmapToFile(Bitmap bmp, String dir, String path) {
File file = new File(dir);
if (!file.isDirectory()) {
file.mkdirs();
}
File f = new File(path);
try {
f.createNewFile();
FileOutputStream fOut = null;
fOut = new FileOutputStream(f);
bmp.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/** 读取本地图片文件,压缩至合适宽高,返回bitmap */
public static Bitmap getBitmapFromFile(String path, int mScreenWidth,
int mScreenHeight) {
if (path != null) {
BitmapFactory.Options options = null;
InputStream is = null;
try {
options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
is = new FileInputStream(path);
BitmapFactory.decodeStream(is, null, options);
int w = options.outWidth;
int h = options.outHeight;
options = new BitmapFactory.Options();
options.inSampleSize = Math.max((int) (w / mScreenWidth), h
/ mScreenHeight);
options.inJustDecodeBounds = false;
is.close();
is = new FileInputStream(path);
return BitmapFactory.decodeStream(is, null, options);
} catch (Throwable e) {
e.printStackTrace();
} finally {
try {
if (is != null)
is.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
return null;
}
/** 读取本地图片,并压缩scale倍,返回bitmap */
public static Bitmap getScaleBitmapFromFile(String path, int scale) {
if (path != null) {
try {
BitmapFactory.Options options = new BitmapFactory.Options();
InputStream is = new FileInputStream(path);
options = new BitmapFactory.Options();
options.inSampleSize = scale;
options.inJustDecodeBounds = false;
is.close();
is = new FileInputStream(path);
return BitmapFactory.decodeStream(is, null, options);
} catch (Throwable e) {
e.printStackTrace();
}
}
return null;
}
/** 读取资源图片,返回bitmap */
public static Bitmap getBitmapForResource(Context context, int resourceId,
int mScreenWidth, int mScreenHeight) {
try {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(context.getResources(), resourceId,
options);
int w = options.outWidth;
int h = options.outHeight;
options = new BitmapFactory.Options();
options.inSampleSize = Math.max((int) (w / mScreenWidth), h
/ mScreenHeight);
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(context.getResources(),
resourceId, options);
} catch (Throwable e) {
e.printStackTrace();
}
return null;
}
@SuppressLint("NewApi")
public static Bitmap stringtoBitmap(String string) {
Bitmap bitmap = null;
try {
byte[] bitmapArray;
bitmapArray = Base64.decode(string, Base64.DEFAULT);
bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0,
bitmapArray.length);
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}
/**
* 处理图片
*
* @param bm
* 所要转换的bitmap
* @param newWidth新的宽
* @param newHeight新的高
* @return 指定宽高的bitmap
*/
public static Bitmap scaleImg(Bitmap bm, int newWidth, int newHeight) {
int width = bm.getWidth();
int height = bm.getHeight();
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
Bitmap newbm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix,
true);
return newbm;
}
/**
* 处理图片
*
* @param bm
* 所要转换的bitmap ,按宽度保持比例
* @param newWidth新的宽
* @return 指定宽的bitmap
*/
public static Bitmap scaleImg(Bitmap bm, int newWidth) {
int width = bm.getWidth();
int height = bm.getHeight();
float scaleWidth = ((float) newWidth) / width;
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleWidth);
Bitmap newbm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix,
true);
return newbm;
}
}