Android使用相机照相以及对照片的裁剪
1.Activity源代码:
package com.example.crema;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class CramaActivity extends Activity {
private Button creama=null;
private ImageView img=null;
private TextView text=null;
private File tempFile = new File(Environment.getExternalStorageDirectory(),
getPhotoFileName());
private static final int PHOTO_REQUEST_TAKEPHOTO = 1;// 拍照
private static final int PHOTO_REQUEST_GALLERY = 2;// 从相册中选择
private static final int PHOTO_REQUEST_CUT = 3;// 结果
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_crama);
init();
Log.i("TAG-->", ""+Environment.getExternalStorageDirectory());
}
private void init() {
// TODO Auto-generated method stub
creama=(Button) findViewById(R.id.btn_creama);
img=(ImageView) findViewById(R.id.img_creama);
creama.setOnClickListener(listener);
text=(TextView) findViewById(R.id.text);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case PHOTO_REQUEST_TAKEPHOTO:// 当选择拍照时调用
startPhotoZoom(Uri.fromFile(tempFile));
break;
case PHOTO_REQUEST_GALLERY:// 当选择从本地获取图片时
// 做非空判断,当我们觉得不满意想重新剪裁的时候便不会报异常,下同
if (data != null)
startPhotoZoom(data.getData());
break;
case PHOTO_REQUEST_CUT:// 返回的结果
if (data != null)
// setPicToView(data);
sentPicToNext(data);
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
private OnClickListener listener=new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent cameraintent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// 指定调用相机拍照后照片的储存路径
cameraintent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(tempFile));
startActivityForResult(cameraintent, PHOTO_REQUEST_TAKEPHOTO);
}};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_crama, menu);
return true;
}
private void startPhotoZoom(Uri uri) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
// crop为true是设置在开启的intent中设置显示的view可以剪裁
intent.putExtra("crop", "true");
// aspectX aspectY 是宽高的比例
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
// outputX,outputY 是剪裁图片的宽高
intent.putExtra("outputX", 300);
intent.putExtra("outputY", 300);
intent.putExtra("return-data", true);
intent.putExtra("noFaceDetection", true);
startActivityForResult(intent, PHOTO_REQUEST_CUT);
}
// 将进行剪裁后的图片传递到下一个界面上
private void sentPicToNext(Intent picdata) {
Bundle bundle = picdata.getExtras();
if (bundle != null) {
Bitmap photo = bundle.getParcelable("data");
if (photo==null) {
img.setImageResource(R.drawable.get_user_photo);
}else {
补充:移动开发 , Android ,