我们知道在Android中ImageView是一个用于显示图像的控件,比如在很多联系人方面的应用中,需要为联系人拍摄头像,并将得到的头像放在一个ImageView对象中显示。通常,在没有为联系人拍摄头像之前,ImageView对象中显示的是一个缺省的图片。因此,在保存联系人头像数据时,需要比较拍摄前和拍摄后两个图像是否一致,因为拍摄后是可能取消拍摄结果的,因此有可能造成拍摄动作发生前后ImageView中显示的是同一个图像。只有当不一样时,才将ImageView对象中的图像保存到数据库中,否则不用保存,这样一方面可以提高性能,另一方面可以节省空间。
根据上面的思路,我们可以在开始拍摄前获取ImageView对象中的图像,在保存数据的时候,再次从该ImageView对象中获取图像,然后比较先后所得到的像个图像是否一致,并进行进一步的处理。
1、从ImageView对象中获取图像的方法是什么呢?
从ImageView对象中获取图像的方法,就是ImageView类中的getDrawingCache()方法,比如下面的代码就是从一个ImageView对象iv_photo中获取图像:
Bitmap obmp = Bitmap.createBitmap(iv_photo.getDrawingCache());
需要注意:
1. 在调用getDrawingCache()方法从ImageView对象获取图像之前,一定要调用setDrawingCacheEnabled(true)方法:
iv_photo.setDrawingCacheEnabled(true);
否则,无法从ImageView对象iv_photo中获取图像;
2. 在调用getDrawingCache()方法从ImageView对象获取图像之后,一定要调用setDrawingCacheEnabled(false)方法:
iv_photo.setDrawingCacheEnabled(false);
以清空画图缓冲区,否则,下一次从ImageView对象iv_photo中获取的图像,还是原来的图像。
2、代码如下:
1. activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/iv_photo"
android:layout_width="fill_parent"
android:layout_height="220dp"
android:clickable="true"
android:src="@drawable/ic_launcher" />
<Button
android:id="@+id/btn_photo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/iv_photo"
android:layout_centerHorizontal="true"
android:text="保存" />
</LinearLayout>
2.MainActivity.java
package com.example.head;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.Toast;
public class MainActivity extends Activity implements OnTouchListener {
private Bitmap obmp;
private ImageView iv_photo;
private Button btn_photo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv_photo = (ImageView) findViewById(R.id.iv_photo);
iv_photo.setOnTouchListener(this);
btn_photo = (Button) findViewById(R.id.btn_photo);
btn_photo.setOnTouchListener(this);
}
public boolean onTouch(View v, MotionEvent event) {
switch (v.getId()) {
case R.id.iv_photo:
if (event.getAction() == MotionEvent.ACTION_DOWN) {
iv_photo.setDrawingCacheEnabled(true);
obmp = Bitmap.createBitmap(iv_photo.getDrawingCache());
iv_photo.setDrawingCacheEnabled(false);
// ImageView对象(iv_photo)必须做以上设置后,才能获取其中的图像
FileOutputStream m_fileOutPutStream = null;
String filepath = Environment.getExternalStorageDirectory()
+ File.separator + "tempPhoto.png";//文件保存路径
try {
m_fileOutPutStream = new FileOutputStream(filepath);
obmp.compress(CompressFormat.PNG, 100, m_fileOutPutStream);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} finally {
try {
m_fileOutPutStream.flush();
m_fileOutPutStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//调用系统相机
startActivityForResult(intent, 1);
}
break;
case R.id.btn_photo:
if (event.getAction() == MotionEvent.ACTION_DOWN) {
// ImageView对象(iv_photo)必须做如下设置后,才能获取其中的图像
iv_photo.setDrawingCacheEnabled(true);
// 获取ImageView中的图像
Bitmap sbmp = Bitmap.createBitmap(iv_photo.getDrawingCache());
// 从ImaggeView对象(iv_photo)中获取图像后,要记得调用setDrawingCacheEnabled(false)
// 清空画图缓冲区
iv_photo.setDrawingCacheEnabled(false);
// 将得到sbmp写入文件
FileOutputStream m_fileOutPutStream = null;
String filepath = Environment.getExternalStorageDirectory()
+ File.separator + "tempPhoto1.png";
try {
m_fileOutPutStream = new FileOutputStream(filepath);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
sbmp.compress(CompressFormat.PNG, 100, m_fileOutPutStream);
try {
m_fileOutPutStream.flush();
m_fileOutPutStream.close();
} catch (IOException e) {
e.printStackTrace();
}
if (!compare2Image(obmp, sbmp)) {
Toast.makeText(this, "new picture", Toast.LENGTH_LONG)
.show();
} else {
Toast.makeText(this, "old picture", Toast.LENGTH_LONG)
.show();
}
}
break;
}
return false;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {
Bundle bundle = data.getExtras();
Bitmap bitmap = (Bitmap) bundle.get("data");// 获取相机返回的数据,并转换为Bitmap图片格式
iv_photo.setScaleType(ScaleType.FIT_XY);
iv_photo.setImageBitmap(bitmap);
}
super.onActivityResult(requestCode, resultCode, data);
}
// 简单的比较两个图像是否一致
private boolean compare2Image(Bitmap bmp1, Bitmap bmp2) {
int iteration = 0;
int width = bmp1.getWidth();
int height = bmp1.getHeight();
if (width != bmp2.getWidth())
return false;
if (height != bmp2.getHeight())
return false;
if (width < height) {
iteration = width;
} else {
iteration = height;
}
for (int i = 0; i < iteration; ++i) {
if (bmp1.getPixel(i, i) != bmp2.getPixel(i, i))
return false;
}
return true;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}