Android图像处理技术(实现Android中的PS)(3)

Android图像处理技术(实现Android中的PS)(三)

今天我们接着上次讲的内容,介绍另一种改变图片色彩的方法:像素;
今天通过一个例子来熟悉像素操作:实现图片的 底片,怀旧,雕塑,这三种比较有意思的效果。

首先,上图,勾引一下你。
Android图像处理技术(实现Android中的PS)(3)

然后开始:

我们知道,图片是由众多排列紧密的像素点构成,每一个像素点都包含四个信息,即R,G,B,A;于是,我们通过改变每个像素的这几个值,就可以达到改变图片色彩的目的:这就是我们的主要思路,下面我们开始代码的编写工作。

新建一个xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/image1"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1" />

        <ImageView
            android:id="@+id/image2"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >

        <ImageView
            android:id="@+id/image3"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1" />

        <ImageView
            android:id="@+id/image4"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1" />
    </LinearLayout>

</LinearLayout>

很简单,四个图片,均匀分布,分别用来显示:原图,底片,怀旧,雕塑

在贴代码之前,首先发几张图帮助理解:
Android图像处理技术(实现Android中的PS)(3)
Android图像处理技术(实现Android中的PS)(3)
Android图像处理技术(实现Android中的PS)(3)
下面是实现三个效果的代码,并有详细的注释:

首先:底片

public Bitmap handleNegative(Bitmap bt) {
        int width = bt.getWidth();
        int height = bt.getHeight();
        int color;
        int r, g, b, a;
        Bitmap bmp = Bitmap.createBitmap(width, height, Config.ARGB_8888);
        // 存放原来的像素值
        int oldPix[] = new int[width * height];
        int newPix[] = new int[width * height];
        // 将像素值赋值给oldpix;
        bt.getPixels(oldPix, 0, width, 0, 0, width, height);
        // 循环取出每个像素,并对其进行更改
        for (int i = 0; i < oldPix.length; i++) {
            // 分别取出这个像素值对应的RGB值
            color = oldPix[i];
            r = Color.red(color);
            g = Color.green(color);
            b = Color.red(color);
            a = Color.alpha(color);
            // 应用底片变换公式
            r = 255 - r;
            g = 255 - g;
            b = 255 - b;
            // 检查越界
            if (r < 0) {
                r = 0;
            } else if (r > 255) {
                r = 255;
            }
            if (g < 0) {
                g = 0;
            } else if (g > 255) {
                g = 255;
            }
            if (b < 0) {
                b = 0;
            } else if (b > 255) {
                b = 255;
            }
            newPix[i] = Color.argb(a, r, g, b);
        }
        bmp.setPixels(newPix, 0, width, 0, 0, width, height);
        return bmp;
    }

其次:怀旧

public Bitmap handleOld(Bitmap bt) {
        int width = bt.getWidth();
        int height = bt.getHeight();
        int color;
        int r, g, b, a;
        Bitmap bmp = Bitmap.createBitmap(width, height, Config.ARGB_8888);
        // 存放原来的像素值
        int oldPix[] = new int[width * height];
        int newPix[] = new int[width * height];
        // 将像素值赋值给oldpix;
        bt.getPixels(oldPix, 0, width, 0, 0, width, height);
        // 循环取出每个像素,并对其进行更改
        for (int i = 0; i < oldPix.length; i++) {
            // 分别取出这个像素值对应的RGB值
            color = oldPix[i];
            r = Color.red(color);
            g = Color.green(color);
            b = Color.red(color);
            a = Color.alpha(color);
            // 应用底片变换公式
            r = (int) (0.393*r+0.769*r+0.189*r);
            g = (int) (0.349*g+0.686*g+0.189*g);
            b = (int) (0.272*b+0.534*b+0.131*b);
            // 检查越界
            if (r < 0) {
                r = 0;
            } else if (r > 255) {
                r = 255;
            }
            if (g < 0) {
                g = 0;
            } else if (g > 255) {
                g = 255;
            }
            if (b < 0) {
                b = 0;
            } else if (b > 255) {
                b = 255;
            }
            newPix[i] = Color.argb(a, r, g, b);
        }
        bmp.setPixels(newPix, 0, width, 0, 0, width, height);
        return bmp;
    }

最后:雕塑

public Bitmap handleRelief(Bitmap bt) {
        int width = bt.getWidth();
        int height = bt.getHeight();
        int color,color1;
        int r, g, b, a;
        int r1,g1,b1;
        Bitmap bmp = Bitmap.createBitmap(width, height, Config.ARGB_8888);
        // 存放原来的像素值
        int oldPix[] = new int[width * height];
        int newPix[] = new int[width * height];
        // 将像素值赋值给oldpix;
        bt.getPixels(oldPix, 0, width, 0, 0, width, height);
        // 循环取出每个像素,并对其进行更改
        for (int i = 0; i < oldPix.length; i++) {
            // 分别取出这个像素值对应的RGB值
            color = oldPix[i];
            r = Color.red(color);
            g = Color.green(color);
            b = Color.red(color);
            a = Color.alpha(color);
            //注意这里的处理:防止数组越界
            color1 = oldPix[i==0?0:i-1];
            r1 = Color.red(color1);
            g1 = Color.green(color1);
            b1 = Color.red(color1);
            // 应用底片变换公式
            r = r1-r+127;
            g = g1-g+127;
            b = b1-b+127;
            // 检查越界
            if (r < 0) {
                r = 0;
            } else if (r > 255) {
                r = 255;
            }
            if (g < 0) {
                g = 0;
            } else if (g > 255) {
                g = 255;
            }
            if (b < 0) {
                b = 0;
            } else if (b > 255) {
                b = 255;
            }
            newPix[i] = Color.argb(a, r, g, b);
        }
        bmp.setPixels(newPix, 0, width, 0, 0, width, height);
        return bmp;
    }

很容易理解了。

最后:Demo地址 http://download.csdn.net/detail/nsgsbs/8533269