標籤:rgba colormatrix bitmap 像素 imageview
轉載請註明出處:http://blog.csdn.net/forwardyzk/article/details/44410833
下面介紹Android影像處理。
RGBA模型:red green blue alpha
1.改變其色像,透明度和亮度。
2.使用顏色矩陣處理映像
3.改變像素處理映像
1.使用ColorMatrix改變色相,透明度和亮度
擷取新的映像的步驟:
(1)建立新的BitMap,使用createBitmap()方法
(2)使用ColorMatrix對象改變其色相,色值和亮度
/** * Set the rotation on a color axis by the specified values. * <code>axis=0</code> correspond to a rotation around the RED color * <code>axis=1</code> correspond to a rotation around the GREEN color * <code>axis=2</code> correspond to a rotation around the BLUE color */
setRotate(參數1,參數2)改變色相,第一個參數:0表示改變的紅色,1表示的改變的綠色,2表示改變額是藍色,第二個參數:表示的是深度。
setSaturation():設定透明度
postConcat():設定亮度
(3)使用畫筆在畫布上畫出新的BitMap
給畫筆設定顏色選取器,使顏色矩陣和畫筆串連起來。
setColorFilter();
/** * 擷取改變了色值,透明度,亮度的BitMap * * @param bitmap * @param rorate 色值 * @param saturation 透明度 * @param scale 亮度 * @return */ public static Bitmap handleImageEffect(Bitmap bitmap, float rorate, float saturation, float scale) { Bitmap newBitMap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888); //色值 ColorMatrix rorateMatrix = new ColorMatrix(); rorateMatrix.setRotate(0, rorate);//red rorateMatrix.setRotate(1, rorate);//green rorateMatrix.setRotate(2, rorate);//blue //透明度 ColorMatrix saturationMatrix = new ColorMatrix(); saturationMatrix.setSaturation(saturation); //亮度 ColorMatrix scaleMatrix = new ColorMatrix(); scaleMatrix.setScale(scale, scale, scale, 1); ColorMatrix colorMatrix = new ColorMatrix(); colorMatrix.postConcat(rorateMatrix); colorMatrix.postConcat(saturationMatrix); colorMatrix.postConcat(scaleMatrix); Canvas canvas = new Canvas(newBitMap); Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setColorFilter(new ColorMatrixColorFilter(colorMatrix)); canvas.drawBitmap(bitmap, 0, 0, paint); return newBitMap; }
primary_color_activity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_centerHorizontal="true" android:layout_height="match_parent" tools:context=".MainActivity"> <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:layout_marginBottom="10dp" /> <!--調節色象相--> <SeekBar android:id="@+id/seek_rorate" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:layout_marginBottom="10dp" android:layout_below="@id/image" /> <!--調節透明度--> <SeekBar android:id="@+id/seek_saturation" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:layout_marginBottom="10dp" android:layout_below="@id/seek_rorate" /> <!--調節亮度--> <SeekBar<span style="font-size:18px;"> android:id="@+id/seek_sacle" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:layout_marginBottom="10dp" android:layout_below="@id/seek_saturation" /></RelativeLayout></span>
PrimaryColor.java
在initData()中擷取ImageView顯示的BitMap和給SeekBar顯示最大值和預設顯示的進度
以設定色相的為例:
顏色的最大值為255,最小值為0,
private final int MAX_VALUE = 255; private final int MID_VALUE = 127;
seek_rorate.setMax(MAX_VALUE); seek_rorate.setProgress(MID_VALUE);
給SeekBar設定進度改變監聽器
@Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { switch (seekBar.getId()) { case R.id.seek_rorate://色相 rorate = ((progress - MID_VALUE) * 1.0f / MID_VALUE * 180); break; case R.id.seek_sacle://亮度 sacle = progress * 1.0f / MID_VALUE; break; case R.id.seek_saturation://透明度 saturation = progress * 1.0f / MID_VALUE; break; } image.setImageBitmap(ImageHelper.handleImageEffect(bitmap, rorate, saturation, sacle)); }
:
第一個seekBar調節色相,第二個seekBar調節透明度,第三個seekBar調節亮度
2.使用顏色矩陣處理Android圖片
顏色矩陣為4*5矩陣
使用顏色矩陣來設定圖片
(1)建立新的BitMap
(2)使用ColorMatrix設定顏色矩陣
(3)使用畫布連接新的BitMap
(4)設定畫筆的顏色選取器-顏色矩陣
(5)使用畫筆在畫布上畫出新的BitMap
/** * 通過顏色矩陣獲得BitMap * * @param bitmap * @param flagColor 顏色矩陣 * @return */ public static Bitmap handleImageColorMatrix(Bitmap bitmap, float[] flagColor) { Bitmap bitmap1 = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888); ColorMatrix matrix = new ColorMatrix(); matrix.set(flagColor); Canvas canvas = new Canvas(bitmap1); Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setColorFilter(new ColorMatrixColorFilter(matrix)); canvas.drawBitmap(bitmap, 0, 0, paint); return bitmap1; }
matrix_color_activity.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center_horizontal" tools:context=".MainActivity"> <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:layout_marginBottom="10dp" android:layout_weight="2" /> <GridLayout android:layout_gravity="center_horizontal" android:id="@+id/gridlayout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="3" android:rowCount="4" android:columnCount="5"></GridLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="5dp" android:layout_gravity="bottom"> <Button android:id="@+id/change" android:text="Change" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/reset" android:text="Reset" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout></LinearLayout>
初始化顏色色值
private float[] colorMatrix = new float[20];
private void initColorMatrix() { for (int i = 0; i < 20; i++) { if (i % 6 == 0) { colorMatrix[i] = 1; } else { colorMatrix[i] = 0; } } }
將EditText添加到GridLayout中
private void initEditTextMatrix() { EditText editText; for (int i = 0; i < 20; i++) { editText = new EditText(MatrixColor.this); editText.setText(String.valueOf(colorMatrix[i])); editTextMatrix[i] = editText; gridlayout.addView(editText); } }
為什麼要預設是設定這樣的矩陣值,因為這樣的矩陣與其他的矩陣相乘,都不會改變原來矩陣值。
當改變了矩陣中的值,我們要點擊Change,將新的矩陣值,設定給BitMap,
private void change(Bitmap bitmap, float[] flagColor) { getEditText(); Bitmap bitmap1 = ImageHelper.handleImageColorMatrix(bitmap, flagColor); //設定個ImageView image.setImageBitmap(bitmap1); }
當點擊了Reset後,會把預設的值設定給BitMap
private void reset() { initColorMatrix(); setEditText(); change(bitmap, colorMatrix); } private void setEditText() { for (int i = 0; i < editTextMatrix.length; i++) { editTextMatrix[i].setText(String.valueOf(colorMatrix[i])); } }
:
第一行改變紅色,第二行改變綠色,第三行改變藍色
3.通過像素值處理映像
(1)建立新的BitMap
(2)擷取當前BitMap的像素數組
(3)通過演算法,擷取新的像素數組
(4)給新的BitMap設定像素數組
/** * 擷取底片-通過控制像素 * * @param bitmap * @return */ public static Bitmap handleImagePixel(Bitmap bitmap) { //擷取BitMap的長和寬,其實就是像素的值 int width = bitmap.getWidth(); int height = bitmap.getHeight(); //建立新的BitMap Bitmap newBitMap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); int[] oldPixel = new int[width * height]; int[] newPixel = new int[width * height]; int r, g, b, a, r1, g1, b1; //擷取當前BitMap的像素 //接收像素的數組,擷取像素數組的開始位置,擷取像素的寬度,讀取像素的開始的x座標,讀取像素的開始的y座標,計算的像素的寬度範圍,計算的像素的高度度範圍 bitmap.getPixels(oldPixel, 0, width, 0, 0, width, height); for (int i = 0; i < oldPixel.length; i++) { //擷取像素值 int color = oldPixel[i]; //將像素值轉化成紅色,綠色,藍色和透明度 r = Color.red(color); g = Color.green(color); b = Color.blue(color); a = Color.alpha(color); //擷取新的紅色,綠色,藍色和透明度的值,這隻是其中的一個演算法 r1 = 255 - r; g1 = 255 - g; b1 = 255 - b; //因為顏色色值範圍是0~255,不能超過這個範圍 if (r1 < 0) { r1 = 0; } else if (r1 > 255) { r1 = 255; } if (g1 < 0) { g1 = 0; } else if (g1 > 255) { g1 = 255; } if (b1 < 0) { b1 = 0; } else if (b1 > 255) { b1 = 255; } //將新的紅色,綠色,藍色和透明度轉為ARGB顏色色值。 int newColor = Color.argb(a, r1, g1, b1); //添加到新的像素數組 newPixel[i] = newColor; } //給新的BitMap設定像素數組 newBitMap.setPixels(newPixel, 0, width, 0, 0, width, height); return newBitMap; }
pixel_color_activity.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center_horizontal" tools:context=".MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_weight="1"> <ImageView android:id="@+id/image1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" /> <ImageView android:id="@+id/image2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" /> </LinearLayout></LinearLayout>
PixelColor.java
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.beauty); image1 = (ImageView) findViewById(R.id.image1); image2 = (ImageView) findViewById(R.id.image2); image1.setImageBitmap(bitmap); image2.setImageBitmap(ImageHelper.handleImagePixel(bitmap));
:
源碼下載: http://download.csdn.net/detail/forwardyzk/8512191
Android影像處理