Android image processing (3) Negative effects

Source: Internet
Author: User

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
    1. Package com.color;
    2. Import Android.content.Context;
    3. Import Android.graphics.Bitmap;
    4. Import Android.graphics.BitmapFactory;
    5. Import Android.graphics.Canvas;
    6. Import Android.graphics.Color;
    7. Import Android.graphics.Paint;
    8. Import Android.util.AttributeSet;
    9. Import Android.widget.ImageView;
    10. Public class Colorview extends ImageView {
    11. private Paint mypaint = null;
    12. private Bitmap Bitmap = null;
    13. private int width,height;
    14. private int[] oldpixels;
    15. private int[] newpixels;
    16. private int color,color2;
    17. private int pixelsr,pixelsg,pixelsb,pixelsa,pixelsr2,pixelsg2,pixelsb2;
    18. Public Colorview (context context, AttributeSet attrs)
    19. {
    20. Super (context, attrs);
    21. Bitmap = Bitmapfactory.decoderesource (Context.getresources (), R.DRAWABLE.WW);
    22. width = Bitmap.getwidth ();
    23. Height = bitmap.getheight ();
    24. Oldpixels = new int[width*height];
    25. Newpixels = new int[width*height];
    26. Invalidate ();
    27. }
    28. @Override
    29. protected void OnDraw (canvas canvas) {
    30. Super.ondraw (canvas);
    31. //Get pixels
    32. Bitmap.getpixels (Oldpixels, 0, width, 0, 0, width, height);
    33. For (int i = 1;i < height*width; i++) {
    34. color = Oldpixels[i];
    35. //Get RGB components
    36. Pixelsa = Color.alpha (Color);
    37. PIXELSR = Color.Red (Color);
    38. PIXELSG = Color.green (Color);
    39. PIXELSB = Color.Blue (Color);
    40. //Conversion
    41. PIXELSR = (255-PIXELSR);
    42. PIXELSG = (255-pixelsg);
    43. PIXELSB = (255-pixelsb);
    44. //Both are less than or equal to 255 greater than or equal to 0
    45. if (PIXELSR > 255) {
    46. PIXELSR = 255;
    47. }
    48. Else if (PIXELSR < 0) {
    49. PIXELSR = 0;
    50. }
    51. if (pixelsg > 255) {
    52. PIXELSG = 255;
    53. }
    54. Else if (PIXELSG < 0) {
    55. PIXELSG = 0;
    56. }
    57. if (pixelsb > 255) {
    58. PIXELSB = 255;
    59. }
    60. Else if (PIXELSB < 0) {
    61. PIXELSB = 0;
    62. }
    63. //Generate new pixels based on new RGB
    64. Newpixels[i] = Color.argb (Pixelsa, PIXELSR, PIXELSG, PIXELSB);
    65. }
    66. //Create new picture based on new pixel
    67. Bitmap.setpixels (Newpixels, 0, width, 0, 0, width, height);
    68. Canvas.drawbitmap (Bitmap,0,0,mypaint);
    69. }
    70. }

Android image processing (3) Negative effects

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.