android图像处理系统框架

图像处理框架的功能简介:

作者:郑海波 2012-08-12 Email:zhb931706659@126.com


1.布局
  有TextView、ImageView和Button三种控件
2.功能:
  2.1 显示:系统初始化时,显示Lenna图像
  2.2 打开图像:Open按钮完成打开图像功能。点击Open按钮,用户可以选择媒体库中的图像;
  2.3 处理图像:Process按钮。当点击时,跳转到一个算法选择的Activity,根据用户的选择,系统开始处理图像;
  2.4 保存图像:Save按钮。将当前显示的图像保存到原图像的路径,即替换原图像。
  2.5 拍照功能:Camera按钮。用户可以打开摄像头,将拍照的图像作为素材。
  2.6 截图功能:Screen按钮。点击按钮,将当前用户的界面截取下来,作为拍照的素材。
  2.7 触摸功能:在ImageView区域触摸,当用户点击图像时,图像由‘原大小’变为‘适合屏幕’或相反。向上滑动,显示系统的lenna图像。向右滑动,显示上一幅图像。向下滑动,显示原图像。

部分代码展示:

布局文件:

  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:id="@+id/RelativeLayout_bg" android:layout_width="fill_parent" 
  4.     android:layout_height="fill_parent"> 
  5.     <!-- 头部 --> 
  6.     <RelativeLayout  
  7.         android:id="@+id/MyLayout_top" 
  8.         android:orientation="horizontal"  
  9.         android:layout_width="fill_parent" 
  10.         android:layout_height="25dp"  
  11.         android:layout_alignParentTop="true" 
  12.         android:gravity="center"> 
  13.         <TextView 
  14.             android:id="@+id/ImagePath" 
  15.             android:layout_width="wrap_content" 
  16.             android:layout_height="wrap_content" 
  17.             android:textColor="#FF00ff00"  
  18.             android:text="ImageProcess" /> 
  19.     </RelativeLayout> 
  20.     <!-- 底部 --> 
  21.     <RelativeLayout 
  22.         android:id="@+id/MyLayout_bottom" 
  23.         android:orientation="horizontal"  
  24.         android:layout_width="fill_parent" 
  25.         android:layout_height="60dp"  
  26.         android:layout_alignParentBottom="true" 
  27.         android:gravity="center"> 
  28.         <!-- 底部第一个横条 --> 
  29.         <LinearLayout 
  30.             android:id="@+id/MyLayout_bottom1" 
  31.             android:orientation="horizontal"  
  32.             android:layout_width="fill_parent" 
  33.             android:layout_height="40dp"  
  34.             android:layout_alignParentTop="true"> 
  35.             <Button 
  36.                    android:id="@+id/SelectBtn" 
  37.                    android:layout_weight="1" 
  38.                    android:layout_width="fill_parent" 
  39.                    android:layout_height="fill_parent" 
  40.                    android:text="Open" /> 
  41.              <Button 
  42.                    android:id="@+id/processBtn" 
  43.                    android:layout_weight="1" 
  44.                    android:layout_width="fill_parent" 
  45.                    android:layout_height="fill_parent" 
  46.                    android:text="Process" /> 
  47.              <Button 
  48.                    android:id="@+id/SaveBtn" 
  49.                    android:layout_weight="1" 
  50.                    android:layout_width="fill_parent" 
  51.                    android:layout_height="fill_parent" 
  52.                    android:text="Save" /> 
  53.               <Button 
  54.                    android:id="@+id/CameraBtn" 
  55.                    android:layout_weight="1" 
  56.                    android:layout_width="fill_parent" 
  57.                    android:layout_height="fill_parent" 
  58.                    android:text="Camera" /> 
  59.               <Button 
  60.                    android:id="@+id/ScreenShotBtn" 
  61.                    android:layout_weight="1" 
  62.                    android:layout_width="fill_parent" 
  63.                    android:layout_height="fill_parent" 
  64.                    android:text="Screen" /> 
  65.              <Button 
  66.                    android:id="@+id/AboutBtn" 
  67.                    android:layout_weight="1" 
  68.                    android:layout_width="fill_parent" 
  69.                    android:layout_height="fill_parent" 
  70.                    android:text="About" /> 
  71.           </LinearLayout> 
  72.           <!-- 底部第二个横条 --> 
  73.           <LinearLayout 
  74.              android:id="@+id/MyLayout_bottom2" 
  75.              android:orientation="horizontal"  
  76.              android:layout_width="fill_parent" 
  77.              android:layout_height="20dp" 
  78.              android:background="#DFDFDF" 
  79.              android:layout_alignParentBottom="true"  
  80.              android:gravity="center"> 
  81.              <TextView 
  82.            android:id="@+id/AboutTextView" 
  83.            android:layout_width="wrap_content" 
  84.            android:layout_height="wrap_content" 
  85.            android:text="zhb931706659@126.com" /> 
  86.           </LinearLayout> 
  87.     </RelativeLayout> 
  88.     <!-- 中部 --> 
  89.     <RelativeLayout  
  90.         android:orientation="horizontal" 
  91.         android:layout_width="fill_parent"  
  92.         android:layout_height="fill_parent" 
  93.         android:layout_above="@id/MyLayout_bottom"  
  94.         android:layout_below="@id/MyLayout_top" 
  95.         android:background="#EFDFDF" 
  96.         android:gravity="center"> 
  97.         <ImageView 
  98.                android:id="@+id/imageshow" 
  99.                android:layout_width="fill_parent" 
  100.                android:layout_height="wrap_content" /> 
  101.     </RelativeLayout> 
  102. </RelativeLayout> 
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout_bg" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <!-- 头部 -->
    <RelativeLayout 
        android:id="@+id/MyLayout_top"
        android:orientation="horizontal" 
        android:layout_width="fill_parent"
        android:layout_height="25dp" 
        android:layout_alignParentTop="true"
        android:gravity="center">
        <TextView
	        android:id="@+id/ImagePath"
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:textColor="#FF00ff00" 
	        android:text="ImageProcess" />
    </RelativeLayout>
    <!-- 底部 -->
    <RelativeLayout
        android:id="@+id/MyLayout_bottom"
        android:orientation="horizontal" 
        android:layout_width="fill_parent"
        android:layout_height="60dp" 
        android:layout_alignParentBottom="true"
        android:gravity="center">
        <!-- 底部第一个横条 -->
        <LinearLayout
            android:id="@+id/MyLayout_bottom1"
            android:orientation="horizontal" 
            android:layout_width="fill_parent"
            android:layout_height="40dp" 
            android:layout_alignParentTop="true">
            <Button
	               android:id="@+id/SelectBtn"
	               android:layout_weight="1"
	               android:layout_width="fill_parent"
	               android:layout_height="fill_parent"
	               android:text="Open" />
             <Button
	               android:id="@+id/processBtn"
	               android:layout_weight="1"
	               android:layout_width="fill_parent"
	               android:layout_height="fill_parent"
	               android:text="Process" />
             <Button
	               android:id="@+id/SaveBtn"
	               android:layout_weight="1"
	               android:layout_width="fill_parent"
	               android:layout_height="fill_parent"
	               android:text="Save" />
              <Button
	               android:id="@+id/CameraBtn"
	               android:layout_weight="1"
	               android:layout_width="fill_parent"
	               android:layout_height="fill_parent"
	               android:text="Camera" />
              <Button
	               android:id="@+id/ScreenShotBtn"
	               android:layout_weight="1"
	               android:layout_width="fill_parent"
	               android:layout_height="fill_parent"
	               android:text="Screen" />
             <Button
	               android:id="@+id/AboutBtn"
	               android:layout_weight="1"
	               android:layout_width="fill_parent"
	               android:layout_height="fill_parent"
	               android:text="About" />
          </LinearLayout>
          <!-- 底部第二个横条 -->
          <LinearLayout
             android:id="@+id/MyLayout_bottom2"
             android:orientation="horizontal" 
             android:layout_width="fill_parent"
             android:layout_height="20dp"
             android:background="#DFDFDF"
             android:layout_alignParentBottom="true" 
             android:gravity="center">
             <TextView
	       android:id="@+id/AboutTextView"
	       android:layout_width="wrap_content"
	       android:layout_height="wrap_content"
	       android:text="zhb931706659@126.com" />
          </LinearLayout>
    </RelativeLayout>
    <!-- 中部 -->
    <RelativeLayout 
        android:orientation="horizontal"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        android:layout_above="@id/MyLayout_bottom" 
        android:layout_below="@id/MyLayout_top"
        android:background="#EFDFDF"
        android:gravity="center">
        <ImageView
	           android:id="@+id/imageshow"
	           android:layout_width="fill_parent"
	           android:layout_height="wrap_content" />
    </RelativeLayout>
