您的位置:首页 > 移动开发 > Android开发

android图像处理系统框架

2012-08-12 16:41 435 查看
图像处理框架的功能简介:

作者:郑海波 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图像。向右滑动,显示上一幅图像。向下滑动,显示原图像。

部分代码展示:

布局文件:

<?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代码

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息