</pre><pre>
<pre name="code" class="java">package com.example.test;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity {
private final int SYSTEM_CAMERA_REQUESTCODE = 1;
private Uri imageFileUri = null;
private File file;
ImageView imageview;
int imageWidth;
int imageHeigth;
Button btn_takepicture;
Button btn_view_picture;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
imageview = (ImageView) findViewById(R.id.imageview);
imageWidth = imageview.getWidth();
imageHeigth = imageview.getHeight();
btn_takepicture = (Button) findViewById(R.id.btn_takepicture);
btn_view_picture = (Button) findViewById(R.id.btn_view_picture);
/**
* 分别设置拍照和预览图片这两个监听事件
*/
btn_takepicture.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
boolean result = isSupportSDCard();
if (result == true) {
Intent intent = new Intent();
intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
file = new File(Environment.getExternalStorageDirectory(),"test.png");
<span style="white-space:pre"> <span style="white-space:pre"> </span>try {
<span style="white-space:pre"> </span> if (file.exists()) {
<span style="white-space:pre"> </span> <span style="white-space:pre"> </span> file.delete();
<span style="white-space:pre"> </span> }
<span style="white-space:pre"> </span> file.createNewFile();
<span style="white-space:pre"> </span> } catch (Exception e) {
<span style="white-space:pre"> </span> }</span>
<span style="white-space:pre"> </span>imageFileUri = Uri.fromFile(file);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageFileUri);
startActivityForResult(intent, SYSTEM_CAMERA_REQUESTCODE);
/**
* 如果设置了putExtra,则保存到指定的路径,并且在onActivityResult中返回的bitmap是没有失真的。
*如果不设置putExtra,则保存到默认的路径,并且对onActivityResult中返回的bitmap,进行了16*16的压缩。
*
* Intent intent = new Intent();
* intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
* startActivityForResult(intent, SYSTEM_CAMERA_REQUESTCODE);
*/
} else {
Toast.makeText(getApplicationContext(), "打开相机失败,请确认已经插入SD卡",
Toast.LENGTH_LONG).show();
}
}
});
btn_view_picture.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "image/*");
startActivity(intent);
}
});
}
/**
* 处理接收到的图片
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == SYSTEM_CAMERA_REQUESTCODE && resultCode == RESULT_OK) {
// 对拍照之后的图片进行压缩。
Bitmap bitmap = compressBySize(file.getPath(), imageWidth,
imageHeigth);
try {
// 对拍照之后的图片保存,并且进行compress压缩
saveFile(bitmap, file.getPath());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 取出图片,放置在imageview上面
Bitmap map = BitmapFactory.decodeFile(file.getPath());
Log.e("sydlog", "map.getwidth=" + map.getWidth());
Log.e("sydlog", "map.getHeight=" + map.getHeight());
imageview.setImageURI(Uri.fromFile(file));
}
/*上面没有intent.putExtra(MediaStore.EXTRA_OUTPUT, imageFileUri);的时候*/
/*将返回Bitmap的缩小图(16*16)的缩小放入到data中,可以通过这样的方式取得*/
//Bitmap bitmap = (Bitmap) data.getExtras().get("data");
//((ImageView)findViewById(R.id.imageview)).setImageBitmap(bitmap);
}
/**
* 对图片进行裁剪
*
* @param pathName
* @param targetWidth
* @param targetHeight
* @return
*/
public Bitmap compressBySize(String pathName, int targetWidth,
int targetHeight) {
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inJustDecodeBounds = true;// 不去真的解析图片,只是获取图片的头部信息,包含宽高等;
Bitmap bitmap = BitmapFactory.decodeFile(pathName, opts);
// 得到图片的宽度、高度;
float imgWidth = opts.outWidth;
float imgHeight = opts.outHeight;
Log.e("sydlog", "压缩前图片的宽度=" + imgWidth);
Log.e("sydlog", "压缩前图片的高度=" + imgHeight);
// 分别计算图片宽度、高度与目标宽度、高度的比例;取大于等于该比例的最小整数;
int widthRatio = (int) Math.ceil(imgWidth / (float) targetWidth);
int heightRatio = (int) Math.ceil(imgHeight / (float) targetHeight);
opts.inSampleSize = 1;
if (widthRatio > 1 || widthRatio > 1) {
if (widthRatio > heightRatio) {
opts.inSampleSize = widthRatio;
} else {
opts.inSampleSize = heightRatio;
}
}
// 设置好缩放比例后,加载图片进内容;
opts.inJustDecodeBounds = false;
bitmap = BitmapFactory.decodeFile(pathName, opts);
Log.e("sydlog", "压缩后图片的宽度=" + bitmap.getWidth());
Log.e("sydlog", "压缩后图片的高度=" + bitmap.getHeight());
return bitmap;
}
/**
* 对图片进行压缩,这种压缩为等质压缩,对png图片,基本没有效果,图片大小基本不变。
* @param bm
* @param fileName
* @throws Exception
*/
public void saveFile(Bitmap bm, String fileName) throws Exception {
File myCaptureFile = new File(fileName);
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(myCaptureFile));
// 100表示不进行压缩,70表示压缩率为30%
bm.compress(Bitmap.CompressFormat.PNG, 100, bos);
bos.flush();
bos.close();
}
/**
* 检测手机是否支持将拍照的图片放入到SD卡
* @return
*/
protected boolean isSupportSDCard() {
String state = Environment.getExternalStorageState();
if (state.equals(Environment.MEDIA_MOUNTED)) {
return true;
} else {
return false;
}
}
//计算图片大小
public int getBitmapSize(Bitmap bitmap){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){ //API 19
return bitmap.getAllocationByteCount();
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1){//API 12
return bitmap.getByteCount();
}
return bitmap.getRowBytes()*bitmap.getHeight(); //earlier version
}
}