文章目录
本文收录常用工具类。
系列文章:
根据原图和变长绘制圆形图片
/**
* 根据原图和变长绘制圆形图片
*
* @param source
* @param min
* @return
*/
public static Bitmap createCircleImage(Bitmap source, int min) {
final Paint paint = new Paint();
paint.setAntiAlias(true);
Bitmap target = Bitmap.createBitmap(min, min, Bitmap.Config.ARGB_8888);
/**
* 产生一个同样大小的画布
*/
Canvas canvas = new Canvas(target);
/**
* 首先绘制圆形
*/
canvas.drawCircle(min / 2, min / 2, min / 2, paint);
/**
* 使用SRC_IN
*/
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
/**
* 绘制图片
*/
canvas.drawBitmap(source, 0, 0, paint);
return target;
}
得到有一定透明度的图片
/**
* 得到有一定透明度的图片
*
* @param sourceImg
* @param number
* @return
*/
public static Bitmap getTransparentBitmap(Bitmap sourceImg, int number) {
if (sourceImg == null) {
return null;
}
int[] argb = new int[sourceImg.getWidth() * sourceImg.getHeight()];
sourceImg.getPixels(argb, 0, sourceImg.getWidth(), 0, 0, sourceImg
.getWidth(), sourceImg.getHeight());// 获得图片的ARGB值
number = number * 255 / 100;
for (int i = 0; i < argb.length; i++) {
argb[i] = (number << 24) | (argb[i] & 0x00FFFFFF);
}
sourceImg = Bitmap.createBitmap(argb, sourceImg.getWidth(), sourceImg
.getHeight(), Bitmap.Config.ARGB_8888);
return sourceImg;
}
保存 bitmap 图片到指定路径
/**
* 保存bitmap文件到指定路径
*
* @param mBitmap
* @param path
* @return
*/
public static boolean saveMyBitmap(Bitmap mBitmap, String path) {
if (mBitmap == null) {
return false;
}
File f = new File(path);
FileOutputStream fOut = null;
Log.e("saveMyBitmap", "File Path:" + f.getAbsolutePath());
try {
fOut = new FileOutputStream(f);
mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
fOut.flush();
fOut.close();
return true;
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
生成 bitmap文件
/**
* 生成bitmap文件
*
* @param context
* @param resId
* @return
*/
public static Bitmap getBitmap(Context context, int resId) {
Resources res = context.getResources();
Bitmap bmp = BitmapFactory.decodeResource(res, resId);
return bmp;
}
计算出原始图片的长度和宽度
/**
* 计算出原始图片的长度和宽度
*
* @param options
* @param minSideLength
* @param maxNumOfPixels
* @return
*/
private static int computeInitialSampleSize(BitmapFactory.Options options, int minSideLength, int maxNumOfPixels) {
double w = options.outWidth;
double h = options.outHeight;
int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math
.sqrt(w * h / maxNumOfPixels));
int upperBound = (minSideLength == -1) ? 128 : (int) Math.min(
Math.floor(w / minSideLength), Math.floor(h / minSideLength));
if (upperBound < lowerBound) {
return lowerBound;
}
if ((maxNumOfPixels == -1) && (minSideLength == -1)) {
return 1;
} else if (minSideLength == -1) {
return lowerBound;
} else {
return upperBound;
}
}
bitmap 转字节数组
/**
* bitmap转字节数组
*
* @param bm
* @return
*/
public static byte[] Bitmap2Bytes(Bitmap bm) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
return baos.toByteArray();
}
字节数组转 bitmap
/**
* 字节数组转bitmap
*
* @param b
* @return
*/
public static Bitmap Bytes2Bimap(byte[] b) {
if (b.length != 0) {
return BitmapFactory.decodeByteArray(b, 0, b.length);
} else {
return null;
}
}
从文件路径中获取 bitmap
/**
* 从文件路径中获取bitmap
*
* @param path
* @return
*/
public static Bitmap getBitmapFromFile(String path) {
Bitmap bitmap = null;
try {
FileInputStream fis = new FileInputStream(path);
bitmap = BitmapFactory.decodeStream(fis);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return bitmap;
}
drawable 转 bitmap
/**
* drawable转bitmap
*
* @param drawable
* @return
*/
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;
}
drawable 缩放
/**
* drawable缩放
*
* @param drawable
* @param w
* @param h
* @return
*/
public static Drawable zoomDrawable(Drawable drawable, int w, int h) {
int width = drawable.getIntrinsicWidth();
int height = drawable.getIntrinsicHeight();
// drawable转换成bitmap
Bitmap oldbmp = drawableToBitmap(drawable);
// 创建操作图片用的Matrix对象
Matrix matrix = new Matrix();
// 计算缩放比例
float sx = ((float) w / width);
float sy = ((float) h / height);
// 设置缩放比例
matrix.postScale(sx, sy);
// 建立新的bitmap,其内容是对原bitmap的缩放后的图
Bitmap newbmp = Bitmap.createBitmap(oldbmp, 0, 0, width, height,
matrix, true);
return new BitmapDrawable(newbmp);
}
生成缩略图
/**
* 可用于生成缩略图
* Creates a centered bitmap of the desired size. Recycles the input.
*
* @param source
*/
public static Bitmap extractMiniThumb(Bitmap source, int width, int height) {
return extractMiniThumb(source, width, height, true);
}
生成二维码
/**
* 生成二维码
*
* @param activity
* @param content
* @return
*/
public static Bitmap generateQRCode(Activity activity, String content) {
try {
int width = AndroidUtil.getDisplayMetrics(activity).widthPixels * 4 / 5;
QRCodeWriter writer = new QRCodeWriter();
// MultiFormatWriter writer = new MultiFormatWriter();
BitMatrix matrix = writer.encode(content, BarcodeFormat.QR_CODE,
width, width);
return bitMatrix2Bitmap(matrix);
} catch (WriterException e) {
e.printStackTrace();
}
return null;
}
位阵 bitMatrix 转位图
/**
* 位阵bitMatrix转位图
*
* @param matrix
* @return
*/
private static Bitmap bitMatrix2Bitmap(BitMatrix matrix) {
int w = matrix.getWidth();
int h = matrix.getHeight();
int[] rawData = new int[w * h];
for (int i = 0; i < w; i++) {
for (int j = 0; j < h; j++) {
int color = Color.WHITE;
if (matrix.get(i, j)) {
color = Color.BLACK;
}
rawData[i + (j * w)] = color;
}
}
Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.RGB_565);
bitmap.setPixels(rawData, 0, w, 0, 0, w, h);
return bitmap;
}
得到新尺寸的 bitmap
/**
* 得到新尺寸的 bitmap
*
* @param bm
* @param newHeight
* @param newWidth
* @return
*/
public static Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
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);
return Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
}
从资源文件中生成 bitmap
/**
* 从资源文件中生成bitmap
*
* @param mContext
* @param resId
* @param reqWidth
* @param reqHeight
* @return
*/
public static Bitmap decodeBitmapFromResource(Context mContext, @DrawableRes int resId,int reqWidth, int reqHeight) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(mContext.getResources(), resId, options);
options.inSampleSize = calculateInSampleSize(mContext, resId, reqWidth, reqHeight);
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(mContext.getResources(), resId, options);
}
获取网络图片资源
/**
* 获取网络图片资源
*
* @param url
* @return
*/
public static Bitmap getHttpBitmap(String url) {
URL myFileURL;
Bitmap bitmap = null;
try {
myFileURL = new URL(url);
//获得连接
HttpURLConnection conn = (HttpURLConnection) myFileURL.openConnection();
//设置超时时间为6000毫秒,conn.setConnectionTiem(0);表示没有时间限制
conn.setConnectTimeout(6000);
//连接设置获得数据流
conn.setDoInput(true);
//不使用缓存
conn.setUseCaches(false);
//这句可有可无,没有影响
//conn.connect();
//得到数据流
InputStream is = conn.getInputStream();
//解析得到图片
bitmap = BitmapFactory.decodeStream(is);
//关闭数据流
is.close();
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}