</RelativeLayout>


主Activity代码

  1. package com.example.njupt.zhb.imageprocesssystem; 
  2. import java.io.BufferedOutputStream; 
  3. import java.io.File; 
  4. import java.io.FileNotFoundException; 
  5. import java.io.FileOutputStream; 
  6. import java.io.IOException; 
  7. import android.net.Uri; 
  8. import android.os.Bundle; 
  9. import android.provider.MediaStore; 
  10. import android.app.Activity; 
  11. import android.content.BroadcastReceiver; 
  12. import android.content.Context; 
  13. import android.content.Intent; 
  14. import android.content.IntentFilter; 
  15. import android.database.Cursor; 
  16. import android.graphics.Bitmap; 
  17. import android.graphics.BitmapFactory; 
  18. import android.graphics.Bitmap.CompressFormat; 
  19. import android.graphics.Rect; 
  20. import android.view.Menu; 
  21. import android.view.MotionEvent; 
  22. import android.view.View; 
  23. import android.view.View.OnClickListener; 
  24. import android.view.View.OnTouchListener; 
  25. import android.view.ViewGroup.LayoutParams; 
  26. import android.widget.Button; 
  27. import android.widget.ImageView; 
  28. import android.widget.TextView; 
  29. import android.widget.Toast; 
  30. public class SystemMain extends Activity implements OnTouchListener { 
  31.     private Button selectImgBtn; 
  32.     private Button selectAlgBtn; 
  33.     private Button aboutBtn; 
  34.     private Button saveBtn; 
  35.     private Button shotScrBtn; 
  36.     private Button cameraBtn; 
  37.     private TextView filepathView; 
  38.     private TextView aboutView; 
  39.     private OnClickListener seleImgBtnListener=null
  40.     private OnClickListener seleAlgBtnListener=null
  41.     private OnClickListener aboutBtnListener=null
  42.     private OnClickListener saveBtnListener=null
  43.     private OnClickListener shotScrBtnListener=null
  44.     private OnClickListener cameraBtnListener=null
  45.     private static int RESULT_LOAD_IMAGE = 123
  46.     private static int SystemCapture=125
  47.     private String picturePath=null
  48.     private Bitmap myBitmap; 
  49.     private Bitmap displayBitmap; 
  50.     private Bitmap preBitmap; 
  51.     private ImageView myImageView; 
  52.     private ImageProcess myImageProcess=new ImageProcess(); 
  53.     private static final String DYNAMICACTION_Broadcast = "Broadcast.njupt.zhb.selectAlg"
  54.     float Touch_x1=0;float Touch_y1=0
  55.     float Touch_x2=0;float Touch_y2=0
  56.     @Override 
  57.     public void onCreate(Bundle savedInstanceState) { 
  58.         super.onCreate(savedInstanceState); 
  59.         setTitle("ImageProcessing Made by ZhengHaibo"); 
  60.         setContentView(R.layout.activity_system_main); 
  61.         seleImgBtnListener= new View.OnClickListener() { 
  62.             @Override 
  63.             public void onClick(View arg0) { 
  64.                 Intent i = new Intent( 
  65.                         Intent.ACTION_PICK, 
  66.                         android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
  67.                 startActivityForResult(i, RESULT_LOAD_IMAGE); 
  68.             } 
  69.         }; 
  70.         seleAlgBtnListener=new OnClickListener() { 
  71.             @Override 
  72.             public void onClick(View v) { 
  73.                 Intent intent=new Intent(SystemMain.this,SelectAlgActivity.class); 
  74.                 startActivity(intent); 
  75.             } 
  76.         }; 
  77.         saveBtnListener=new OnClickListener() { 
  78.              
  79.             @Override 
  80.             public void onClick(View v) { 
  81.                 // TODO Auto-generated method stub 
  82.                 SaveBitmapToJpegFile(); 
  83.             } 
  84.         }; 
  85.         aboutBtnListener=new OnClickListener() { 
  86.              
  87.             @Override 
  88.             public void onClick(View v) { 
  89.                 // TODO Auto-generated method stub 
  90.                 Intent intent=new Intent(SystemMain.this,ActivityAbout.class); 
  91.                 startActivity(intent); 
  92.             } 
  93.         }; 
  94.         shotScrBtnListener=new OnClickListener() { 
  95.              
  96.             @Override 
  97.             public void onClick(View v) { 
  98.                 // TODO Auto-generated method stub 
  99.                 preBitmap=myBitmap; 
  100.                 myBitmap=ShotScreentoBitmap(null); 
  101.                 ShowImage(myBitmap); 
  102.             } 
  103.         }; 
  104.         cameraBtnListener=new OnClickListener() { 
  105.              
  106.             @Override 
  107.             public void onClick(View v) { 
  108.                 // TODO Auto-generated method stub 
  109.                 Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);  
  110.                 startActivityForResult(intent, SystemCapture);  
  111.             } 
  112.         }; 
  113.         SetControl(); 
  114.         ShowImage(null); 
  115.     } 
  116.     private void SetControl(){ 
  117.         selectAlgBtn=(Button)findViewById(R.id.processBtn); 
  118.         selectImgBtn=(Button)findViewById(R.id.SelectBtn); 
  119.         saveBtn=(Button)findViewById(R.id.SaveBtn); 
  120.         aboutBtn=(Button)findViewById(R.id.AboutBtn); 
  121.         shotScrBtn=(Button)findViewById(R.id.ScreenShotBtn); 
  122.         cameraBtn=(Button)findViewById(R.id.CameraBtn); 
  123.         filepathView=(TextView)findViewById(R.id.ImagePath); 
  124.         aboutView=(TextView)findViewById(R.id.AboutTextView); 
  125.         myImageView=(ImageView)findViewById(R.id.imageshow); 
  126.         selectAlgBtn.setOnClickListener(seleAlgBtnListener); 
  127.         selectImgBtn.setOnClickListener(seleImgBtnListener); 
  128.         saveBtn.setOnClickListener(saveBtnListener); 
  129.         aboutBtn.setOnClickListener(aboutBtnListener); 
  130.         shotScrBtn.setOnClickListener(shotScrBtnListener); 
  131.         cameraBtn.setOnClickListener(cameraBtnListener); 
  132.         myImageView.setOnTouchListener(this); 
  133.         IntentFilter filter_dynamic = new IntentFilter(); 
  134.         filter_dynamic.addAction(DYNAMICACTION_Broadcast); 
  135.         registerReceiver(dynamicReceiver, filter_dynamic); 
  136.     } 
  137.     // 2 自定义动态广播接收器,内部类,接收选择的算法 
  138.     private BroadcastReceiver dynamicReceiver = new BroadcastReceiver() { 
  139.         @Override 
  140.         public void onReceive(Context context, Intent intent) { 
  141.             if(intent.getAction().equals(DYNAMICACTION_Broadcast)){ 
  142.                 String seleFlag = intent.getStringExtra("selectFlag"); 
  143.                 int ch=Integer.parseInt(seleFlag); 
  144.                 switch(ch){ 
  145.                 case 0
  146.                     ShowImage(myImageProcess.brighten(10, myBitmap)); 
  147.                     break
  148.                 case 1
  149.                     ShowImage(myImageProcess.averageFilter(3,3,myBitmap)); 
  150.                     break
  151.                 case 2
  152.                     ShowImage(myImageProcess.averageFilter(3,3,myBitmap)); 
  153.                     break
  154.                 default
  155.                 Toast.makeText(SystemMain.this, "Wrong!", Toast.LENGTH_SHORT).show(); 
  156.                         break
  157.                 } 
  158.                 Toast.makeText(SystemMain.this, "Processing finished!", Toast.LENGTH_SHORT).show(); 
  159.             } 
  160.         } 
  161.     }; 
  162.     private Bitmap FilesToBitmap(String filename){ 
  163.         Bitmap temp=null
  164.         if(filename!=null){ 
  165.             File imageFile = new File(filename); 
  166.             if (imageFile.exists()) 
  167.             { 
  168.                 // Load the image from file 
  169.                 temp = BitmapFactory.decodeFile(filename); 
  170.             } 
  171.              
  172.         } 
  173.         return temp; 
  174.     } 
  175.     public void ShowImage(Bitmap bitmap){ 
  176.         if (bitmap!=null) { 
  177.             myImageView.setImageBitmap(bitmap); 
  178.             displayBitmap=bitmap; 
  179.         } 
  180.         else
  181.            bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.lenna); 
  182.            myImageView.setImageBitmap(bitmap); 
  183.            myBitmap=bitmap; 
  184.            preBitmap=bitmap; 
  185.            displayBitmap=bitmap; 
  186.            picturePath=null
  187.         } 
  188.     } 
  189.     @Override 
  190.     protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
  191.         super.onActivityResult(requestCode, resultCode, data); 
  192.   
  193.         if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) { 
  194.             Uri selectedImage = data.getData(); 
  195.             String[] filePathColumn = { MediaStore.Images.Media.DATA }; 
  196.   
  197.             Cursor cursor = getContentResolver().query(selectedImage, 
  198.                     filePathColumn, null, null, null); 
  199.             cursor.moveToFirst(); 
  200.             int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
  201.             picturePath = cursor.getString(columnIndex); 
  202.             cursor.close(); 
  203.             filepathView.setText(picturePath); 
  204.             //imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath)); 
  205.             preBitmap=myBitmap; 
  206.             myBitmap=FilesToBitmap(picturePath); 
  207.             ShowImage(myBitmap); 
  208.         } 
  209.         if(requestCode==SystemCapture&&resultCode == RESULT_OK && null != data){ 
  210.             preBitmap=myBitmap; 
  211.             myBitmap = (Bitmap)data.getExtras().get("data");  
  212.             ShowImage(myBitmap); 
  213.         } 
  214.     } 
  215.     @Override 
  216.     public boolean onCreateOptionsMenu(Menu menu) { 
  217.         getMenuInflater().inflate(R.menu.activity_system_main, menu); 
  218.         return true
  219.     } 
  220.     @Override 
  221.     public boolean onTouch(View v, MotionEvent event) { 
  222.         // TODO Auto-generated method stub 
  223.         if(v.getId()==R.id.imageshow){ 
  224.             if(event.getAction()==MotionEvent.ACTION_DOWN){ 
  225.                 Touch_x1=event.getX(); 
  226.                 Touch_y1=event.getY(); 
  227.                 //String posString="pos=("+Touch_x1+","+Touch_y1+",)"; 
  228.                 //Toast.makeText(SystemMain.this,posString,Toast.LENGTH_SHORT).show(); 
  229.             } 
  230.             if(event.getAction()==MotionEvent.ACTION_UP){ 
  231.                 Touch_x2=event.getX(); 
  232.                 Touch_y2=event.getY(); 
  233.                 //String posString="pos=("+Touch_x2+","+Touch_y2+",)"; 
  234.                 //Toast.makeText(SystemMain.this,posString,Toast.LENGTH_SHORT).show(); 
  235.                 float temp_x=Math.abs(Touch_x1-Touch_x1); 
  236.                 float temp_y=Math.abs(Touch_y1-Touch_y2); 
  237.                 if (temp_x<=1&&temp_y<=1) {//视为没有移动 
  238.                     LayoutParams para=myImageView.getLayoutParams(); 
  239.                     if(para.height==LayoutParams.FILL_PARENT){ 
  240.                         para.height=LayoutParams.WRAP_CONTENT; 
  241.                     }else
  242.                         para.height=LayoutParams.FILL_PARENT; 
  243.                     } 
  244.                     myImageView.setLayoutParams(para); 
  245.                 } 
  246.                 if(temp_x>=temp_y){ 
  247.                     if (Touch_x1>=Touch_x2) {//向左滑动 
  248.                         //ShowImage(displayBitmap); 
  249.                     } 
  250.                     else{//向右滑动 
  251.                         aboutView.setText("上一幅图像"); 
  252.                         ShowImage(preBitmap); 
  253.                     } 
  254.                 } 
  255.                 else
  256.                     if (Touch_y1>=Touch_y2) {//向上滑动 
  257.                         aboutView.setText("Lenna图像"); 
  258.                         ShowImage(null); 
  259.                     } 
  260.                     else{//向下滑动 
  261.                         aboutView.setText("原图像"); 
  262.                         ShowImage(myBitmap); 
  263.                     } 
  264.                 } 
  265.             } 
  266.         } 
  267.         return true
  268.     } 
  269.     public void SaveBitmapToJpegFile(){ 
  270.     try
  271.         String outputPath=null
  272.         if(picturePath==null){ 
  273.             File outputDirectory = new File("/sdcard"); 
  274.             String outputFile = "lennaFromSystem.jpg"
  275.             outputPath = outputDirectory.toString() + "/" + outputFile; 
  276.         } 
  277.         else
  278.             outputPath=picturePath; 
  279.         } 
  280.         int quality = 75
  281.         FileOutputStream fileOutStr = new FileOutputStream(outputPath); 
  282.         BufferedOutputStream bufOutStr = new BufferedOutputStream(fileOutStr); 
  283.         displayBitmap.compress(CompressFormat.JPEG, quality, bufOutStr); 
  284.         bufOutStr.flush(); bufOutStr.close(); 
  285.         String tips="file have save as:"+outputPath; 
  286.         Toast.makeText(SystemMain.this,tips,Toast.LENGTH_SHORT).show(); 
  287.         aboutView.setText(tips); 
  288.         } 
  289.         catch (FileNotFoundException exception) 
  290.         {    
  291.             Toast.makeText(SystemMain.this,exception.toString(), Toast.LENGTH_SHORT).show(); 
  292.             //Log.e("debug_log", exception.toString()); 
  293.         } catch (IOException e) { 
  294.             // TODO Auto-generated catch block 
  295.             e.printStackTrace(); 
  296.         } 
  297.     } 
  298.     private Bitmap ShotScreentoBitmap(Activity activity) { 
  299.         Bitmap resultBitmap=null
  300.         //View是你需要截图的View 
  301.         if (activity==null) { 
  302.             activity=(Activity)this
  303.         } 
  304.         View view = activity.getWindow().getDecorView();  
  305.         view.setDrawingCacheEnabled(true);  
  306.         view.buildDrawingCache();  
  307.         resultBitmap = view.getDrawingCache();  
  308.             
  309.         //获取状态栏高度  
  310.         Rect frame = new Rect();    
  311.         activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);    
  312.         int statusBarHeight = frame.top;    
  313.         System.out.println(statusBarHeight);  
  314.             
  315.         //获取屏幕长和高  
  316.         int width = activity.getWindowManager().getDefaultDisplay().getWidth();    
  317.         int height = activity.getWindowManager().getDefaultDisplay().getHeight();    
  318.         Bitmap b = Bitmap.createBitmap(resultBitmap, 0, statusBarHeight, width, height - statusBarHeight);  
  319.         view.destroyDrawingCache();  
  320.         return b; 
  321.     } 
