Image Processing System for Android Learning (1)

Source: Internet
Author: User
Image Processing System for Android Learning (1)
Source code: Android image processing system 1.0

Is the software running:


This article only provides the functions of image opening and simple processing algorithms (image brightening, median filtering, and smoothing filtering. When the software is running, the system first calls the lenna image in the software and displays it on the imageview. You can also select images in the media repository. Click the select algorithm button, select the corresponding processing algorithm (currently there are only three types), and then process it. All image processing algorithms are managed by the imageprocess class. Systemmain. Java is the main function of the system and is responsible for calling other activities. Selectalgactivity. Java displays the algorithm list for users to choose from. Reprinted please declare: 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. graphi CS. 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 textvie W aboutview; private onclicklistener listener = 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 = new intent. this, selectalgactivity. class); startactivity (intent) ;}}; aboutbtnlistener = new onclicklistener () {@ overridepublic void onclick (view V) {// todo auto-generated method stubintent intent = new intent (systemmain. this, Ctivityabout. class); startactivity (intent) ;}}; setcontrol (); showimage (null);} private void setcontrol () {selectalgbtn = (button) findviewbyid (R. id. proc1_tn); 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 (listener); intentfilter filter_dynamic = new intentfilter (); iterator (dynamicaction_broadcast); registerreceiver (dynamicreceiver, filter_dynamic);} // 2 custom dynamic broadcast receiver, internal class, receive the selected algorithm private broadcastreceiver dynamicreceiver = new broadca Streceiver () {@ override public void onreceive (context, 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 = getcontentresolver (). query (selectedimage, filepathcolumn, 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) {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 AC Tiancimplements onitemclicklistener {Private Static final string dynamicaction_broadcast = "broadcast. njupt. ZHB. selectalg "; private listview; Public void sendflagtoactivity (string flag) {intent = new intent (); intent. setaction (dynamicaction_broadcast); intent. putextra ("selectflag", flag); sendbroadcast (intent) ;}@ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (SAV Edinstancestate); 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); // bind listener interface} private list <string> getdata () {list <string> DATA = new arraylist <string> (); data. add ("image brightening"); data. add ("median filter"); data. add ("smooth filtering "); Return data;}/* implement the onitemclicklistener interface */@ 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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.