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

下图是软件运行的截图:


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


SystemMain.java
  1. package com.example.njupt.zhb.imageprocesssystem;  
  2. import java.io.File;  
  3. import android.net.Uri;  
  4. import android.os.Bundle;  
  5. import android.provider.MediaStore;  
  6. import android.app.Activity;  
  7. import android.content.BroadcastReceiver;  
  8. import android.content.Context;  
  9. import android.content.Intent;  
  10. import android.content.IntentFilter;  
  11. import android.database.Cursor;  
  12. import android.graphics.Bitmap;  
  13. import android.graphics.BitmapFactory;  
  14. import android.view.Menu;  
  15. import android.view.View;  
  16. import android.view.View.OnClickListener;  
  17. import android.widget.Button;  
  18. import android.widget.ImageView;  
  19. import android.widget.TextView;  
  20. import android.widget.Toast;  
  21.   
  22. public class SystemMain extends Activity {  
  23.     private Button selectImgBtn;  
  24.     private Button selectAlgBtn;  
  25.     private Button aboutBtn;  
  26.     private TextView filepathView;  
  27.     private TextView aboutView;  
  28.     private OnClickListener seleImgBtnListener=null;  
  29.     private OnClickListener seleAlgBtnListener=null;  
  30.     private OnClickListener aboutBtnListener=null;  
  31.     private static int RESULT_LOAD_IMAGE = 123;  
  32.     private String picturePath=null;  
  33.     private Bitmap myBitmap;  
  34.     private ImageView myImageView;  
  35.     private ImageProcess myImageProcess=new ImageProcess();  
  36.     private static final String DYNAMICACTION_Broadcast = "Broadcast.njupt.zhb.selectAlg";  
  37.     @Override  
  38.     public void onCreate(Bundle savedInstanceState) {  
  39.         super.onCreate(savedInstanceState);  
  40.         setTitle("ImageProcessing Made by ZhengHaibo");  
  41.         setContentView(R.layout.activity_system_main);  
  42.         seleImgBtnListener= new View.OnClickListener() {  
  43.             @Override  
  44.             public void onClick(View arg0) {  
  45.                 Intent i = new Intent(  
  46.                         Intent.ACTION_PICK,  
  47.                         android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);  
  48.                 startActivityForResult(i, RESULT_LOAD_IMAGE);  
  49.             }  
  50.         };  
  51.         seleAlgBtnListener=new OnClickListener() {  
  52.             @Override  
  53.             public void onClick(View v) {  
  54.                 Intent intent=new Intent(SystemMain.this,SelectAlgActivity.class);  
  55.                 startActivity(intent);  
  56.             }  
  57.         };  
  58.         aboutBtnListener=new OnClickListener() {  
  59.               
  60.             @Override  
  61.             public void onClick(View v) {  
  62.                 // TODO Auto-generated method stub  
  63.                 Intent intent=new Intent(SystemMain.this,ActivityAbout.class);  
  64.                 startActivity(intent);  
  65.             }  
  66.         };  
  67.         SetControl();  
  68.         ShowImage(null);  
  69.     }  
  70.     private void SetControl(){  
  71.         selectAlgBtn=(Button)findViewById(R.id.processBtn);  
  72.         selectImgBtn=(Button)findViewById(R.id.SelectBtn);  
  73.         aboutBtn=(Button)findViewById(R.id.AboutBtn);  
  74.         filepathView=(TextView)findViewById(R.id.ImagePath);  
  75.         aboutView=(TextView)findViewById(R.id.AboutTextView);  
  76.         myImageView=(ImageView)findViewById(R.id.imageshow);  
  77.         selectAlgBtn.setOnClickListener(seleAlgBtnListener);  
  78.         selectImgBtn.setOnClickListener(seleImgBtnListener);  
  79.         aboutBtn.setOnClickListener(aboutBtnListener);  
  80.         IntentFilter filter_dynamic = new IntentFilter();  
  81.         filter_dynamic.addAction(DYNAMICACTION_Broadcast);  
  82.         registerReceiver(dynamicReceiver, filter_dynamic);  
  83.     }  
  84.     // 2 自定义动态广播接收器,内部类,接收选择的算法  
  85.     private BroadcastReceiver dynamicReceiver = new BroadcastReceiver() {  
  86.         @Override  
  87.         public void onReceive(Context context, Intent intent) {  
  88.             if(intent.getAction().equals(DYNAMICACTION_Broadcast)){  
  89.                 Toast.makeText(SystemMain.this"Please wait ...", Toast.LENGTH_SHORT).show();  
  90.                 String seleFlag = intent.getStringExtra("selectFlag");  
  91.                 int ch=Integer.parseInt(seleFlag);  
  92.                 switch(ch){  
  93.                 case 0:  
  94.                     ShowImage(myImageProcess.brighten(10, myBitmap));  
  95.                     break;  
  96.                 case 1:  
  97.                     ShowImage(myImageProcess.averageFilter(3,3,myBitmap));  
  98.                     break;  
  99.                 case 2:  
  100.                     ShowImage(myImageProcess.averageFilter(3,3,myBitmap));  
  101.                     break;  
  102.                 default:  
  103.                 Toast.makeText(SystemMain.this"Wrong!", Toast.LENGTH_SHORT).show();  
  104.                         break;  
  105.                 }  
  106.                 Toast.makeText(SystemMain.this"Processing finished!", Toast.LENGTH_SHORT).show();  
  107.             }  
  108.         }  
  109.     };  
  110.     private Bitmap FilesToBitmap(String filename){  
  111.         Bitmap temp=null;  
  112.         if(filename!=null){  
  113.             File imageFile = new File(filename);  
  114.             if (imageFile.exists())  
  115.             {  
  116.                 // Load the image from file  
  117.                 temp = BitmapFactory.decodeFile(filename);  
  118.             }  
  119.               
  120.         }  
  121.         return temp;  
  122.     }  
  123.     public void ShowImage(Bitmap bitmap){  
  124.         if (bitmap!=null) {  
  125.             myImageView.setImageBitmap(bitmap);  
  126.         }  
  127.         else {  
  128.            bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.lenna);  
  129.            myImageView.setImageBitmap(bitmap);  
  130.            myBitmap=bitmap;  
  131.         }  
  132.     }  
  133.     @Override  
  134.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
  135.         super.onActivityResult(requestCode, resultCode, data);  
  136.    
  137.         if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {  
  138.             Uri selectedImage = data.getData();  
  139.             String[] filePathColumn = { MediaStore.Images.Media.DATA };  
  140.    
  141.             Cursor cursor = getContentResolver().query(selectedImage,  
  142.                     filePathColumn, nullnullnull);  
  143.             cursor.moveToFirst();  
  144.             int columnIndex = cursor.getColumnIndex(filePathColumn[0]);  
  145.             picturePath = cursor.getString(columnIndex);  
  146.             cursor.close();  
  147.             filepathView.setText(picturePath);  
  148.             //imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));  
  149.             myBitmap=FilesToBitmap(picturePath);  
  150.             ShowImage(myBitmap);  
  151.         }  
  152.     }  
  153.     @Override  
  154.     public boolean onCreateOptionsMenu(Menu menu) {  
  155.         getMenuInflater().inflate(R.menu.activity_system_main, menu);  
  156.         return true;  
  157.     }  
  158. }  
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
  1. package com.example.njupt.zhb.imageprocesssystem;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5. import android.app.Activity;  
  6. import android.content.Intent;  
  7. import android.os.Bundle;  
  8. import android.view.View;  
  9. import android.widget.AdapterView;  
  10. import android.widget.ArrayAdapter;  
  11. import android.widget.ListView;  
  12. import android.widget.AdapterView.OnItemClickListener;  
  13. public class SelectAlgActivity extends Activity implements OnItemClickListener{  
  14.     private static final String DYNAMICACTION_Broadcast = "Broadcast.njupt.zhb.selectAlg";  
  15.     private ListView listView;  
  16.     public void sendFlagToActivity(String flag){  
  17.         Intent intent = new Intent();  
  18.         intent.setAction(DYNAMICACTION_Broadcast);  
  19.         intent.putExtra("selectFlag", flag);  
  20.         sendBroadcast(intent);  
  21.     }  
  22.     @Override  
  23.     public void onCreate(Bundle savedInstanceState) {  
  24.         super.onCreate(savedInstanceState);  
  25.         setTitle("Choose Image processing type!");  
  26.         listView = new ListView(this);  
  27.         List<String> list=getData();  
  28.         ArrayAdapter<String> adapter=new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1,list);  
  29.         listView.setAdapter(adapter);  
  30.         setContentView(listView);  
  31.         listView.setOnItemClickListener(this);//绑定监听接口    
  32.     }  
  33.     private List<String> getData(){  
  34.         List<String> data = new ArrayList<String>();  
  35.         data.add("图像变亮");  
  36.         data.add("中值滤波");  
  37.         data.add("平滑滤波");  
  38.         return data;  
  39.     }  
  40.     /*实现OnItemClickListener接口*/  
  41.     @Override  
  42.     public void onItemClick(AdapterView<?> parent, View v, int position, long id) {  
  43.         //finish();  
  44.         String posString=Integer.toString(position);  
  45.         sendFlagToActivity(posString);  
  46.         finish();  
  47.     }  
  48. }  
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
  1. package com.example.njupt.zhb.imageprocesssystem;  
  2.   
  3. import android.graphics.Bitmap;  
  4.   
  5. public class ImageProcess {  
  6.     public ImageProcess() {  
  7.         // TODO Auto-generated constructor stub  
  8.     }  
  9.     public Bitmap brighten(int brightenOffset,Bitmap myBitmap)  
  10.     {  
  11.         // Create new array  
  12.         int width = myBitmap.getWidth();  
  13.         int height = myBitmap.getHeight();  
  14.         int[] pix = new int[width * height];  
  15.         myBitmap.getPixels(pix, 0, width, 00, width, height);  
  16.           
  17.         // Apply pixel-by-pixel change  
  18.         int index = 0;  
  19.         for (int y = 0; y < height; y++)  
  20.         {  
  21.             for (int x = 0; x < width; x++)  
  22.             {  
  23.                 int r = (pix[index] >> 16) & 0xff;  
  24.                 int g = (pix[index] >> 8) & 0xff;  
  25.                 int b = pix[index] & 0xff;  
  26.                 r = Math.max(0, Math.min(255, r + brightenOffset));  
  27.                 g = Math.max(0, Math.min(255, g + brightenOffset));  
  28.                 b = Math.max(0, Math.min(255, b + brightenOffset));  
  29.                 pix[index] = 0xff000000 | (r << 16) | (g << 8) | b;  
  30.                 index++;  
  31.             } // x  
  32.         } // y  
  33.           
  34.         // Change bitmap to use new array  
  35.         Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);  
  36.         bitmap.setPixels(pix, 0, width, 00, width, height);         
  37.         myBitmap = null;  
  38.         pix = null;  
  39.         return bitmap;  
  40.     }  
  41.       
  42.     // filterWidth and filterHeight must be odd numbers  
  43.     public Bitmap averageFilter(int filterWidth, int filterHeight,Bitmap myBitmap)  
  44.     {  
  45.         // Create new array  
  46.         int width = myBitmap.getWidth();  
  47.         int height = myBitmap.getHeight();  
  48.         int[] pixNew = new int[width * height];  
  49.         int[] pixOld = new int[width * height];  
  50.         myBitmap.getPixels(pixNew, 0, width, 00, width, height);  
  51.         myBitmap.getPixels(pixOld, 0, width, 00, width, height);  
  52.           
  53.         // Apply pixel-by-pixel change  
  54.         int filterHalfWidth = filterWidth/2;  
  55.         int filterHalfHeight = filterHeight/2;  
  56.         int filterArea = filterWidth * filterHeight;  
  57.         for (int y = filterHalfHeight; y < height-filterHalfHeight; y++)  
  58.         {  
  59.             for (int x = filterHalfWidth; x < width-filterHalfWidth; x++)  
  60.             {  
  61.                 // Accumulate values in neighborhood  
  62.                 int accumR = 0, accumG = 0, accumB = 0;  
  63.                 for (int dy = -filterHalfHeight; dy <= filterHalfHeight; dy++)  
  64.                 {  
  65.                     for (int dx = -filterHalfWidth; dx <= filterHalfWidth; dx++)  
  66.                     {  
  67.                         int index = (y+dy)*width + (x+dx);  
  68.                         accumR += (pixOld[index] >> 16) & 0xff;  
  69.                         accumG += (pixOld[index] >> 8) & 0xff;  
  70.                         accumB += pixOld[index] & 0xff;  
  71.                     } // dx  
  72.                 } // dy  
  73.                   
  74.                 // Normalize  
  75.                 accumR /= filterArea;  
  76.                 accumG /= filterArea;  
  77.                 accumB /= filterArea;  
  78.                 int index = y*width + x;  
  79.                 pixNew[index] = 0xff000000 | (accumR << 16) | (accumG << 8) | accumB;  
  80.             } // x  
  81.         } // y  
  82.           
  83.         // Change bitmap to use new array  
  84.         Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);  
  85.         bitmap.setPixels(pixNew, 0, width, 00, width, height);  
  86.         myBitmap = null;  
  87.         pixOld = null;  
  88.         pixNew = null;  
  89.         return bitmap;  
  90.     }  
  91.       
  92.     // filterWidth and filterHeight must be odd numbers  
  93.     public Bitmap medianFilter(int filterWidth, int filterHeight,Bitmap myBitmap)  
  94.     {  
  95.         // Create new array  
  96.         int width = myBitmap.getWidth();  
  97.         int height = myBitmap.getHeight();  
  98.         int[] pixNew = new int[width * height];  
  99.         int[] pixOld = new int[width * height];  
  100.         myBitmap.getPixels(pixNew, 0, width, 00, width, height);  
  101.         myBitmap.getPixels(pixOld, 0, width, 00, width, height);  
  102.           
  103.         // Apply pixel-by-pixel change  
  104.         int filterHalfWidth = filterWidth/2;  
  105.         int filterHalfHeight = filterHeight/2;  
  106.         int filterArea = filterWidth * filterHeight;  
  107.         for (int y = filterHalfHeight; y < height-filterHalfHeight; y++)  
  108.         {  
  109.             for (int x = filterHalfWidth; x < width-filterHalfWidth; x++)  
  110.             {  
  111.                 // Accumulate values in neighborhood  
  112.                 int accumR = 0, accumG = 0, accumB = 0;  
  113.                 for (int dy = -filterHalfHeight; dy <= filterHalfHeight; dy++)  
  114.                 {  
  115.                     for (int dx = -filterHalfWidth; dx <= filterHalfWidth; dx++)  
  116.                     {  
  117.                         int index = (y+dy)*width + (x+dx);  
  118.                         accumR += (pixOld[index] >> 16) & 0xff;  
  119.                         accumG += (pixOld[index] >> 8) & 0xff;  
  120.                         accumB += pixOld[index] & 0xff;  
  121.                     } // dx  
  122.                 } // dy  
  123.                   
  124.                 // Normalize  
  125.                 accumR /= filterArea;  
  126.                 accumG /= filterArea;  
  127.                 accumB /= filterArea;  
  128.                 int index = y*width + x;  
  129.                 pixNew[index] = 0xff000000 | (accumR << 16) | (accumG << 8) | accumB;  
  130.             } // x  
  131.         } // y  
  132.           
  133.         // Change bitmap to use new array  
  134.         Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);  
  135.         bitmap.setPixels(pixNew, 0, width, 00, width, height);  
  136.         myBitmap = null;  
  137.         pixOld = null;  
  138.         pixNew = null;  
  139.         return bitmap;  
  140.     }  
  141. }  
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

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.     android:id="@+id/widget38"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     android:orientation="vertical"  
  7.     xmlns:android="http://schemas.android.com/apk/res/android">  
  8. <TextView  
  9.     android:id="@+id/ImagePath"  
  10.     android:layout_width="fill_parent"  
  11.     android:layout_height="wrap_content"  
  12.     android:text="ImageProcess" />  
  13. <Button  
  14.     android:id="@+id/SelectBtn"  
  15.     android:layout_width="fill_parent"  
  16.     android:layout_height="wrap_content"  
  17.     android:text="Select Image" />  
  18. <Button  
  19.     android:id="@+id/processBtn"  
  20.     android:layout_width="fill_parent"  
  21.     android:layout_height="wrap_content"  
  22.     android:text="Select Image Pcocessing Algorithm" />  
  23. <ImageView  
  24.     android:id="@+id/imageshow"  
  25.     android:layout_width="fill_parent"  
  26.     android:layout_height="wrap_content" />  
  27. <Button  
  28.     android:id="@+id/AboutBtn"  
  29.     android:layout_width="fill_parent"  
  30.     android:layout_height="wrap_content"  
  31.     android:text="About author" />  
  32. <TextView  
  33.     android:id="@+id/AboutTextView"  
  34.     android:layout_width="fill_parent"  
  35.     android:layout_height="wrap_content"  
  36.     android:text="zhb931706659@126.com  njupt zhb" />  
  37. </LinearLayout>  
<?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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值