This article will deal with the effect of film effects. As before, the pixel is processed, and the algorithm is universal.
Algorithm principle: The RGB value of the current pixel points and the difference of 255 respectively as the current point of the RGB value.
Cases:
Abc
To find the negative effect of B point:
B.R = 255-B.R;
B.G = 255-B.G;
b.b = 255-b.b;
:
Original
Code:
[Java]View PlainCopy
- Package com.color;
- Import Android.content.Context;
- Import Android.graphics.Bitmap;
- Import Android.graphics.BitmapFactory;
- Import Android.graphics.Canvas;
- Import Android.graphics.Color;
- Import Android.graphics.Paint;
- Import Android.util.AttributeSet;
- Import Android.widget.ImageView;
- Public class Colorview extends ImageView {
- private Paint mypaint = null;
- private Bitmap Bitmap = null;
- private int width,height;
- private int[] oldpixels;
- private int[] newpixels;
- private int color,color2;
- private int pixelsr,pixelsg,pixelsb,pixelsa,pixelsr2,pixelsg2,pixelsb2;
- Public Colorview (context context, AttributeSet attrs)
- {
- Super (context, attrs);
- Bitmap = Bitmapfactory.decoderesource (Context.getresources (), R.DRAWABLE.WW);
- width = Bitmap.getwidth ();
- Height = bitmap.getheight ();
- Oldpixels = new int[width*height];
- Newpixels = new int[width*height];
- Invalidate ();
- }
- @Override
- protected void OnDraw (canvas canvas) {
- Super.ondraw (canvas);
- //Get pixels
- Bitmap.getpixels (Oldpixels, 0, width, 0, 0, width, height);
- For (int i = 1;i < height*width; i++) {
- color = Oldpixels[i];
- //Get RGB components
- Pixelsa = Color.alpha (Color);
- PIXELSR = Color.Red (Color);
- PIXELSG = Color.green (Color);
- PIXELSB = Color.Blue (Color);
- //Conversion
- PIXELSR = (255-PIXELSR);
- PIXELSG = (255-pixelsg);
- PIXELSB = (255-pixelsb);
- //Both are less than or equal to 255 greater than or equal to 0
- if (PIXELSR > 255) {
- PIXELSR = 255;
- }
- Else if (PIXELSR < 0) {
- PIXELSR = 0;
- }
- if (pixelsg > 255) {
- PIXELSG = 255;
- }
- Else if (PIXELSG < 0) {
- PIXELSG = 0;
- }
- if (pixelsb > 255) {
- PIXELSB = 255;
- }
- Else if (PIXELSB < 0) {
- PIXELSB = 0;
- }
- //Generate new pixels based on new RGB
- Newpixels[i] = Color.argb (Pixelsa, PIXELSR, PIXELSG, PIXELSB);
- }
- //Create new picture based on new pixel
- Bitmap.setpixels (Newpixels, 0, width, 0, 0, width, height);
- Canvas.drawbitmap (Bitmap,0,0,mypaint);
- }
- }
Android image processing (3) Negative effects