Android影像處理技術(實現Android中的PS)

來源:互聯網
上載者:User

Android影像處理技術(實現Android中的PS)

今天我們接著上次講的內容,介紹另一種改變圖片色彩的方法:像素;
今天通過一個例子來熟悉像素操作:實現圖片的 底片,懷舊,雕塑,這三種比較有意思的效果。

首先,,勾引一下你。

然後開始:

我們知道,圖片是由眾多排列緊密的像素點構成,每一個像素點都包含四個資訊,即R,G,B,A;於是,我們通過改變每個像素的這幾個值,就可以達到改變圖片色彩的目的:這就是我們的主要思路,下面我們開始代碼的編寫工作。

建立一個xml檔案:<喎?http://www.bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vcD4NCjxwcmUgY2xhc3M9"brush:java;">

很簡單,四個圖片,均勻分布,分別用來顯示:原圖,底片,懷舊,雕塑

在貼代碼之前,首先發幾張圖協助理解:



下面是實現三個效果的代碼,並有詳細的注釋:

首先:底片

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

很容易理解了。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.