作者:郑海波 2012-08-12 Email:[email protected]
部分代码展示:
布局文件:
主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