Android 之照相機的使用
最近在做圖形處理的項目,要使用到照相機。主要實現調用圖庫功能打開圖片或者調用照相機照相將圖片返回并顯示出來。下面是主要代碼:
package com.cloay.camera;import java.io.FileNotFoundException;
import android.app.Activity; import android.app.AlertDialog; import android.content.ContentResolver; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.os.Bundle; import android.view.MotionEvent; import android.view.View; import android.view.View.OnClickListener; import android.view.View.OnTouchListener; import android.widget.ImageButton; import android.widget.ImageView; /**
- 打開照相機或者圖庫添加圖片
- CameraTestActivity.java
- @author Cloay
2011-11-30 */ public class CameraTestActivity extends Activity { private ImageButton camera; private ImageView image; @Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.main); image = (ImageView) findViewById(R.id.image); camera = (ImageButton) findViewById(R.id.camera); camera.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if(event.getAction() == MotionEvent.ACTION_DOWN){ v.setBackgroundResource(R.drawable.camera_bg_pressed); } if(event.getAction() == MotionEvent.ACTION_UP){ showMenuDialog(); v.setBackgroundResource(R.drawable.camera_bg_normal); } return false; } });
}
private void showMenuDialog() {
View menuView = View.inflate(CameraTestActivity.this, R.layout.menu, null); final AlertDialog menuDialog = new AlertDialog.Builder(CameraTestActivity.this) .setView(menuView) .setTitle("選擇操作")
// .setIcon(R.drawable.camera)
.create(); menuDialog.show(); menuView.findViewById(R.id.camera).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { menuDialog.dismiss(); Intent intentCamera = new Intent("android.media.action.IMAGE_CAPTURE");//使用照相機 startActivityForResult(intentCamera, Activity.DEFAULT_KEYS_DIALER); } }); menuView.findViewById(R.id.picture).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { menuDialog.dismiss();
// Intent intentPhoto = new Intent(Intent.ACTION_GET_CONTENT, null);
// intentPhoto.setType("image/*"); //這個參數是確定要選擇的內容為圖片 // intentPhoto.putExtra("crop", "circle"); //這個參數 不太懂,唯一知道的是:設置了參數,就會調用裁剪,如果不設置,就會跳過裁剪的過程。 // intentPhoto.putExtra("aspectX", 33); //這個是裁剪時候的 裁剪框的 X 方向的比例。 // intentPhoto.putExtra("aspectY",43); //同上Y 方向的比例. (注意: aspectX, aspectY ,兩個值都需要為 整數,如果有一個為浮點數,就會導致比例失效。)//設置aspectX 與 aspectY 后,裁剪框會按照所指定的比例出現,放大縮小都不會更改。如果不指定,那么 裁剪框就可以隨意調整了。
// intentPhoto.putExtra("outputX", 50); //返回數據的時候的 X 像素大小。 // intentPhoto.putExtra("outputY", 100); //返回的時候 Y 的像素大小。
//以上兩個值,設置之后會按照兩個值生成一個Bitmap, 兩個值就是這個bitmap的橫向和縱向的像素值,如果裁剪的圖像和這個像素值不符合,那么空白部分以黑色填充。
// intentPhoto.putExtra("noFaceDetection", true); // 是否去除面部檢測, 如果你需要特定的比例去裁剪圖片,那么這個一定要去掉,因為它會破壞掉特定的比例。 // // intentPhoto.putExtra("return-data", true); //是否要返回值。 一般都要。 // startActivityForResult(intentPhoto, 1);
Intent intent = new Intent();
/* 開啟Pictures畫面Type設定為image */
intent.setType("image/*");
/* 使用Intent.ACTION_GET_CONTENT這個Action */
intent.setAction(Intent.ACTION_GET_CONTENT);
/* 取得相片后返回本畫面 */
startActivityForResult(intent, 2);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == 2){
if (resultCode == RESULT_OK) {
Uri uri = data.getData();
ContentResolver cr = this.getContentResolver();
try {
Bitmap bitmap = BitmapFactory.decodeStream(cr.openInputStream(uri));
/* 將Bitmap設定到ImageView */
image.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
image.setImageResource(R.drawable.empty);
}
}
}
if(requestCode == Activity.DEFAULT_KEYS_DIALER){
if(data != null){
Bundle extras = data.getExtras();
Bitmap bitmap = (Bitmap) extras.get("data");
image.setImageBitmap(bitmap);
}
else{
image.setImageResource(R.drawable.empty);
}
}
super.onActivityResult(requestCode, resultCode, data);
}
}</pre>轉自:http://blog.csdn.net/shang_515/article/details/7313387