android 学习之图像处理系统(一)

android 学习之图像处理系统(一)
源代码: android图像处理系统1.0

下图是软件运行的截图:


本文只做了图像的打开和简单处理算法(图像的变亮、中值滤波、平滑滤波)的功能。其中,当软件运行时,首先调用软件内的lenna图像,显示在ImageView上。用户也可以选择媒体库中的图像。点击选择算法按钮,选择相应的处理算法(目前仅有3种),然后进行处理。图像处理的所有算法实现均由ImageProcess类管理。SystemMain.java是系统的主函数,负责调用其他Activity等。SelectAlgActivity.java是显示算法列表的,供用户选择。转载请声明:http://blog.csdn.net/nuptboyzhb/article/details/7852999


SystemMain.java
package com.example.njupt.zhb.imageprocesssystem;
import java.io.File;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class SystemMain extends Activity {
    private Button selectImgBtn;
    private Button selectAlgBtn;
    private Button aboutBtn;
    private TextView filepathView;
    private TextView aboutView;
    private OnClickListener seleImgBtnListener=null;
    private OnClickListener seleAlgBtnListener=null;
    private OnClickListener aboutBtnListener=null;
    private static int RESULT_LOAD_IMAGE = 123;
    private String picturePath=null;
    private Bitmap myBitmap;
    private ImageView myImageView;
    private ImageProcess myImageProcess=new ImageProcess();
    private static final String DYNAMICACTION_Broadcast = "Broadcast.njupt.zhb.selectAlg";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle("ImageProcessing Made by ZhengHaibo");
        setContentView(R.layout.activity_system_main);
        seleImgBtnListener= new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                Intent i = new Intent(
                        Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(i, RESULT_LOAD_IMAGE);
            }
        };
        seleAlgBtnListener=new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent=new Intent(SystemMain.this,SelectAlgActivity.class);
				startActivity(intent);
			}
		};
		aboutBtnListener=new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent=new Intent(SystemMain.this,ActivityAbout.class);
				startActivity(intent);
			}
		};
		SetControl();
		ShowImage(null);
    }
    private void SetControl(){
    	selectAlgBtn=(Button)findViewById(R.id.processBtn);
    	selectImgBtn=(Button)findViewById(R.id.SelectBtn);
    	aboutBtn=(Button)findViewById(R.id.AboutBtn);
    	filepathView=(TextView)findViewById(R.id.ImagePath);
    	aboutView=(TextView)findViewById(R.id.AboutTextView);
    	myImageView=(ImageView)findViewById(R.id.imageshow);
    	selectAlgBtn.setOnClickListener(seleAlgBtnListener);
    	selectImgBtn.setOnClickListener(seleImgBtnListener);
    	aboutBtn.setOnClickListener(aboutBtnListener);
		IntentFilter filter_dynamic = new IntentFilter();
		filter_dynamic.addAction(DYNAMICACTION_Broadcast);
		registerReceiver(dynamicReceiver, filter_dynamic);
    }
    // 2 自定义动态广播接收器,内部类,接收选择的算法
  	private BroadcastReceiver dynamicReceiver = new BroadcastReceiver() {
  		@Override
  		public void onReceive(Context context, Intent intent) {
  			if(intent.getAction().equals(DYNAMICACTION_Broadcast)){
  				Toast.makeText(SystemMain.this, "Please wait ...", Toast.LENGTH_SHORT).show();
  				String seleFlag = intent.getStringExtra("selectFlag");
                int ch=Integer.parseInt(seleFlag);
                switch(ch){
                case 0:
                	ShowImage(myImageProcess.brighten(10, myBitmap));
                	break;
                case 1:
                	ShowImage(myImageProcess.averageFilter(3,3,myBitmap));
                	break;
                case 2:
                	ShowImage(myImageProcess.averageFilter(3,3,myBitmap));
                	break;
                default:
                Toast.makeText(SystemMain.this, "Wrong!", Toast.LENGTH_SHORT).show();
                		break;
                }
                Toast.makeText(SystemMain.this, "Processing finished!", Toast.LENGTH_SHORT).show();
  			}
  		}
  	};
    private Bitmap FilesToBitmap(String filename){
    	Bitmap temp=null;
    	if(filename!=null){
        	File imageFile = new File(filename);
            if (imageFile.exists())
            {
            	// Load the image from file
            	temp = BitmapFactory.decodeFile(filename);
            }
        	
    	}
    	return temp;
    }
    public void ShowImage(Bitmap bitmap){
    	if (bitmap!=null) {
    		myImageView.setImageBitmap(bitmap);
		}
    	else {
		   bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.lenna);
		   myImageView.setImageBitmap(bitmap);
		   myBitmap=bitmap;
		}
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
 
        if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
            Uri selectedImage = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };
 
            Cursor cursor = getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
            cursor.moveToFirst();
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            picturePath = cursor.getString(columnIndex);
            cursor.close();
            filepathView.setText(picturePath);
            //imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
            myBitmap=FilesToBitmap(picturePath);
            ShowImage(myBitmap);
        }
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_system_main, menu);
        return true;
    }
}