package com.example.njupt.zhb.imageprocesssystem;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
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.graphics.Bitmap.CompressFormat;
import android.graphics.Rect;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class SystemMain extends Activity implements OnTouchListener {
    private Button selectImgBtn;
    private Button selectAlgBtn;
    private Button aboutBtn;
    private Button saveBtn;
    private Button shotScrBtn;
    private Button cameraBtn;
    private TextView filepathView;
    private TextView aboutView;
    private OnClickListener seleImgBtnListener=null;
    private OnClickListener seleAlgBtnListener=null;
    private OnClickListener aboutBtnListener=null;
    private OnClickListener saveBtnListener=null;
    private OnClickListener shotScrBtnListener=null;
    private OnClickListener cameraBtnListener=null;
    private static int RESULT_LOAD_IMAGE = 123;
    private static int SystemCapture=125;
    private String picturePath=null;
    private Bitmap myBitmap;
    private Bitmap displayBitmap;
    private Bitmap preBitmap;
    private ImageView myImageView;
    private ImageProcess myImageProcess=new ImageProcess();
    private static final String DYNAMICACTION_Broadcast = "Broadcast.njupt.zhb.selectAlg";
	float Touch_x1=0;float Touch_y1=0;
	float Touch_x2=0;float Touch_y2=0;
    @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);
			}
		};
		saveBtnListener=new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				SaveBitmapToJpegFile();
			}
		};
		aboutBtnListener=new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent=new Intent(SystemMain.this,ActivityAbout.class);
				startActivity(intent);
			}
		};
		shotScrBtnListener=new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				preBitmap=myBitmap;
				myBitmap=ShotScreentoBitmap(null);
				ShowImage(myBitmap);
			}
		};
		cameraBtnListener=new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
				startActivityForResult(intent, SystemCapture); 
			}
		};
		SetControl();
		ShowImage(null);
    }
    private void SetControl(){
    	selectAlgBtn=(Button)findViewById(R.id.processBtn);
    	selectImgBtn=(Button)findViewById(R.id.SelectBtn);
    	saveBtn=(Button)findViewById(R.id.SaveBtn);
    	aboutBtn=(Button)findViewById(R.id.AboutBtn);
    	shotScrBtn=(Button)findViewById(R.id.ScreenShotBtn);
    	cameraBtn=(Button)findViewById(R.id.CameraBtn);
    	filepathView=(TextView)findViewById(R.id.ImagePath);
    	aboutView=(TextView)findViewById(R.id.AboutTextView);
    	myImageView=(ImageView)findViewById(R.id.imageshow);
    	selectAlgBtn.setOnClickListener(seleAlgBtnListener);
    	selectImgBtn.setOnClickListener(seleImgBtnListener);
    	saveBtn.setOnClickListener(saveBtnListener);
    	aboutBtn.setOnClickListener(aboutBtnListener);
    	shotScrBtn.setOnClickListener(shotScrBtnListener);
    	cameraBtn.setOnClickListener(cameraBtnListener);
    	myImageView.setOnTouchListener(this);
		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)){
  				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);
    		displayBitmap=bitmap;
		}
    	else {
		   bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.lenna);
		   myImageView.setImageBitmap(bitmap);
		   myBitmap=bitmap;
		   preBitmap=bitmap;
		   displayBitmap=bitmap;
		   picturePath=null;
		}
    }
    @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));
            preBitmap=myBitmap;
            myBitmap=FilesToBitmap(picturePath);
            ShowImage(myBitmap);
        }
        if(requestCode==SystemCapture&&resultCode == RESULT_OK && null != data){
        	preBitmap=myBitmap;
        	myBitmap = (Bitmap)data.getExtras().get("data"); 
        	ShowImage(myBitmap);
        }
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_system_main, menu);
        return true;
    }
	@Override
	public boolean onTouch(View v, MotionEvent event) {
		// TODO Auto-generated method stub
		if(v.getId()==R.id.imageshow){
			if(event.getAction()==MotionEvent.ACTION_DOWN){
				Touch_x1=event.getX();
				Touch_y1=event.getY();
				//String posString="pos=("+Touch_x1+","+Touch_y1+",)";
				//Toast.makeText(SystemMain.this,posString,Toast.LENGTH_SHORT).show();
			}
			if(event.getAction()==MotionEvent.ACTION_UP){
				Touch_x2=event.getX();
				Touch_y2=event.getY();
				//String posString="pos=("+Touch_x2+","+Touch_y2+",)";
				//Toast.makeText(SystemMain.this,posString,Toast.LENGTH_SHORT).show();
				float temp_x=Math.abs(Touch_x1-Touch_x1);
				float temp_y=Math.abs(Touch_y1-Touch_y2);
				if (temp_x<=1&&temp_y<=1) {//视为没有移动
					LayoutParams para=myImageView.getLayoutParams();
					if(para.height==LayoutParams.FILL_PARENT){
						para.height=LayoutParams.WRAP_CONTENT;
					}else{
						para.height=LayoutParams.FILL_PARENT;
					}
					myImageView.setLayoutParams(para);
				}
				if(temp_x>=temp_y){
					if (Touch_x1>=Touch_x2) {//向左滑动
						//ShowImage(displayBitmap);
					}
					else{//向右滑动
						aboutView.setText("上一幅图像");
						ShowImage(preBitmap);
					}
				}
				else{
					if (Touch_y1>=Touch_y2) {//向上滑动
						aboutView.setText("Lenna图像");
						ShowImage(null);
					}
					else{//向下滑动
						aboutView.setText("原图像");
						ShowImage(myBitmap);
					}
				}
			}
		}
		return true;
	}
	public void SaveBitmapToJpegFile(){
	try {
		String outputPath=null;
		if(picturePath==null){
			File outputDirectory = new File("/sdcard");
			String outputFile = "lennaFromSystem.jpg";
			outputPath = outputDirectory.toString() + "/" + outputFile;
		}
		else {
			outputPath=picturePath;
		}
		int quality = 75;
		FileOutputStream fileOutStr = new FileOutputStream(outputPath);
		BufferedOutputStream bufOutStr = new BufferedOutputStream(fileOutStr);
		displayBitmap.compress(CompressFormat.JPEG, quality, bufOutStr);
		bufOutStr.flush(); bufOutStr.close();
		String tips="file have save as:"+outputPath;
		Toast.makeText(SystemMain.this,tips,Toast.LENGTH_SHORT).show();
		aboutView.setText(tips);
		}
		catch (FileNotFoundException exception)
		{   
			Toast.makeText(SystemMain.this,exception.toString(), Toast.LENGTH_SHORT).show();
			//Log.e("debug_log", exception.toString());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	private Bitmap ShotScreentoBitmap(Activity activity) {
		Bitmap resultBitmap=null;
		//View是你需要截图的View
		if (activity==null) {
			activity=(Activity)this;
		}
        View view = activity.getWindow().getDecorView(); 
        view.setDrawingCacheEnabled(true); 
        view.buildDrawingCache(); 
        resultBitmap = view.getDrawingCache(); 
           
        //获取状态栏高度 
        Rect frame = new Rect();   
        activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);   
        int statusBarHeight = frame.top;   
        System.out.println(statusBarHeight); 
           
        //获取屏幕长和高 
        int width = activity.getWindowManager().getDefaultDisplay().getWidth();   
        int height = activity.getWindowManager().getDefaultDisplay().getHeight();   
        Bitmap b = Bitmap.createBitmap(resultBitmap, 0, statusBarHeight, width, height - statusBarHeight); 
        view.destroyDrawingCache(); 
		return b;
	}
}


源代码下载地址:(http://download.csdn.net/detail/nuptboyzhb/4494401)

转载请声明:http://write.blog.csdn.net/postedit/7857366

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值