【轉】四、android映像特效處理之底片效果

來源:互聯網
上載者:User

標籤:android   c   style   class   blog   code   

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

演算法原理:將當前像素點的RGB值分別與255之差後的值作為當前點的RGB值。

例:

ABC

求B點的底片效果:

B.r = 255 - B.r;

B.g = 255 - B.g;

B.b = 255 - B.b;

:

原圖:

代碼:

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);         //擷取像素        bitmap.getPixels(oldPixels, 0, width, 0, 0, width, height);                for(int i = 1;i < height*width; i++){                color = oldPixels[i];                //擷取RGB分量                pixelsA = Color.alpha(color);                pixelsR = Color.red(color);                pixelsG = Color.green(color);                pixelsB = Color.blue(color);                                //轉換                pixelsR = (255 - pixelsR);                pixelsG = (255 - pixelsG);                pixelsB = (255 - pixelsB);                //均小於等於255大於等於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;                }                //根據新的RGB產生新像素                newPixels[i] = Color.argb(pixelsA, pixelsR, pixelsG, pixelsB);                        }        //根據新像素產生新圖片        bitmap.setPixels(newPixels, 0, width, 0, 0, width, height);        canvas.drawBitmap(bitmap,0,0,myPaint);    }}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.