SelectAlgActivity.java
package com.example.njupt.zhb.imageprocesssystem;

import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
public class SelectAlgActivity extends Activity implements OnItemClickListener{
	private static final String DYNAMICACTION_Broadcast = "Broadcast.njupt.zhb.selectAlg";
	private ListView listView;
	public void sendFlagToActivity(String flag){
		Intent intent = new Intent();
		intent.setAction(DYNAMICACTION_Broadcast);
		intent.putExtra("selectFlag", flag);
		sendBroadcast(intent);
	}
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTitle("Choose Image processing type!");
		listView = new ListView(this);
		List<String> list=getData();
		ArrayAdapter<String> adapter=new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1,list);
		listView.setAdapter(adapter);
		setContentView(listView);
		listView.setOnItemClickListener(this);//绑定监听接口	
	}
	private List<String> getData(){
		List<String> data = new ArrayList<String>();
		data.add("图像变亮");
		data.add("中值滤波");
		data.add("平滑滤波");
		return data;
	}
	/*实现OnItemClickListener接口*/
	@Override
	public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
		//finish();
		String posString=Integer.toString(position);
		sendFlagToActivity(posString);
		finish();
	}
}


ImageProcess.java
package com.example.njupt.zhb.imageprocesssystem;

import android.graphics.Bitmap;

public class ImageProcess {
	public ImageProcess() {
		// TODO Auto-generated constructor stub
	}
	public Bitmap brighten(int brightenOffset,Bitmap myBitmap)
    {
    	// Create new array
    	int width = myBitmap.getWidth();
    	int height = myBitmap.getHeight();
    	int[] pix = new int[width * height];
    	myBitmap.getPixels(pix, 0, width, 0, 0, width, height);
    	
    	// Apply pixel-by-pixel change
    	int index = 0;
    	for (int y = 0; y < height; y++)
    	{
    		for (int x = 0; x < width; x++)
    		{
    			int r = (pix[index] >> 16) & 0xff;
    			int g = (pix[index] >> 8) & 0xff;
    			int b = pix[index] & 0xff;
    			r = Math.max(0, Math.min(255, r + brightenOffset));
    			g = Math.max(0, Math.min(255, g + brightenOffset));
    			b = Math.max(0, Math.min(255, b + brightenOffset));
    			pix[index] = 0xff000000 | (r << 16) | (g << 8) | b;
    			index++;
    		} // x
    	} // y
    	
    	// Change bitmap to use new array
    	Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
    	bitmap.setPixels(pix, 0, width, 0, 0, width, height);    	
    	myBitmap = null;
    	pix = null;
    	return bitmap;
    }
    
