安卓图像处理入门教程

简介: 第1章 图像处理概念介绍1、RGBA模型分析RGB:red,green,blue,alpha色相:物体传递的颜色饱和度:颜色的灰度,从0(灰)-100%亮度:颜色的相对明暗程度ColorMatrix setRotate() setSaturation() setScale() postConcat()第2章 颜色矩阵变换与实例图片

第1章 图像处理概念介绍

1、RGBA模型分析

RGB:red,green,blue,alpha
色相:物体传递的颜色
饱和度:颜色的灰度,从0(灰)-100%

亮度:颜色的相对明暗程度

ColorMatrix
setRotate()
setSaturation()
setScale()
postConcat()

第2章 颜色矩阵变换与实例

图片中矩阵的作用通常用于初始化颜色矩阵
这里写图片描述

第3章 像素点分析与实例

1.使用getPixels()方法可获取图片的所有点的颜色值,一般采用以下格式:bitmap.getPixels(int[] ,0,width,0,0,width,height)。
参数说明:存放像素值的数组->最开始读取像素时的偏移量->行距(多少算作一行,一般去width)->(x,y)第一次读取参数的目标->要读取像素的长度->要读取像素的宽度

2.获取所有点的颜色值后,想对像素点进行操作,则需要一Color。red(color)方法读取R分量,G、B、Alpha分量类似。此处的color为之前存放在数组里的颜色值

3.处理RGB值之后,还需检查其是否依旧在(0,255)范围内

4.使用方法newPx[i] = Color.argb(a, r, g, b);将新的RGB值创造新的颜色值

5.使用bmp.setPixels(newPx, 0, width, 0, 0, width, height);将颜色值应用到图片上,返回图片

  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;

老照片

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);

浮雕

 r = (r - r1 + 127);
    g = (g - g1 + 127);
    b = (b - b1 + 127);

ImageHelper工具类

public class ImageHelper {
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(Paint.ANTI_ALIAS_FLAG);

    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;
}
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;
}

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;
}

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;
}
}
目录
相关文章
|
存储 缓存 Android开发
|
Java 编译器 开发工具
安卓逆向系列篇:安卓工具总结(一)
安卓逆向系列篇:安卓工具总结
103 0
安卓逆向系列篇:安卓工具总结(一)
|
存储 安全 Java
安卓逆向系列篇:安卓工具总结(三)
安卓逆向系列篇:安卓工具总结
360 0
安卓逆向系列篇:安卓工具总结(三)
|
数据可视化 安全 Java
安卓逆向系列篇:安卓工具总结(二)
安卓逆向系列篇:安卓工具总结
913 0
安卓逆向系列篇:安卓工具总结(二)
|
算法 数据处理 开发工具
|
编解码 Android开发 计算机视觉
|
存储 缓存 数据可视化
iOS 开发全能工具箱:技术篇
iOS 开发工具箱是一系列的非常好用的 iOS 开发工具的集合,里面包括了网站,在桌面/移动设备上的应用,还有些后端(Back-end)的服务。我会尽力把这些工具分好类,如果有新添加近来的工具,我会放在 NEW 类别下。
269 0
|
安全 Java Android开发
浅谈安卓apk加固原理和实现
在安卓开发中,打包发布是开发的最后一个环节,apk是整个项目的源码和资源的结合体;对于懂点反编译原理的人可以轻松编译出apk的源码资源,并且可以修改资源代码、重新打包编译,轻轻松松变成自己的apk或者修改其中一部分窃取用户信息。
4766 1
|
Android开发
安卓的开发也有意思的啦
想多学习,想多了解。 不然,关于IT的整个链条就串不起来~ 到了安卓开发这里了, 感觉蛮有意思~~~ 书是按照台湾旗标公司的《Android APP开发入门》一步一步学习。 这个公司的书,不错。
1053 0

热门文章

最新文章