package com.moredo.photograph;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Matrix;
import android.graphics.Paint;
public class PhotogEffect
{
/**
* @fun 图片三原色处理
* @param bm
* @param hue
* @param saturation
* @param lum
* @return
*/
public static Bitmap handleImageEffect(Bitmap bm, float hue, float saturation, float lum)
{
Bitmap bmp = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bmp);
Paint paint = new Paint();
ColorMatrix hueMatrix = new ColorMatrix();
hueMatrix.setRotate(0, hue);
hueMatrix.setRotate(1, hue);
hueMatrix.setRotate(2, hue);
ColorMatrix saturationMatrix = new ColorMatrix();
saturationMatrix.setSaturation(saturation);
ColorMatrix lumMatrix = new ColorMatrix();
lumMatrix.setScale(lum, lum, lum, 1);
ColorMatrix imageMatrix = new ColorMatrix();
imageMatrix.postConcat(hueMatrix);
imageMatrix.postConcat(saturationMatrix);
imageMatrix.postConcat(lumMatrix);
paint.setColorFilter(new ColorMatrixColorFilter(imageMatrix));
canvas.drawBitmap(bm, 0, 0, paint);
return bmp;
}
/**
* @fun 底片效果
* @param bm
* @return
*/
public static Bitmap handleImageNegative(Bitmap bm)
{
int width = bm.getWidth();
int height = bm.getHeight();
int color;
int r, g, b, a;
Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
int[] oldPx = new int[width * height];
int[] newPx = new int[width * height];
bm.getPixels(oldPx, 0, width, 0, 0, width, height);
for (int i = 0; i < width * height; i++)
{
color = oldPx[i];
r = Color.red(color);
g = Color.green(color);
b = Color.blue(color);
a = Color.alpha(color);
r = 255 - r;
g = 255 - g;
b = 255 - b;
if (r > 255)
{
r = 255;
}
else if (r < 0)
{
r = 0;
}
if (g > 255)
{
g = 255;
}
else if (g < 0)
{
g = 0;
}
if (b > 255)
{
b = 255;
}
else if (b < 0)
{
b = 0;
}
newPx[i] = Color.argb(a, r, g, b);
}
bmp.setPixels(newPx, 0, width, 0, 0, width, height);
return bmp;
}
/**
* @fun 老照片效果
* @param bm
* @return
*/
public static Bitmap handleImagePixelsOldPhoto(Bitmap bm)
{
Bitmap bmp = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), Bitmap.Config.ARGB_8888);
int width = bm.getWidth();
int height = bm.getHeight();
int color = 0;
int r, g, b, a, r1, g1, b1;
int[] oldPx = new int[width * height];
int[] newPx = new int[width * height];
bm.getPixels(oldPx, 0, bm.getWidth(), 0, 0, width, height);
for (int i = 0; i < width * height; i++)
{
color = oldPx[i];
a = Color.alpha(color);
r = Color.red(color);
g = Color.green(color);
b = Color.blue(color);
r1 = (int) (0.393 * r + 0.769 * g + 0.189 * b);
g1 = (int) (0.349 * r + 0.686 * g + 0.168 * b);
b1 = (int) (0.272 * r + 0.534 * g + 0.131 * b);
if (r1 > 255)
{
r1 = 255;
}
if (g1 > 255)
{
g1 = 255;
}
if (b1 > 255)
{
b1 = 255;
}
newPx[i] = Color.argb(a, r1, g1, b1);
}
bmp.setPixels(newPx, 0, width, 0, 0, width, height);
return bmp;
}
/**
* @fun 浮雕效果
* @param bm
* @return
*/
public static Bitmap handleImagePixelsRelief(Bitmap bm)
{
Bitmap bmp = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), Bitmap.Config.ARGB_8888);
int width = bm.getWidth();
int height = bm.getHeight();
int color = 0, colorBefore = 0;
int a, r, g, b;
int r1, g1, b1;
int[] oldPx = new int[width * height];
int[] newPx = new int[width * height];
bm.getPixels(oldPx, 0, bm.getWidth(), 0, 0, width, height);
for (int i = 1; i < width * height; i++)
{
colorBefore = oldPx[i - 1];
a = Color.alpha(colorBefore);
r = Color.red(colorBefore);
g = Color.green(colorBefore);
b = Color.blue(colorBefore);
color = oldPx[i];
r1 = Color.red(color);
g1 = Color.green(color);
b1 = Color.blue(color);
r = (r - r1 + 127);
g = (g - g1 + 127);
b = (b - b1 + 127);
if (r > 255)
{
r = 255;
}
if (g > 255)
{
g = 255;
}
if (b > 255)
{
b = 255;
}
newPx[i] = Color.argb(a, r, g, b);
}
bmp.setPixels(newPx, 0, width, 0, 0, width, height);
return bmp;
}
/**
* 功能:将图片转成黑白图 注重白色
*
* @param bitmap
* 要处理的图片
* @return base 处理后的图片
*/
public static Bitmap func(Bitmap bitmap)
{
// Bitmap bitmap = BitmapFactory.decodeResource(getResources(), id);
int width = bitmap.getWidth();
int height = bitmap.getHeight();
Bitmap base = Bitmap.createBitmap(width, height, bitmap.getConfig());
Canvas canvas = new Canvas(base); /// < 以base为模板创建canvas对象
canvas.drawBitmap(bitmap, new Matrix(), new Paint());
for (int i = 0; i < width; i++) /// < 遍历像素点
{
for (int j = 0; j < height; j++)
{
int color = bitmap.getPixel(i, j);
int r = Color.red(color);
int g = Color.green(color);
int b = Color.blue(color);
int a = Color.alpha(color);
int avg = (r + g + b) / 3; /// < RGB均值
if (avg >= 50) /// < 100可以理解为经验值
{
base.setPixel(i, j, Color.argb(a, 255, 255, 255));/// < 设为白色
}
else
{
base.setPixel(i, j, Color.argb(a, 0, 0, 0)); /// < 设为黑色
}
}
}
return base;
}
}