    // filterWidth and filterHeight must be odd numbers
    public Bitmap averageFilter(int filterWidth, int filterHeight,Bitmap myBitmap)
    {
    	// Create new array
    	int width = myBitmap.getWidth();
    	int height = myBitmap.getHeight();
    	int[] pixNew = new int[width * height];
    	int[] pixOld = new int[width * height];
    	myBitmap.getPixels(pixNew, 0, width, 0, 0, width, height);
    	myBitmap.getPixels(pixOld, 0, width, 0, 0, width, height);
    	
    	// Apply pixel-by-pixel change
    	int filterHalfWidth = filterWidth/2;
    	int filterHalfHeight = filterHeight/2;
    	int filterArea = filterWidth * filterHeight;
    	for (int y = filterHalfHeight; y < height-filterHalfHeight; y++)
    	{
    		for (int x = filterHalfWidth; x < width-filterHalfWidth; x++)
    		{
    			// Accumulate values in neighborhood
    			int accumR = 0, accumG = 0, accumB = 0;
    			for (int dy = -filterHalfHeight; dy <= filterHalfHeight; dy++)
    			{
    				for (int dx = -filterHalfWidth; dx <= filterHalfWidth; dx++)
    				{
    					int index = (y+dy)*width + (x+dx);
    					accumR += (pixOld[index] >> 16) & 0xff;
    					accumG += (pixOld[index] >> 8) & 0xff;
    					accumB += pixOld[index] & 0xff;
    				} // dx
    			} // dy
    			
    			// Normalize
    			accumR /= filterArea;
    			accumG /= filterArea;
    			accumB /= filterArea;
    			int index = y*width + x;
    			pixNew[index] = 0xff000000 | (accumR << 16) | (accumG << 8) | accumB;
    		} // x
    	} // y
    	
    	// Change bitmap to use new array
    	Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
    	bitmap.setPixels(pixNew, 0, width, 0, 0, width, height);
    	myBitmap = null;
    	pixOld = null;
    	pixNew = null;
    	return bitmap;
    }
    
    // filterWidth and filterHeight must be odd numbers
    public Bitmap medianFilter(int filterWidth, int filterHeight,Bitmap myBitmap)
    {
    	// Create new array
    	int width = myBitmap.getWidth();
    	int height = myBitmap.getHeight();
    	int[] pixNew = new int[width * height];
    	int[] pixOld = new int[width * height];
    	myBitmap.getPixels(pixNew, 0, width, 0, 0, width, height);
    	myBitmap.getPixels(pixOld, 0, width, 0, 0, width, height);
    	
    	// Apply pixel-by-pixel change
    	int filterHalfWidth = filterWidth/2;
    	int filterHalfHeight = filterHeight/2;
    	int filterArea = filterWidth * filterHeight;
    	for (int y = filterHalfHeight; y < height-filterHalfHeight; y++)
    	{
    		for (int x = filterHalfWidth; x < width-filterHalfWidth; x++)
    		{
    			// Accumulate values in neighborhood
    			int accumR = 0, accumG = 0, accumB = 0;
    			for (int dy = -filterHalfHeight; dy <= filterHalfHeight; dy++)
    			{
    				for (int dx = -filterHalfWidth; dx <= filterHalfWidth; dx++)
    				{
    					int index = (y+dy)*width + (x+dx);
    					accumR += (pixOld[index] >> 16) & 0xff;
    					accumG += (pixOld[index] >> 8) & 0xff;
    					accumB += pixOld[index] & 0xff;
    				} // dx
    			} // dy
    			
    			// Normalize
    			accumR /= filterArea;
    			accumG /= filterArea;
    			accumB /= filterArea;
    			int index = y*width + x;
    			pixNew[index] = 0xff000000 | (accumR << 16) | (accumG << 8) | accumB;
    		} // x
    	} // y
    	
    	// Change bitmap to use new array
    	Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
    	bitmap.setPixels(pixNew, 0, width, 0, 0, width, height);
    	myBitmap = null;
    	pixOld = null;
    	pixNew = null;
    	return bitmap;
    }
}


main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
	android:id="@+id/widget38"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent"
	android:orientation="vertical"
	xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
	android:id="@+id/ImagePath"
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	android:text="ImageProcess" />
<Button
	android:id="@+id/SelectBtn"
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	android:text="Select Image" />
<Button
	android:id="@+id/processBtn"
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	android:text="Select Image Pcocessing Algorithm" />
<ImageView
	android:id="@+id/imageshow"
	android:layout_width="fill_parent"
	android:layout_height="wrap_content" />
<Button
	android:id="@+id/AboutBtn"
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	android:text="About author" />
<TextView
	android:id="@+id/AboutTextView"
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	android:text="zhb931706659@126.com  njupt zhb" />
</LinearLayout>



评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值