1.将图片转换成圆形
/**
* 转换图片成圆形
* @param bitmap 传入Bitmap对象
* @return
*/
public static Bitmap toRoundBitmap(Bitmap bitmap) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
float roundPx;
float left,top,right,bottom,dst_left,dst_top,dst_right,dst_bottom;
if (width <= height) {
roundPx = width / 2;
top = 0;
bottom = width;
left = 0;
right = width;
height = width;
dst_left = 0;
dst_top = 0;
dst_right = width;
dst_bottom = width;
} else {
roundPx = height / 2;
float clip = (width - height) / 2;
left = clip;
right = width - clip;
top = 0;
bottom = height;
width = height;
dst_left = 0;
dst_top = 0;
dst_right = height;
dst_bottom = height;
}
Bitmap output = Bitmap.createBitmap(width,
height, Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect src = new Rect((int)left, (int)top, (int)right, (int)bottom);
final Rect dst = new Rect((int)dst_left, (int)dst_top, (int)dst_right, (int)dst_bottom);
final RectF rectF = new RectF(dst);
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, src, dst, paint);
return output;
}
2.将图片缩小后变成圆形(图片本身会被修改)
/**
* 处理图片(缩小、变成圆形)
* @return 返回处理后的图片
*/
public static Bitmap HandlePhotoFromCamera(){
BitmapFactory.Options op = new BitmapFactory.Options();
op.inSampleSize = 6; // 这个数字越大,图片大小越小.
// 设置图片的大小
Bitmap bitMap = BitmapFactory.decodeFile(BitmapUtil.PHOTONAME);
int width = bitMap.getWidth();
int height = bitMap.getHeight();
// 设置想要的大小
int newWidth = 250;
int newHeight = 250;
// 计算缩放比例
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// 取得想要缩放的matrix参数
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
// 得到新的图片
//bitMap = Bitmap.createBitmap(bitMap, 0, 0, width, height,
// matrix, true);
// canvas.drawBitmap(bitMap, 0, 0, paint)
Bitmap pic = null;
pic = BitmapUtil.toRoundBitmap(BitmapFactory.decodeFile(BitmapUtil.PHOTONAME, op));//将图片变成圆形
FileOutputStream b = null;
;
try {
b = new FileOutputStream(BitmapUtil.PHOTONAME);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
if (pic != null) {
pic.compress(Bitmap.CompressFormat.JPEG, 30, b);
}
return pic;
}
3 bitmap与string转换以及base64编码
//// 将Bitmap转换成字符串
public String bitmaptoString(Bitmap bitmap) {
String string = null;
ByteArrayOutputStream bStream = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 100, bStream);
byte[] bytes = bStream.toByteArray();
string = Base64.encodeToString(bytes, Base64.DEFAULT);
return string;
}
public Bitmap stringtoBitmap(String string) {
// 将字符串转换成Bitmap类型
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;
}
//将bytep数组按base64 编码
public static String bytesToBase64(byte[] data) {
if (data == null) {
return null;
}
return Base64.encodeToString(data, Base64.DEFAULT);
}
4 根据本地Uri获取bitmap对象后处理
private Bitmap getBitmapFromUri(Uri uri) {
Bitmap bitmap = null;
try {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
bitmap = BitmapFactory.decodeStream(getContentResolver()
.openInputStream(uri), null, options);
int picWidth = options.outWidth;
int picHeight = options.outHeight;
WindowManager windowManager = getWindowManager();
Display display = windowManager.getDefaultDisplay();
int screenWidth = display.getWidth();
int screenHeight = display.getHeight();
options.inSampleSize = 1;
if (picWidth > picHeight) {
if (picWidth > screenWidth)
options.inSampleSize = picWidth / screenWidth;
} else {
if (picHeight > screenHeight)
options.inSampleSize = picHeight / screenHeight;
}
options.inJustDecodeBounds = false;
bitmap = BitmapFactory.decodeStream(getContentResolver()
.openInputStream(uri), null, options);
} catch (Exception e) {
e.printStackTrace();
return null;
}
return bitmap;
}
5.将图片压缩后转换成byte数组
ByteArrayOutputStream baos = new ByteArrayOutputStream();
fianBitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);// 100不压缩
byte[] bitmapByte = baos.toByteArray();
之后转换成base64编码即可上传到服务器
6.从字节数组中获取bitmap
public static Bitmap getBitmapFromBytes(byte[] data) {
if (data == null) {
return null;
}
return BitmapFactory.decodeByteArray(data, 0, data.length);// 从字节数组解码位图
}