package imageUtil;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream; import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader.TileMode;
import android.graphics.drawable.Drawable;
import android.util.Log; public class ImageUtil {
// 放大缩小图片
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, , , 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(, , width, height);
drawable.draw(canvas);
return bitmap; } public static String SaveBitmap(Bitmap bmp, String name) {
File file = new File("mnt/sdcard/picture/");
String path = null;
if (!file.exists())
file.mkdirs();
try {
path = file.getPath() + "/" + name;
FileOutputStream fileOutputStream = new FileOutputStream(path); bmp.compress(Bitmap.CompressFormat.JPEG, , fileOutputStream);
fileOutputStream.flush();
fileOutputStream.close();
System.out.println("saveBmp is here");
} catch (Exception e) {
e.printStackTrace();
} return path;
} public static String saveToLocal(Bitmap bm) {
String path = "/sdcard/test.jpg";
try {
FileOutputStream fos = new FileOutputStream(path);
bm.compress(CompressFormat.JPEG, , fos);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
} return path;
} // 获得圆角图片的方法
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(, , bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = new RectF(rect); paint.setAntiAlias(true);
canvas.drawARGB(, , , );
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 = ;
int width = bitmap.getWidth();
int height = bitmap.getHeight(); Matrix matrix = new Matrix();
matrix.preScale(, -); Bitmap reflectionImage = Bitmap.createBitmap(bitmap, , height / , width, height / , matrix, false); Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height / ), Config.ARGB_8888); Canvas canvas = new Canvas(bitmapWithReflection);
canvas.drawBitmap(bitmap, , , null);
Paint deafalutPaint = new Paint();
canvas.drawRect(, height, width, height + reflectionGap, deafalutPaint); canvas.drawBitmap(reflectionImage, , height + reflectionGap, null); Paint paint = new Paint();
LinearGradient shader = new LinearGradient(, bitmap.getHeight(), , 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(, height, width, bitmapWithReflection.getHeight() + reflectionGap, paint); return bitmapWithReflection;
} /**
* 读取资源图片
*
* @param context
* @param resId
* @return
*/
public static Bitmap readBitMap(Context context, int resId) {
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inPreferredConfig = Bitmap.Config.RGB_565;
opt.inPurgeable = true;
opt.inInputShareable = true;
// 获取资源图片
InputStream is = context.getResources().openRawResource(resId);
return BitmapFactory.decodeStream(is, null, opt);
} /**
* 从sd卡读取图片
*
* @param path
* @return
*/
public static Bitmap readBitMap(String path) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Config.ARGB_8888;
Bitmap bm = BitmapFactory.decodeFile(path, options);
return bm;
} /**
* 图片旋转
*
* @param bmp
* @param degree
* @return
*/
public static Bitmap postRotateBitamp(Bitmap bmp, float degree) {
// 获得Bitmap的高和宽
int bmpWidth = bmp.getWidth();
int bmpHeight = bmp.getHeight();
// 产生resize后的Bitmap对象
Matrix matrix = new Matrix();
matrix.postRotate(degree);
Bitmap resizeBmp = Bitmap.createBitmap(bmp, , , bmpWidth, bmpHeight, matrix, true);
return resizeBmp;
} // 图片翻转
public static Bitmap reverseBitmap(Bitmap bmp, int flag) {
float[] floats = null;
switch (flag) {
case : // 水平反转
floats = new float[] { -1f, 0f, 0f, 0f, 1f, 0f, 0f, 0f, 1f };
break;
case : // 垂直反转
floats = new float[] { 1f, 0f, 0f, 0f, -1f, 0f, 0f, 0f, 1f };
break;
} if (floats != null) {
Matrix matrix = new Matrix();
matrix.setValues(floats);
return Bitmap.createBitmap(bmp, , , bmp.getWidth(), bmp.getHeight(), matrix, true);
} return bmp;
} /**
* 组合涂鸦图片和源图片
*
* @param src
* 源图片
* @param watermark
* 涂鸦图片
* @return
*/
public static Bitmap doodle(Bitmap src, Bitmap watermark, int x, int y) {
// 另外创建一张图片
Bitmap newb = Bitmap.createBitmap(src.getWidth(), src.getHeight(), Config.ARGB_8888);// 创建一个新的和SRC长度宽度一样的位图
Canvas canvas = new Canvas(newb);
canvas.drawBitmap(src, , , null);// 在 0,0坐标开始画入原图片src
canvas.drawBitmap(watermark, (src.getWidth() - watermark.getWidth()) / ,
(src.getHeight() - watermark.getHeight()) / , null); // 涂鸦图片画到原图片中间位置
canvas.save(Canvas.ALL_SAVE_FLAG);
canvas.restore();
watermark.recycle();
watermark = null;
return newb;
} /**
* 图片上写文字
*
* @param src源图片
* @param msg文字
* @param x
* @param y
* @return
*/
public static Bitmap drawText(Bitmap src, String msg, int x, int y) {
// 另外创建一张图片
Canvas canvas = new Canvas(src);
Paint paint = new Paint();
paint.setColor(Color.RED);
canvas.drawText(msg, x, y, paint);
canvas.save(Canvas.ALL_SAVE_FLAG);
canvas.restore();
return src;
} public static Bitmap oldRemeber(Bitmap bmp) {
if (ImageCache.get("oldRemeber") != null) {
return ImageCache.get("oldRemeber");
}
// 速度测试
long start = System.currentTimeMillis();
int width = bmp.getWidth();
int height = bmp.getHeight();
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
int pixColor = ;
int pixR = ;
int pixG = ;
int pixB = ;
int newR = ;
int newG = ;
int newB = ;
int[] pixels = new int[width * height];
bmp.getPixels(pixels, , width, , , width, height);
for (int i = ; i < height; i++) {
for (int k = ; k < width; k++) {
pixColor = pixels[width * i + k];
pixR = Color.red(pixColor);
pixG = Color.green(pixColor);
pixB = Color.blue(pixColor);
newR = (int) (0.393 * pixR + 0.769 * pixG + 0.189 * pixB);
newG = (int) (0.349 * pixR + 0.686 * pixG + 0.168 * pixB);
newB = (int) (0.272 * pixR + 0.534 * pixG + 0.131 * pixB);
int newColor = Color.argb(, newR > ? : newR, newG > ? : newG, newB > ?
: newB);
pixels[width * i + k] = newColor;
}
} bitmap.setPixels(pixels, , width, , , width, height);
long end = System.currentTimeMillis();
Log.e("may", "used time=" + (end - start));
ImageCache.put("oldRemeber", bitmap);
return bitmap;
} /**
* 模糊效果
*
* @param bmp
* @return
*/
public static Bitmap blurImage(Bitmap bmp) {
if (ImageCache.get("blurImage") != null) {
return ImageCache.get("blurImage");
}
// 速度测试
long start = System.currentTimeMillis();
int width = bmp.getWidth();
int height = bmp.getHeight();
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565); int pixColor = ; int newR = ;
int newG = ;
int newB = ; int newColor = ; int[][] colors = new int[][];
for (int i = , length = width - ; i < length; i++) {
for (int k = , len = height - ; k < len; k++) {
for (int m = ; m < ; m++) {
int s = ;
int p = ;
switch (m) {
case :
s = i - ;
p = k - ;
break;
case :
s = i;
p = k - ;
break;
case :
s = i + ;
p = k - ;
break;
case :
s = i + ;
p = k;
break;
case :
s = i + ;
p = k + ;
break;
case :
s = i;
p = k + ;
break;
case :
s = i - ;
p = k + ;
break;
case :
s = i - ;
p = k;
break;
case :
s = i;
p = k;
}
pixColor = bmp.getPixel(s, p);
colors[m][] = Color.red(pixColor);
colors[m][] = Color.green(pixColor);
colors[m][] = Color.blue(pixColor);
} for (int m = ; m < ; m++) {
newR += colors[m][];
newG += colors[m][];
newB += colors[m][];
} newR = (int) (newR / 9F);
newG = (int) (newG / 9F);
newB = (int) (newB / 9F); newR = Math.min(, Math.max(, newR));
newG = Math.min(, Math.max(, newG));
newB = Math.min(, Math.max(, newB)); newColor = Color.argb(, newR, newG, newB);
bitmap.setPixel(i, k, newColor); newR = ;
newG = ;
newB = ;
}
}
long end = System.currentTimeMillis();
Log.e("blurImage()", "used time=" + (end - start));
ImageCache.put("blurImage", bitmap);
return bitmap;
} /**
* 柔化效果(高斯模糊)(优化后比上面快三倍)
*
* @param bmp
* @return
*/
public static Bitmap blurImageAmeliorate(Bitmap bmp) {
if (ImageCache.get("blurImageAmeliorate") != null) {
return ImageCache.get("blurImageAmeliorate");
}
long start = System.currentTimeMillis();
// 高斯矩阵
int[] gauss = new int[] { , , , , , , , , }; int width = bmp.getWidth();
int height = bmp.getHeight();
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565); int pixR = ;
int pixG = ;
int pixB = ; int pixColor = ; int newR = ;
int newG = ;
int newB = ; int delta = ; // 值越小图片会越亮,越大则越暗 int idx = ;
int[] pixels = new int[width * height];
bmp.getPixels(pixels, , width, , , width, height);
for (int i = , length = height - ; i < length; i++) {
for (int k = , len = width - ; k < len; k++) {
idx = ;
for (int m = -; m <= ; m++) {
for (int n = -; n <= ; n++) {
pixColor = pixels[(i + m) * width + k + n];
pixR = Color.red(pixColor);
pixG = Color.green(pixColor);
pixB = Color.blue(pixColor); newR = newR + (int) (pixR * gauss[idx]);
newG = newG + (int) (pixG * gauss[idx]);
newB = newB + (int) (pixB * gauss[idx]);
idx++;
}
} newR /= delta;
newG /= delta;
newB /= delta; newR = Math.min(, Math.max(, newR));
newG = Math.min(, Math.max(, newG));
newB = Math.min(, Math.max(, newB)); pixels[i * width + k] = Color.argb(, newR, newG, newB); newR = ;
newG = ;
newB = ;
}
} bitmap.setPixels(pixels, , width, , , width, height);
long end = System.currentTimeMillis();
Log.d("blurImageAmeliorate", "used time=" + (end - start));
ImageCache.put("blurImageAmeliorate", bitmap);
return bitmap;
} public static Bitmap sketch(Bitmap bmp) {
if (ImageCache.get("sketch") != null) {
return ImageCache.get("sketch");
}
long start = System.currentTimeMillis();
int pos, row, col, clr;
int width = bmp.getWidth();
int height = bmp.getHeight();
int[] pixSrc = new int[width * height];
int[] pixNvt = new int[width * height];
// 先对图象的像素处理成灰度颜色后再取反
bmp.getPixels(pixSrc, , width, , , width, height); for (row = ; row < height; row++) {
for (col = ; col < width; col++) {
pos = row * width + col;
pixSrc[pos] = (Color.red(pixSrc[pos]) + Color.green(pixSrc[pos]) + Color.blue(pixSrc[pos])) / ;
pixNvt[pos] = - pixSrc[pos];
}
} // 对取反的像素进行高斯模糊, 强度可以设置,暂定为5.0
gaussGray(pixNvt, 5.0, 5.0, width, height); // 灰度颜色和模糊后像素进行差值运算
for (row = ; row < height; row++) {
for (col = ; col < width; col++) {
pos = row * width + col; clr = pixSrc[pos] << ;
clr /= - pixNvt[pos];
clr = Math.min(clr, ); pixSrc[pos] = Color.rgb(clr, clr, clr);
}
}
bmp.setPixels(pixSrc, , width, , , width, height);
long end = System.currentTimeMillis();
Log.d("blurImageAmeliorate", "used time=" + (end - start));
ImageCache.put("sketch", bmp);
return bmp;
} private static int gaussGray(int[] psrc, double horz, double vert, int width, int height) {
int[] dst, src;
double[] n_p, n_m, d_p, d_m, bd_p, bd_m;
double[] val_p, val_m;
int i, j, t, k, row, col, terms;
int[] initial_p, initial_m;
double std_dev;
int row_stride = width;
int max_len = Math.max(width, height);
int sp_p_idx, sp_m_idx, vp_idx, vm_idx; val_p = new double[max_len];
val_m = new double[max_len]; n_p = new double[];
n_m = new double[];
d_p = new double[];
d_m = new double[];
bd_p = new double[];
bd_m = new double[]; src = new int[max_len];
dst = new int[max_len]; initial_p = new int[];
initial_m = new int[]; // 垂直方向
if (vert > 0.0) {
vert = Math.abs(vert) + 1.0;
std_dev = Math.sqrt(-(vert * vert) / ( * Math.log(1.0 / 255.0))); // 初试化常量
findConstants(n_p, n_m, d_p, d_m, bd_p, bd_m, std_dev); for (col = ; col < width; col++) {
for (k = ; k < max_len; k++) {
val_m[k] = val_p[k] = ;
} for (t = ; t < height; t++) {
src[t] = psrc[t * row_stride + col];
} sp_p_idx = ;
sp_m_idx = height - ;
vp_idx = ;
vm_idx = height - ; initial_p[] = src[];
initial_m[] = src[height - ]; for (row = ; row < height; row++) {
terms = (row < ) ? row : ; for (i = ; i <= terms; i++) {
val_p[vp_idx] += n_p[i] * src[sp_p_idx - i] - d_p[i] * val_p[vp_idx - i];
val_m[vm_idx] += n_m[i] * src[sp_m_idx + i] - d_m[i] * val_m[vm_idx + i];
}
for (j = i; j <= ; j++) {
val_p[vp_idx] += (n_p[j] - bd_p[j]) * initial_p[];
val_m[vm_idx] += (n_m[j] - bd_m[j]) * initial_m[];
} sp_p_idx++;
sp_m_idx--;
vp_idx++;
vm_idx--;
} transferGaussPixels(val_p, val_m, dst, , height); for (t = ; t < height; t++) {
psrc[t * row_stride + col] = dst[t];
}
}
} // 水平方向
if (horz > 0.0) {
horz = Math.abs(horz) + 1.0; if (horz != vert) {
std_dev = Math.sqrt(-(horz * horz) / ( * Math.log(1.0 / 255.0))); // 初试化常量
findConstants(n_p, n_m, d_p, d_m, bd_p, bd_m, std_dev);
} for (row = ; row < height; row++) {
for (k = ; k < max_len; k++) {
val_m[k] = val_p[k] = ;
} for (t = ; t < width; t++) {
src[t] = psrc[row * row_stride + t];
} sp_p_idx = ;
sp_m_idx = width - ;
vp_idx = ;
vm_idx = width - ; initial_p[] = src[];
initial_m[] = src[width - ]; for (col = ; col < width; col++) {
terms = (col < ) ? col : ; for (i = ; i <= terms; i++) {
val_p[vp_idx] += n_p[i] * src[sp_p_idx - i] - d_p[i] * val_p[vp_idx - i];
val_m[vm_idx] += n_m[i] * src[sp_m_idx + i] - d_m[i] * val_m[vm_idx + i];
}
for (j = i; j <= ; j++) {
val_p[vp_idx] += (n_p[j] - bd_p[j]) * initial_p[];
val_m[vm_idx] += (n_m[j] - bd_m[j]) * initial_m[];
} sp_p_idx++;
sp_m_idx--;
vp_idx++;
vm_idx--;
} transferGaussPixels(val_p, val_m, dst, , width); for (t = ; t < width; t++) {
psrc[row * row_stride + t] = dst[t];
}
}
} return ;
} private static void findConstants(double[] n_p, double[] n_m, double[] d_p, double[] d_m, double[] bd_p,
double[] bd_m, double std_dev) {
double div = Math.sqrt( * 3.141593) * std_dev;
double x0 = -1.783 / std_dev;
double x1 = -1.723 / std_dev;
double x2 = 0.6318 / std_dev;
double x3 = 1.997 / std_dev;
double x4 = 1.6803 / div;
double x5 = 3.735 / div;
double x6 = -0.6803 / div;
double x7 = -0.2598 / div;
int i; n_p[] = x4 + x6;
n_p[] = (Math.exp(x1) * (x7 * Math.sin(x3) - (x6 + * x4) * Math.cos(x3)) + Math.exp(x0)
* (x5 * Math.sin(x2) - ( * x6 + x4) * Math.cos(x2)));
n_p[] = (
* Math.exp(x0 + x1)
* ((x4 + x6) * Math.cos(x3) * Math.cos(x2) - x5 * Math.cos(x3) * Math.sin(x2) - x7 * Math.cos(x2)
* Math.sin(x3)) + x6 * Math.exp( * x0) + x4 * Math.exp( * x1));
n_p[] = (Math.exp(x1 + * x0) * (x7 * Math.sin(x3) - x6 * Math.cos(x3)) + Math.exp(x0 + * x1)
* (x5 * Math.sin(x2) - x4 * Math.cos(x2)));
n_p[] = 0.0; d_p[] = 0.0;
d_p[] = - * Math.exp(x1) * Math.cos(x3) - * Math.exp(x0) * Math.cos(x2);
d_p[] = * Math.cos(x3) * Math.cos(x2) * Math.exp(x0 + x1) + Math.exp( * x1) + Math.exp( * x0);
d_p[] = - * Math.cos(x2) * Math.exp(x0 + * x1) - * Math.cos(x3) * Math.exp(x1 + * x0);
d_p[] = Math.exp( * x0 + * x1); for (i = ; i <= ; i++) {
d_m[i] = d_p[i];
} n_m[] = 0.0;
for (i = ; i <= ; i++) {
n_m[i] = n_p[i] - d_p[i] * n_p[];
} double sum_n_p, sum_n_m, sum_d;
double a, b; sum_n_p = 0.0;
sum_n_m = 0.0;
sum_d = 0.0; for (i = ; i <= ; i++) {
sum_n_p += n_p[i];
sum_n_m += n_m[i];
sum_d += d_p[i];
} a = sum_n_p / (1.0 + sum_d);
b = sum_n_m / (1.0 + sum_d); for (i = ; i <= ; i++) {
bd_p[i] = d_p[i] * a;
bd_m[i] = d_m[i] * b;
}
} private static void transferGaussPixels(double[] src1, double[] src2, int[] dest, int bytes, int width) {
int i, j, k, b;
int bend = bytes * width;
double sum; i = j = k = ;
for (b = ; b < bend; b++) {
sum = src1[i++] + src2[j++]; if (sum > )
sum = ;
else if (sum < )
sum = ; dest[k++] = (int) sum;
}
} /**
* 锐化效果
*
* @param bmp
* @return
*/
public static Bitmap emboss(Bitmap bmp) {
int width = bmp.getWidth();
int height = bmp.getHeight();
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565); int pixR = ;
int pixG = ;
int pixB = ; int pixColor = ; int newR = ;
int newG = ;
int newB = ; int[] pixels = new int[width * height];
bmp.getPixels(pixels, , width, , , width, height);
int pos = ;
for (int i = , length = height - ; i < length; i++) {
for (int k = , len = width - ; k < len; k++) {
pos = i * width + k;
pixColor = pixels[pos]; pixR = Color.red(pixColor);
pixG = Color.green(pixColor);
pixB = Color.blue(pixColor); pixColor = pixels[pos + ];
newR = Color.red(pixColor) - pixR + ;
newG = Color.green(pixColor) - pixG + ;
newB = Color.blue(pixColor) - pixB + ; newR = Math.min(, Math.max(, newR));
newG = Math.min(, Math.max(, newG));
newB = Math.min(, Math.max(, newB)); pixels[pos] = Color.argb(, newR, newG, newB);
}
} bitmap.setPixels(pixels, , width, , , width, height);
return bitmap;
} /**
* 图片锐化(拉普拉斯变换)
*
* @param bmp
* @return
*/
public static Bitmap sharpenImageAmeliorate(Bitmap bmp) {
if (ImageCache.get("sharpenImageAmeliorate") != null) {
return ImageCache.get("sharpenImageAmeliorate");
}
long start = System.currentTimeMillis();
// 拉普拉斯矩阵
int[] laplacian = new int[] { -, -, -, -, , -, -, -, - }; int width = bmp.getWidth();
int height = bmp.getHeight();
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565); int pixR = ;
int pixG = ;
int pixB = ; int pixColor = ; int newR = ;
int newG = ;
int newB = ; int idx = ;
float alpha = 0.3F;
int[] pixels = new int[width * height];
bmp.getPixels(pixels, , width, , , width, height);
for (int i = , length = height - ; i < length; i++) {
for (int k = , len = width - ; k < len; k++) {
idx = ;
for (int m = -; m <= ; m++) {
for (int n = -; n <= ; n++) {
pixColor = pixels[(i + n) * width + k + m];
pixR = Color.red(pixColor);
pixG = Color.green(pixColor);
pixB = Color.blue(pixColor); newR = newR + (int) (pixR * laplacian[idx] * alpha);
newG = newG + (int) (pixG * laplacian[idx] * alpha);
newB = newB + (int) (pixB * laplacian[idx] * alpha);
idx++;
}
} newR = Math.min(, Math.max(, newR));
newG = Math.min(, Math.max(, newG));
newB = Math.min(, Math.max(, newB)); pixels[i * width + k] = Color.argb(, newR, newG, newB);
newR = ;
newG = ;
newB = ;
}
} bitmap.setPixels(pixels, , width, , , width, height);
long end = System.currentTimeMillis();
Log.e("sharpenImageAmeliorate", "used time=" + (end - start));
ImageCache.put("sharpenImageAmeliorate", bitmap);
return bitmap;
} /**
* 底片效果
*
* @param bmp
* @return
*/
public static Bitmap film(Bitmap bmp) {
// RGBA的最大值
final int MAX_VALUE = ;
int width = bmp.getWidth();
int height = bmp.getHeight();
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565); int pixR = ;
int pixG = ;
int pixB = ; int pixColor = ; int newR = ;
int newG = ;
int newB = ; int[] pixels = new int[width * height];
bmp.getPixels(pixels, , width, , , width, height);
int pos = ;
for (int i = , length = height - ; i < length; i++) {
for (int k = , len = width - ; k < len; k++) {
pos = i * width + k;
pixColor = pixels[pos]; pixR = Color.red(pixColor);
pixG = Color.green(pixColor);
pixB = Color.blue(pixColor); newR = MAX_VALUE - pixR;
newG = MAX_VALUE - pixG;
newB = MAX_VALUE - pixB; newR = Math.min(MAX_VALUE, Math.max(, newR));
newG = Math.min(MAX_VALUE, Math.max(, newG));
newB = Math.min(MAX_VALUE, Math.max(, newB)); pixels[pos] = Color.argb(MAX_VALUE, newR, newG, newB);
}
} bitmap.setPixels(pixels, , width, , , width, height);
return bitmap;
} /**
* 光照效果
*
* @param bmp
* 光照中心x坐标
* @param centerX
* 光照中心要坐标
* @param centerY
* @return
*/
public static Bitmap sunshine(Bitmap bmp, int centerX, int centerY) {
final int width = bmp.getWidth();
final int height = bmp.getHeight();
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565); int pixR = ;
int pixG = ;
int pixB = ; int pixColor = ; int newR = ;
int newG = ;
int newB = ;
int radius = Math.min(centerX, centerY); final float strength = 150F; // 光照强度 100~150
int[] pixels = new int[width * height];
bmp.getPixels(pixels, , width, , , width, height);
int pos = ;
for (int i = , length = height - ; i < length; i++) {
for (int k = , len = width - ; k < len; k++) {
pos = i * width + k;
pixColor = pixels[pos]; pixR = Color.red(pixColor);
pixG = Color.green(pixColor);
pixB = Color.blue(pixColor); newR = pixR;
newG = pixG;
newB = pixB; // 计算当前点到光照中心的距离,平面座标系中求两点之间的距离
int distance = (int) (Math.pow((centerY - i), ) + Math.pow(centerX - k, ));
if (distance < radius * radius) {
// 按照距离大小计算增加的光照值
int result = (int) (strength * (1.0 - Math.sqrt(distance) / radius));
newR = pixR + result;
newG = pixG + result;
newB = pixB + result;
} newR = Math.min(, Math.max(, newR));
newG = Math.min(, Math.max(, newG));
newB = Math.min(, Math.max(, newB)); pixels[pos] = Color.argb(, newR, newG, newB);
}
} bitmap.setPixels(pixels, , width, , , width, height);
return bitmap;
} }

