Android image processing technology (implementing PS in Android) (iii), image processing android
Next, let's talk about another method to change the color of an image: pixel;
Today, we use an example to familiarize ourselves with Pixel operations:These three kinds of interesting effects are used to achieve negative film, nostalgia, and sculpture of images.
First, seduce you.
Then start:
We know that an image is composed of many closely arranged pixels. Each pixel contains four pieces of information, namely, R, G, B, and A. Therefore, by changing the values of each pixel, we can change the color of the image. This is our main idea. Let's start coding.
Create an xml file:
<?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>
Very simple: Four images are evenly distributed for display: original images, negatives, nostalgia, and sculptures.
Before you paste the code, first send a few images to help you understand:
The following code implements three effects with detailed notes:
First: Negative Film
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); // stores the original pixel value int oldPix [] = new int [width * height]; int newPix [] = new int [width * height]; // assign the pixel value to oldpix; bt. getPixels (oldPix, 0, width, 0, 0, width, height); // cyclically retrieve each pixel and change it for (int I = 0; I <oldPix. length; I ++) {// retrieve the RGB value corresponding to this pixel value color = oldPix [I]; r = Color. red (color); g = Color. green (color); B = Color. red (color); a = Color. alpha (color); // apply the negative film transformation formula r = 255-r; g = 255-g; B = 255-B; // check 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 ;}
Second: nostalgia
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); // stores the original pixel value int oldPix [] = new int [width * height]; int newPix [] = new int [width * height]; // assign the pixel value to oldpix; bt. getPixels (oldPix, 0, width, 0, 0, width, height); // cyclically retrieve each pixel and change it for (int I = 0; I <oldPix. length; I ++) {// retrieve the RGB value corresponding to this pixel value color = oldPix [I]; r = Color. red (color); g = Color. green (color); B = Color. red (color); a = Color. alpha (color); // apply the negative film transformation formula 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 ); // check 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 ;}
Final: sculpture
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); // stores the original pixel value int oldPix [] = new int [width * height]; int newPix [] = new int [width * height]; // assign the pixel value to oldpix; bt. getPixels (oldPix, 0, width, 0, 0, width, height); // retrieve each pixel cyclically and change it for (in T I = 0; I <oldPix. length; I ++) {// retrieve the RGB value corresponding to this pixel value color = oldPix [I]; r = Color. red (color); g = Color. green (color); B = Color. red (color); a = Color. alpha (color); // pay attention to the processing here: Prevent arrays from crossing the border color1 = oldPix [I = 0? 0: I-1]; r1 = Color. red (color1); g1 = Color. green (color1); b1 = Color. red (color1); // apply the negative film Transform Formula r = r1-r + 127; g = g1-g + 127; B = b1-b + 127; // check the cross border 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 ;}
It is easy to understand.
Last: Demo address http://download.csdn.net/detail/nsgsbs/8533269