Android影像處理技術(實現Android中的PS)(三),影像處理android
今天我們接著上次講的內容,介紹另一種改變圖片色彩的方法:像素;
今天通過一個例子來熟悉像素操作:實現圖片的 底片,懷舊,雕塑,這三種比較有意思的效果。
首先,,勾引一下你。
然後開始:
我們知道,圖片是由眾多排列緊密的像素點構成,每一個像素點都包含四個資訊,即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>
很簡單,四個圖片,均勻分布,分別用來顯示:原圖,底片,懷舊,雕塑
在貼代碼之前,首先發幾張圖協助理解:
下面是實現三個效果的代碼,並有詳細的注釋:
首先:底片
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