Image图片处理总结

来源:互联网 发布:ros通过ip绑定mac地址 编辑:程序博客网 时间:2024/05/22 19:12

忙了半年终于闲下来了一会,整理了一下图片处理过程中遇到的问题

/**

* 根据输入指定的圆角,获取Bitmap资源

* @param bitmap
* @param roundPX
* @return
*/
public static Bitmap getRoundCornerBitmap(Bitmap bitmap, float roundPX) {
try {
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;
} catch (Exception e) {
return bitmap;
}}


/**
* dp转换成像素

* @param context
* @param dp
* @return
*/
public static int Dp2Px(Context context, float dp) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dp * scale + 0.5f);
}


/**
* 像素转成dp

* @param context
* @param px
* @return
*/
public static int Px2Dp(Context context, float px) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (px / scale + 0.5f);
}


/**
* 图片模块,图片详情也页填充图片。放大比例后,从左上角开始切割

* @param bitmap
* @param windowWith
* @return
*/
public static Bitmap getFullWindownBitmap(Bitmap bitmap, int windowWith) {
float scaleWidth = 1;
float scaleHeight = 1;
try {
int bmpWidth = bitmap.getWidth();
int bmpHeight = bitmap.getHeight();
/* 放大变量 */
double scale = 1.1;
if (bmpWidth < windowWith) {
scale = windowWith / bmpWidth;
scale += 0.5;
}
/* 放大以后的宽高,一定要强制转换为float型的 */
scaleWidth = (float) (scaleWidth * scale);
scaleHeight = (float) (scaleHeight * scale);


/* 产生resize后的Bitmap对象 */
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
Bitmap resizeBmp = Bitmap.createBitmap(bitmap, 0, 0, bmpWidth,
bmpWidth / 16 * 10, matrix, true);
if (resizeBmp != null) {
return resizeBmp;
} else {
return bitmap;
}


} catch (OutOfMemoryError e) {
System.gc();
return bitmap;
} catch (Exception e) {
System.gc();
return bitmap;
}
}/**
* 圆形图片切割,解决大图片方形图片

* @param bitmap
* @return
*/
public static Bitmap getCornerBitmap(Bitmap bitmap) {
if (bitmap == null) {
return null;
}
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;

}

/**
* 获得倒影图片,图片资源不能过大

* @param bitmap
* @return
*/
public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap) {
try {


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;
} catch (OutOfMemoryError e) {
// TODO: handle exception
System.gc();
return bitmap;
}
}

/**
* 通过缩放图片改变图片的大小,图片的宽高可以方法输入

* @param bitMap
* @return
*/
private Bitmap setBitmapSize(Bitmap bitMap) {
int width = bitMap.getWidth();
int height = bitMap.getHeight();
// 设置想要的大小
int newWidth = 500;
int newHeight = 400;
// 计算缩放比例
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);
return bitMap;
}


@SuppressLint("NewApi")
public int getBitmapSize(Bitmap bitmap) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { // API 19
return bitmap.getAllocationByteCount();
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1) {// API
// 12
return bitmap.getByteCount();
}
return bitmap.getRowBytes() * bitmap.getHeight(); // earlier version
}

0 0