Android影像處理(2):懷舊效果

來源:互聯網
上載者:User

圖片懷舊效果的演算法:


 我們用顏色矩陣(ColorMatrix)來完成我們的懷舊效果。如果有不知道ColorMatrix的原理的話可以參考:Android學習筆記之映像顏色處理(ColorMatrix)

這就是那個顏色矩陣。我們可以利用上面的計算方法來改變我們的顏色矩陣的值從而達到我們想要的效果。

上面的計算方法可以轉換為:

   M = 

在Android中,顏色矩陣M是以一維數組m=[a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t]的方式進行儲存的。

M = [0.393,0.768,0.189,0,0 , 0.349 , 0.686, 0.168, 0 , 0, 0.272, 0.534, 0.131, 0 , 0, 0, 0 , 0 , 0,0]

具體操作:

自訂view

ColorView.java

package com.color;import android.content.Context;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Canvas;import android.graphics.ColorMatrix;import android.graphics.ColorMatrixColorFilter;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 ColorMatrix myColorMatrix = null;//設定顏色值private float[] colorArray = {(float) 0.393,(float) 0.768,(float) 0.189,0,0,             (float) 0.349,(float) 0.686,(float) 0.168,0,0,             (float) 0.272,(float) 0.534,(float) 0.131,0,0,             0,0,0,1,0};public ColorView(Context context, AttributeSet attrs){super(context, attrs);bitmap = BitmapFactory.decodeResource(context.getResources(),R.drawable.ww); invalidate(); }@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);//建立畫筆對象myPaint = new Paint();   //描畫(原始圖片)    canvas.drawBitmap(bitmap,0, 0, myPaint);      //建立顏色矩陣對象    myColorMatrix = new ColorMatrix();//設定顏色矩陣的值myColorMatrix.set(colorArray);           //設定畫筆顏色過濾器    myPaint.setColorFilter(new ColorMatrixColorFilter(myColorMatrix));    //描畫(處理後的圖片)canvas.drawBitmap(bitmap,0,0,myPaint);invalidate(); }}

main.xml布局檔案

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/colorView_layout"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >     <com.color.ColorView        android:id="@+id/myColorView"        android:layout_width="fill_parent"        android:layout_height="fill_parent" /></LinearLayout>

                              懷舊效果                                                         原圖

方法2:

將每個像素點的RGB值先分離出來,然後再按照上面的三個算式分別重新計算出RGB值然後做為當前點的RGB值

ColorView.java

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;public ColorView(Context context, AttributeSet attrs){super(context, attrs);bitmap = BitmapFactory.decodeResource(context.getResources(),R.drawable.ww); width = bitmap.getWidth();          height = bitmap.getHeight();        invalidate();}@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas); int pixColor = 0;          int pixR = 0;          int pixG = 0;          int pixB = 0;          int newR = 0;          int newG = 0;          int newB = 0;          int[] pixels = new int[width * height];          //擷取圖片的像素(一維數組)        /*         * pixels      接收位元影像顏色值的數組          * offset      寫入到pixels[]中的第一個像素索引值          * stride      pixels[]中的行間距個數值(必須大於等於位元影像寬度)。可以為負數          * x                               從位元影像中讀取的第一個像素的x座標值。          * y           從位元影像中讀取的第一個像素的y座標值          * width      從每一行中讀取的像素寬度          * height    讀取的行數           */        bitmap.getPixels(pixels, 0, width, 0, 0, width, height);          //擷取一個高height寬width的圖片像素        for (int i = 0; i < height; i++)          {              for (int j = 0; j < width; j++)              {                  pixColor = pixels[width * i + j];                 //等價於(pixColor >> 16) & 0xFF  擷取一個像素的R                pixR = Color.red(pixColor);                //等價於(pixColor >> 8) & 0xFF   擷取一個像素的G                pixG = Color.green(pixColor);                 //等價於(pixColor) & 0xFF   擷取一個像素的B                pixB = Color.blue(pixColor);                  //根據演算法由原圖的RGB產生新的RGB                newR = (int) (0.393 * pixR + 0.769 * pixG + 0.189 * pixB);                  newG = (int) (0.349 * pixR + 0.686 * pixG + 0.168 * pixB);                  newB = (int) (0.272 * pixR + 0.534 * pixG + 0.131 * pixB);                 //由RGB產生一個像素                //函數:argb (int alpha, int red, int green, int blue)                 int newColor = Color.argb(255, newR > 255 ? 255 : newR, newG > 255 ? 255 : newG, newB > 255 ? 255 : newB);                  pixels[width * i + j] = newColor;              }          }          //產生新的圖片        bitmap.setPixels(pixels, 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.