android影像處理(3)浮雕效果

來源:互聯網
上載者:User

標籤:log   tor   prot   upload   ima   paint   bsp   圖片特效   浮雕效果   

這篇將講到圖片特效處理的浮雕效果。跟前面一樣是對像素點進行處理,演算法是通用的。

演算法原理:用前一個像素點的RGB值分別減去當前像素點的RGB值並加上127作為當前像素點的RGB值。

例:

ABC

求B點的浮雕效果如下:

B.r = C.r - B.r + 127;

B.g = C.g - B.g + 127;

B.b = C.b - B.b + 127;

注意RGB值在0~255之間。



                     原圖                                                                              



[java] view plain copy
    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,pixelsB2;  
    21.       
    22.     public ColorView(Context context, AttributeSet attrs)  
    23.     {  
    24.         super(context, attrs);  
    25.         bitmap = BitmapFactory.decodeResource(context.getResources(),R.drawable.ww);   
    26.         width = bitmap.getWidth();    
    27.         height = bitmap.getHeight();  
    28.         oldPixels = new int[width*height];   
    29.         newPixels = new int[width*height];  
    30.         invalidate();  
    31.     }  
    32.     @Override  
    33.     protected void onDraw(Canvas canvas) {  
    34.         super.onDraw(canvas);   
    35.         //擷取像素  
    36.         bitmap.getPixels(oldPixels, 0, width, 0, 0, width, height);  
    37.           
    38.         for(int i = 1;i < height*width; i++){  
    39.                 color = oldPixels[i-1];  
    40.                 //前一個像素  
    41.                 pixelsR = Color.red(color);  
    42.                 pixelsG = Color.green(color);  
    43.                 pixelsB = Color.blue(color);  
    44.                 //當前像素  
    45.                 color2 = oldPixels[i];  
    46.                 pixelsR2 = Color.red(color2);  
    47.                 pixelsG2 = Color.green(color2);  
    48.                 pixelsB2 = Color.blue(color2);  
    49.                   
    50.                 pixelsR = (pixelsR - pixelsR2 + 127);  
    51.                 pixelsG = (pixelsG - pixelsG2 + 127);  
    52.                 pixelsB = (pixelsB - pixelsB2 + 127);  
    53.                 //均小於等於255  
    54.                 if(pixelsR > 255){  
    55.                     pixelsR = 255;  
    56.                 }  
    57.                   
    58.                 if(pixelsG > 255){  
    59.                     pixelsG = 255;  
    60.                 }  
    61.                   
    62.                 if(pixelsB > 255){  
    63.                     pixelsB = 255;  
    64.                 }  
    65.                   
    66.                 newPixels[i] = Color.argb(pixelsA, pixelsR, pixelsG, pixelsB);  
    67.                   
    68.         }  
    69.         bitmap.setPixels(newPixels, 0, width, 0, 0, width, height);  
    70.         canvas.drawBitmap(bitmap,0,0,myPaint);  
    71.     }  

android影像處理(3)浮雕效果

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.