当前位置: 代码迷 >> Android >> Android 之相机的使用
  详细解决方案

Android 之相机的使用

热度:73   发布时间:2016-05-01 10:43:20.0
Android 之照相机的使用
package?com.cloay.camera;
002?
003import?java.io.FileNotFoundException;
004?
005import?android.app.Activity;
006import?android.app.AlertDialog;
007import?android.content.ContentResolver;
008import?android.content.Intent;
009import?android.graphics.Bitmap;
010import?android.graphics.BitmapFactory;
011import?android.net.Uri;
012import?android.os.Bundle;
013import?android.view.MotionEvent;
014import?android.view.View;
015import?android.view.View.OnClickListener;
016import?android.view.View.OnTouchListener;
017import?android.widget.ImageButton;
018import?android.widget.ImageView;
019/**
020?* 打开照相机或者图库添加图片
021?* CameraTestActivity.java
022?* @author Cloay
023?* 2011-11-30
024?*/
025public?class?CameraTestActivity?extends?Activity {
026????private?ImageButton camera;
027????private?ImageView image;
028????@Override
029????public?void?onCreate(Bundle savedInstanceState) {
030????????super.onCreate(savedInstanceState);
031????????setContentView(R.layout.main);
032?????????
033????????image = (ImageView) findViewById(R.id.image);
034????????camera = (ImageButton) findViewById(R.id.camera);
035????????camera.setOnTouchListener(new?OnTouchListener() {
036?????????????
037????????????@Override
038????????????public?boolean?onTouch(View v, MotionEvent event) {
039????????????????if(event.getAction() == MotionEvent.ACTION_DOWN){
040????????????????????v.setBackgroundResource(R.drawable.camera_bg_pressed);
041????????????????}
042????????????????if(event.getAction() == MotionEvent.ACTION_UP){
043????????????????????showMenuDialog();
044????????????????????v.setBackgroundResource(R.drawable.camera_bg_normal);
045????????????????}
046????????????????return?false;
047????????????}
048????????});
049????}
050?????
051????private?void?showMenuDialog() {
052????????View menuView = View.inflate(CameraTestActivity.this, R.layout.menu,?null);
053????????final?AlertDialog menuDialog =?new?AlertDialog.Builder(CameraTestActivity.this)
054????????.setView(menuView)
055????????.setTitle("选择操作")
056//????? .setIcon(R.drawable.camera)
057????????.create();
058????????menuDialog.show();
059????????menuView.findViewById(R.id.camera).setOnClickListener(new?OnClickListener() {
060?????????????
061????????????@Override
062????????????public?void?onClick(View v) {
063????????????????menuDialog.dismiss();
064????????????????Intent intentCamera =?newIntent("android.media.action.IMAGE_CAPTURE");//使用照相机
065????????????????startActivityForResult(intentCamera, Activity.DEFAULT_KEYS_DIALER);
066????????????}
067????????});
068????????menuView.findViewById(R.id.picture).setOnClickListener(new?OnClickListener() {
069?????????????
070????????????@Override
071????????????public?void?onClick(View v) {
072????????????????menuDialog.dismiss();
073//????????????? Intent intentPhoto = new Intent(Intent.ACTION_GET_CONTENT, null);?
074//????????????? intentPhoto.setType("image/*");??? //这个参数是确定要选择的内容为图片
075//????????????? intentPhoto.putExtra("crop", "circle");?? //这个参数 不太懂,唯一知道的是:设置了参数,就会调用裁剪,如果不设置,就会跳过裁剪的过程。
076//????????????????? intentPhoto.putExtra("aspectX", 33);? //这个是裁剪时候的 裁剪框的 X 方向的比例。
077//????????????????? intentPhoto.putExtra("aspectY",43);? //同上Y 方向的比例. (注意: aspectX, aspectY ,两个值都需要为 整数,如果有一个为浮点数,就会导致比例失效。)
078????????????????//设置aspectX 与 aspectY 后,裁剪框会按照所指定的比例出现,放大缩小都不会更改。如果不指定,那么 裁剪框就可以随意调整了。
079//????????????????? intentPhoto.putExtra("outputX", 50);? //返回数据的时候的 X 像素大小。
080//????????????????? intentPhoto.putExtra("outputY", 100);? //返回的时候 Y 的像素大小。
081????????????????//以上两个值,设置之后会按照两个值生成一个Bitmap, 两个值就是这个bitmap的横向和纵向的像素值,如果裁剪的图像和这个像素值不符合,那么空白部分以黑色填充。
082?
083//????????????? intentPhoto.putExtra("noFaceDetection", true); // 是否去除面部检测, 如果你需要特定的比例去裁剪图片,那么这个一定要去掉,因为它会破坏掉特定的比例。
084//
085//????????????? intentPhoto.putExtra("return-data", true);? //是否要返回值。 一般都要。
086//????????????? startActivityForResult(intentPhoto, 1);
087?????????????????
088?????????????????Intent intent =?new?Intent();?
089????????????????????/* 开启Pictures画面Type设定为image */?
090????????????????????intent.setType("image/*");?
091????????????????????/* 使用Intent.ACTION_GET_CONTENT这个Action */?
092????????????????????intent.setAction(Intent.ACTION_GET_CONTENT);??
093????????????????????/* 取得相片后返回本画面 */?
094????????????????????startActivityForResult(intent,?2);
095????????????}
096????????});
097????}
098?
099????@Override
100????protected?void?onActivityResult(int?requestCode,?int?resultCode, Intent data) {
101????????if(requestCode ==?2){
102????????????if?(resultCode == RESULT_OK) {?
103????????????????Uri uri = data.getData();?
104????????????????ContentResolver cr =?this.getContentResolver();?
105????????????????try?{?
106????????????????????Bitmap bitmap = BitmapFactory.decodeStream(cr.openInputStream(uri));?
107????????????????????/* 将Bitmap设定到ImageView */?
108????????????????????image.setImageBitmap(bitmap);?
109????????????????}?catch?(FileNotFoundException e) {?
110????????????????????image.setImageResource(R.drawable.empty);
111????????????????}?
112????????????}?
113????????}
114????????if(requestCode == Activity.DEFAULT_KEYS_DIALER){
115????????????if(data !=?null){
116????????????????Bundle extras = data.getExtras();
117????????????????Bitmap bitmap = (Bitmap) extras.get("data");
118????????????????image.setImageBitmap(bitmap);
119????????????}
120????????????else{
121????????????????image.setImageResource(R.drawable.empty);
122????????????}
123????????}
124????????super.onActivityResult(requestCode, resultCode, data);
125????}
126?????
127?????
128}
  相关解决方案