ImageView 各种工具类的更多相关文章

  1. Android 自定义的圆角矩形ImageView 工具类

    上图看效果 自定义圆角矩形ImageView工具类 package com.wechaotou.utils; import android.content.Context; import androi ...

  2. Android—关于自定义对话框的工具类

    开发中有很多地方会用到自定义对话框,为了避免不必要的城府代码,在此总结出一个工具类. 弹出对话框的地方很多,但是都大同小异,不同无非就是提示内容或者图片不同,下面这个类是将提示内容和图片放到了自定义函 ...

  3. Android两个页面之间的切换效果工具类

    import android.annotation.SuppressLint; import android.app.Activity; import android.content.Context; ...

  4. 【Android代码片段之六】Toast工具类(实现带图片的Toast消息提示)

    转载请注明出处,原文网址:http://blog.csdn.net/m_changgong/article/details/6841266  作者:张燕广 实现的Toast工具类ToastUtil封装 ...

  5. Android加载网络图片的工具类

    ImageView加载网络的图片 HttpUtil.java package com.eiice.httpuimagetils; import java.io.ByteArrayOutputStrea ...

  6. Android常见工具类封装

    MD5加密 import android.annotation.SuppressLint; import java.security.MessageDigest; public class MD5 { ...

  7. Bitmap工具类

    一直在使用的一个Bitmap工具类 处理Bitmap和ImageView对象,实现了下面功能: 1.saveBitmap: 把Bitmap对象持久存储到SD卡或手机内存. 2.getViewBitma ...

  8. Android开发之使用Handler封装下载图片工具类(源码分享)

    假设每下载一张图片,就得重写一次Http协议,多线程的启动和handler的信息传递就显得太麻烦了,我们直接来封装一个工具类,便于我们以后在开发时随时能够调用. (1)在清单文件加入权限 <us ...

  9. Android自定义圆形图片工具类(CTRL+C加CTRL+V直接使用)

    先贴一下工具类的代码!可直接复制粘贴 public class RoundImageView extends ImageView { private Paint mPaint; //画笔 privat ...

随机推荐

  1. 创建基于maven的项目模版

    我们在实际工作中 ,有些项目的架构是相似的,例如基于 restful的接口项目,如果每次都重新搭建一套架构或者通过拷贝建立一个项目难免有些得不偿失,这里我们可以用maven的archtype建立项目模 ...

  2. .net通用权限框架C/S概览

    通用权限框架cs部分 先概述一下,cs使用vs2010+sql2008 和bs公用同一个数据库 为使界面好看使用了第三方控件 donetbar和devexpress,正版是要收费的,但是你们都明白的可 ...

  3. 20151204--JDBC

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  4. 用过滤器和装饰者设计模式(静态代理)解决getParameter乱码问题

    post的乱码问题比较好解决,这里主要是对get请求的乱码做处理 解决思路:增强request对象的getParameter方法,使之 getParameter  直接获取到的就是解决乱码后的数据 有 ...

  5. java打包/命令行方式运行jar(命令行进行程序测试)

    public class Testtmp { public static void main(String[] args) { // TODO Auto-generated method stub f ...

  6. KMP字符串匹配

    #include<iostream> using namespace std; #define MAX 255 typedef unsigned char BYTE; typedef BY ...

  7. 1、 Linux中的root用户切换(转载)

    su和su - 的区别 大部分Linux发行版的默认账户是普通用户,而更改系统文件或者执行某些命令,需要root身份才能进行,这就需要从当前用户切换到root用户,Linux中切换用户的命令是su或s ...

  8. jQuery相关知识

    1.jQuery中$符号有何作用? $作为jQuery的别名,如$(document).ready() 即是 jQuery(document).ready() 2.jQuery选择器有哪几种? 基本选 ...

  9. Oratop工具——实时数据库性能监控工具

    在任何系统优化过程中,“80/20原则”是我们一定要关注的问题.简单的说,就是我们系统80%的性能问题.现象,都是有少数几个甚至一个问题造成的.这就需要我们面对复杂的系统性能问题的时候,要学会“拨开云 ...

  10. ORACLE-Kill 杀死正在执行的Oracle存储过程和死锁语句

    ORACLE-Kill 杀死正在执行的Oracle存储过程和死锁语句 存储过程 1.找到正在执行的存储过程的 sid ,serial# select   b.sid,b.SERIAL#,a.OBJEC ...