• Android图像处理之冰冻效果


                        原图                                                                          效果图



    代码:

    1. package com.color;  
    2.   
    3. import android.content.Context;  
    4. import android.graphics.Bitmap;  
    5. import android.graphics.BitmapFactory;  
    6. import android.graphics.Canvas;  
    7. import android.graphics.Color;  
    8. import android.graphics.Paint;  
    9. import android.util.AttributeSet;  
    10. import android.widget.ImageView;  
    11.   
    12. public class ColorView extends ImageView {  
    13.   
    14.     private Paint myPaint = null;  
    15.     private Bitmap bitmap = null;  
    16.     private int width, height;  
    17.     private int[] oldPixels;  
    18.     private int[] newPixels;  
    19.     private int color, color2;  
    20.     private int pixelsR, pixelsG, pixelsB, pixelsA, pixelsR2, pixelsG2,  
    21.             pixelsB2;  
    22.   
    23.     public ColorView(Context context, AttributeSet attrs) {  
    24.         super(context, attrs);  
    25.         bitmap = BitmapFactory.decodeResource(context.getResources(),  
    26.                 R.drawable.ww);  
    27.         width = bitmap.getWidth();  
    28.         height = bitmap.getHeight();  
    29.         oldPixels = new int[width * height];  
    30.         newPixels = new int[width * height];  
    31.         invalidate();  
    32.     }  
    33.   
    34.     @Override  
    35.     protected void onDraw(Canvas canvas) {  
    36.         super.onDraw(canvas);  
    37.         // 获取像素  
    38.         bitmap.getPixels(oldPixels, 0, width, 0, 0, width, height);  
    39.   
    40.         for (int i = 1; i < height * width; i++) {  
    41.             color = oldPixels[i];  
    42.             // 获取RGB分量  
    43.             pixelsA = Color.alpha(color);  
    44.             pixelsR = Color.red(color);  
    45.             pixelsG = Color.green(color);  
    46.             pixelsB = Color.blue(color);  
    47.             //R  
    48.             int pixel = pixelsR - pixelsG - pixelsB;  
    49.             pixel = pixel * 3 / 2;  
    50.             if (pixel < 0) {  
    51.                 pixel = -pixel;  
    52.             }  
    53.             if (pixel > 255){  
    54.                 pixel = 255;  
    55.             }  
    56.             pixelsR = pixel; // 计算后重置R值,以下类同  
    57.             //G  
    58.             pixel = pixelsG - pixelsR - pixelsB;  
    59.             pixel = pixel * 3 / 2;  
    60.             if (pixel < 0) {  
    61.                 pixel = -pixel;  
    62.             }  
    63.             if (pixel > 255){  
    64.                 pixel = 255;  
    65.             }  
    66.             pixelsG = pixel;  
    67.             //B  
    68.             pixel = pixelsB - pixelsR - pixelsG;  
    69.             pixel = pixel * 3 / 2;  
    70.             if (pixel < 0) {  
    71.                 pixel = -pixel;  
    72.             }  
    73.             if (pixel > 255){  
    74.                 pixel = 255;  
    75.             }  
    76.             pixelsB = pixel;  
    77.               
    78.             // 根据新的RGB生成新像素  
    79.             newPixels[i] = Color.argb(pixelsA, pixelsR, pixelsG, pixelsB);  
    80.   
    81.         }  
    82.         // 根据新像素生成新图片  
    83.         bitmap.setPixels(newPixels, 0, width, 0, 0, width, height);  
    84.         canvas.drawBitmap(bitmap, 0, 0, myPaint);  
    85.     }  
    86. }  



    参考博文:点击打开链接



  • 相关阅读:
    C++:怎样把一个int转成4个字节?
    安装虚拟机
    [Flux] 1. Development Environment Setup
    [CSS] Animating SVG
    [Node.js] Scraping Dynamic JavaScript Websites with Nightmare
    [React] React Fundamentals: Integrating Components with D3 and AngularJS
    [React] React Fundamentals: with-addons
    [JavaScript] Array.prototype.reduce in JavaScript by example
    [CSS] @keyframes
    [CSS] Transforms
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/6722696.html
Copyright © 2020-2023  润新知