android 底片,浮雕,老照片效果


android 底片,浮雕,老照片效果_第1张图片 android 底片,浮雕,老照片效果_第2张图片


package com.example.and6;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.view.ScrollingView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.ScrollView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class MainActivity3 extends Activity implements OnClickListener {

	Bitmap bitmap;
	ImageView iv;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main5);

		iv = (ImageView) findViewById(R.id.iv);
		bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.pic);

		findViewById(R.id.dipian).setOnClickListener(this);

		findViewById(R.id.old).setOnClickListener(this);

		findViewById(R.id.fudiao).setOnClickListener(this);

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		int w = bitmap.getWidth();
		int h = bitmap.getHeight();

		int color;
		int r, g, b, a;

		Bitmap bmp = Bitmap.createBitmap(w, h, Config.ARGB_8888);

		int[] oldPixs = new int[w * h];
		int[] newPixs = new int[w * h];

		bitmap.getPixels(oldPixs, 0, w, 0, 0, w, h);
		for (int i = 0; i < newPixs.length; i++) {
			color = oldPixs[i];
			r = Color.red(color);
			g = Color.green(color);
			b = Color.blue(color);
			a = Color.alpha(color);

			switch (v.getId()) {
				case R.id.dipian:
					r=255-r;
					g=255-g;
					b=255-b;
					
					if (r>255) {
						r=255;
					}else if (r<0) {
						r=0;
					}
					
					if (g>255) {
						g=255;
					}else if (g<0) {
						g=0;
					}

					if (b>255) {
						b=255;
					}else if (b<0) {
						b=0;
					}

					break;
	
				case R.id.old:
	
					r=(int) (0.393*r+0.769*g+0.189*b);
					g=(int) (0.349*r+0.686*g+0.168*b);
					b=(int) (0.272*r+0.534*g+0.131*b);
					
					break;
	
				case R.id.fudiao:
	
					/*
					 * 浮雕算法
					 * 
					 * 若存在ABC三个像素点,要求B点对应的浮雕效果:
					 * B.r=C.r-B.r+127
					 * B.g=C.g-B.g+127
					 * B.b=C.b-B.b+127
					 */
					if (i<newPixs.length-1) {
						int C=oldPixs[i+1];
						
						r=Color.red(C)-r+127;
						g=Color.green(C)-g+127;
						b=Color.blue(C)-b+127;
						if (r>255) {
							r=255;
						}else if (r<0) {
							r=0;
						}
						
						if (g>255) {
							g=255;
						}else if (g<0) {
							g=0;
						}

						if (b>255) {
							b=255;
						}else if (b<0) {
							b=0;
						}

					}
					break;
	
				default:
					break;
			}
			
			newPixs[i]=Color.argb(a, r, g, b);
		}
		
		bmp.setPixels(newPixs, 0, w, 0, 0, w, h);
		iv.setImageBitmap(bmp);

	}

}


你可能感兴趣的:(android,图像处理)