package com.mzeat.ui;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import cn.Ragnarok.BitmapFilter;
//import cn.jingling.lib.filters.FilterFactory;
import com.mzeat.R;
import com.mzeat.task.GenericTask;
import com.mzeat.task.TaskAdapter;
import com.mzeat.task.TaskParams;
import com.mzeat.task.TaskResult;
import com.mzeat.util.ImageUtils;
import com.mzeat.util.ShowToast;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.Toast;
public class ImageFilter extends BaseActivity implements OnClickListener {
//private Bitmap originBitmap;
private Bitmap resultBitmap;
private ImageView img_filter;
private String protraitPath;
private LoadDataTask mLoadDataTask;
public static final int CROP_OK = 1;
public static final int CROP_CANCEL = 2;
public static final int CROP = 3;
private boolean originChange = false;
private String rotate;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_imagefilter);
protraitPath = getIntent().getStringExtra("protraitPath");
//originBitmap = ImageUtils.loadImgThumbnail(protraitPath);
// 输出裁剪的临时文件
String timestamp = new SimpleDateFormat(
"yyyyMMddHHmmss").format(new Date());
// 照片命名
rotate = FILE_SAVEPATH + "mzeat_rotate" + timestamp + ".jpg";
//resultBitmap = originBitmap;
resultBitmap = ImageUtils.loadImgThumbnail(protraitPath);
initView();
}
private void initView() {
// TODO Auto-generated method stub
img_filter = (ImageView) findViewById(R.id.img_filter);
//img_filter.setImageBitmap(originBitmap);
img_filter.setImageBitmap(resultBitmap);
findViewById(R.id.cut).setOnClickListener(this);
findViewById(R.id.rotate).setOnClickListener(this);
//findViewById(R.id.artwork).setOnClickListener(this);
findViewById(R.id.GrayFilter).setOnClickListener(this);
findViewById(R.id.BlurFilter).setOnClickListener(this);
findViewById(R.id.InvertFilter).setOnClickListener(this);
findViewById(R.id.OldFilter).setOnClickListener(this);
findViewById(R.id.SharpenFilter).setOnClickListener(this);
findViewById(R.id.LightFilter).setOnClickListener(this);
findViewById(R.id.LomoFilter).setOnClickListener(this);
findViewById(R.id.SoftGlowFilter).setOnClickListener(this);
findViewById(R.id.SketchFilter).setOnClickListener(this);
findViewById(R.id.btn_cancel).setOnClickListener(this);
findViewById(R.id.btn_delete).setOnClickListener(this);
findViewById(R.id.btn_yes).setOnClickListener(this);
}
int type;
private final static String FILE_SAVEPATH = Environment
.getExternalStorageDirectory().getAbsolutePath() + "/MZEAT/Camera/";
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.cut:
// 输出裁剪的临时文件
String timeStamp = new SimpleDateFormat(
"yyyyMMddHHmmss").format(new Date());
// 照片命名
String fileName = FILE_SAVEPATH + "mzeat_crop" + timeStamp + ".jpg";
//File file = new File(fileName);
try {
Intent data = new Intent(ImageFilter.this, ImageCrop.class);
data.putExtra("cropPath", fileName);
ImageUtils.saveImageToSD( fileName, resultBitmap,75);
startActivityForResult(data, CROP);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
ShowToast.showMessage(ImageFilter.this, "照片保存错误。");
}
break;
case R.id.rotate:
//resultBitmap = newBitmap;
resultBitmap = ImageUtils.postRotateBitamp(resultBitmap, 90);
img_filter.setImageBitmap(resultBitmap);
File file = new File(rotate);
if (file.exists()) {
file.delete();
try {
ImageUtils.saveImageToSD(rotate, resultBitmap, 75);
protraitPath =rotate;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
originChange = true;
break;
/**
case R.id.artwork:
resultBitmap.recycle();
resultBitmap = ImageUtils.loadImgThumbnail(protraitPath);
img_filter.setImageBitmap(resultBitmap);
//img_filter.setImageBitmap(originBitmap);
//resultBitmap = originBitmap;
break;
**/
case R.id.GrayFilter:
type = BitmapFilter.GRAY_STYLE;
loaddata();
break;
case R.id.BlurFilter:
type = BitmapFilter.BLUR_STYLE;
loaddata();
break;
case R.id.InvertFilter:
type = BitmapFilter.INVERT_STYLE;
loaddata();
break;
case R.id.OldFilter:
type = BitmapFilter.OLD_STYLE;
loaddata();
break;
case R.id.SharpenFilter:
type = BitmapFilter.SHARPEN_STYLE;
loaddata();
break;
case R.id.LightFilter:
type = BitmapFilter.LIGHT_STYLE;
loaddata();
break;
case R.id.LomoFilter:
type = BitmapFilter.LOMO_STYLE;
loaddata();
break;
case R.id.SoftGlowFilter:
type = BitmapFilter.SOFT_GLOW_STYLE;
loaddata();
break;
case R.id.SketchFilter:
type = BitmapFilter.SKETCH_STYLE;
loaddata();
break;
case R.id.btn_cancel:
setResult(PubShareActivity.IMG_CANCEL);
//originBitmap.recycle();
resultBitmap.recycle();
finish();
break;
case R.id.btn_delete:
setResult(PubShareActivity.IMG_DELETE);
//originBitmap.recycle();
resultBitmap.recycle();
finish();
break;
case R.id.btn_yes:
if (originChange == false) {
setResult(PubShareActivity.IMG_CANCEL);
//originBitmap.recycle();
resultBitmap.recycle();
finish();
}else {
// 输出裁剪的临时文件
String timestamp = new SimpleDateFormat(
"yyyyMMddHHmmss").format(new Date());
// 照片命名
String filename = FILE_SAVEPATH + "mzeat_change" + timestamp + ".jpg";
//File file = new File(fileName);
try {
Intent data = new Intent();
data.putExtra("fileName", filename);
ImageUtils.saveImageToSD( filename, resultBitmap,75);
setResult(PubShareActivity.IMG_CHANGE, data);
//originBitmap.recycle();
resultBitmap.recycle();
finish();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
ShowToast.showMessage(ImageFilter.this, "照片保存错误。");
}
}
break;
default:
break;
}
}
ProgressDialog pg;
private TaskAdapter mTaskListener = new TaskAdapter() {
@Override
public String getName() {
// TODO Auto-generated method stub
return null;
}
public void onPreExecute(GenericTask task) {
// TODO 任务开始执行,可提供进度条展现
pg = ProgressDialog.show(ImageFilter.this,
getString(R.string.dialog_tips),
getString(R.string.loading), true, true, cancelListener);
pg.setCanceledOnTouchOutside(false);
}
public void onPostExecute(GenericTask task, TaskResult result) {
pg.dismiss();
pg = null;
// TODO 判断TaskReult的返回值是否ok
if (result == TaskResult.OK) {
//resultBitmap = resultBitmap.createBitmap(newBitmap);
img_filter.setImageBitmap(resultBitmap);
//newBitmap.recycle();
originChange = true;
//mLoadDataTask = null;
/**
// 输出裁剪的临时文件
String timestamp = new SimpleDateFormat(
"yyyyMMddHHmmss").format(new Date());
// 照片命名
String filename = FILE_SAVEPATH + "mzeat_change" + timestamp + ".jpg";
//File file = new File(fileName);
try {
ImageUtils.saveImageToSD( filename, resultBitmap,75);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
ShowToast.showMessage(ImageFilter.this, "照片保存错误。");
}
**/
}else {
img_filter.setImageBitmap(resultBitmap);
mLoadDataTask = null;
}
}
public void onProgressUpdate(GenericTask task, Object param) {
// TODO 如果是下载,可在此显示下载进度
}
public void onCancelled(GenericTask task) {
// TODO 后台任务被取消的事件回调,适当情况下可以提示用户,如“下载已取消”
}
};
DialogInterface.OnCancelListener cancelListener = new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface arg0) {
// TODO Auto-generated method stub
clearTask();
}
};
private void clearTask() {
// TODO Auto-generated method stub
if (null != mLoadDataTask
&& mLoadDataTask.getStatus() == GenericTask.Status.RUNNING) {
mLoadDataTask.cancel(true);
mLoadDataTask = null;
}
}
//Bitmap newBitmap;
private class LoadDataTask extends GenericTask {
@Override
protected TaskResult _doInBackground(TaskParams... params) {
// TODO Auto-generated method stub
//String imagepath ;
//if (filename != null ) {
// imagepath = filename;
//}else {
// imagepath = protraitPath;
//}
resultBitmap.recycle();
Bitmap bitmap = ImageUtils.loadImgThumbnail(protraitPath);
resultBitmap = BitmapFilter.changeStyle(bitmap,
type);
bitmap.recycle();
//newBitmap = FilterFactory.createOneKeyFilter("bluetone").apply(ImageFilter.this, originBitmap);
if (resultBitmap != null) {
return TaskResult.OK;
}else {
return TaskResult.FAILED;
}
}
@Override
protected void onCancelled() {
// TODO Auto-generated method stub
super.onCancelled();
}
}
private void loaddata() {
/**
* 重要!!需要判断当前任务是否正在运行,否则重复执行会出错,典型的场景就是用户点击登录按钮多次
*/
if (null != mLoadDataTask
&& mLoadDataTask.getStatus() == GenericTask.Status.RUNNING)
return;
mLoadDataTask = new LoadDataTask();
mLoadDataTask.setListener(mTaskListener);
try {
mLoadDataTask.execute();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
//String filename = null;
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
//super.onActivityResult(requestCode, resultCode, data);
switch (resultCode) {
case CROP_OK:
protraitPath = data.getStringExtra("fileName");
resultBitmap.recycle();
resultBitmap = ImageUtils.getBitmapByPath(protraitPath);
originChange = true;
img_filter.setImageBitmap(resultBitmap);
break;
case CROP_CANCEL:
break;
default:
break;
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_BACK) {
setResult(PubShareActivity.IMG_CANCEL);
finish();
}
return super.onKeyDown(keyCode, event);
}
}