android 學習之影像處理系統(一)

來源:互聯網
上載者:User
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() {@Overridepublic void onClick(View v) {Intent intent=new Intent(SystemMain.this,SelectAlgActivity.class);startActivity(intent);}};aboutBtnListener=new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubIntent 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);}@Overridepublic 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介面*/@Overridepublic 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"?><LinearLayoutandroid: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"><TextViewandroid:id="@+id/ImagePath"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="ImageProcess" /><Buttonandroid:id="@+id/SelectBtn"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="Select Image" /><Buttonandroid:id="@+id/processBtn"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="Select Image Pcocessing Algorithm" /><ImageViewandroid:id="@+id/imageshow"android:layout_width="fill_parent"android:layout_height="wrap_content" /><Buttonandroid:id="@+id/AboutBtn"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="About author" /><TextViewandroid:id="@+id/AboutTextView"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="zhb931706659@126.com  njupt zhb" /></LinearLayout>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.