调用摄像头

来源:互联网 发布:电视机软件小鹰市场 编辑:程序博客网 时间:2024/05/05 12:13
package org.quentin.choosepictest;import java.io.File;import java.io.FileNotFoundException;import java.io.IOException;import android.app.Activity;import android.content.Intent;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.net.Uri;import android.os.Bundle;import android.os.Environment;import android.provider.MediaStore;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.ImageView;/* * 经过测试,发现这些代码在公司的测试机上跑起来是没有问题的。但是在自己的5.1手机测试结果是不正常的。 * 这可真是奇怪啊。学习一本书的时候,还是有一个比较稳定的版本比较好。当入门以后在通过自己查找文档,获得新特性 * 在真正的项目开发中才可能事半功倍 */public class MainActivity extends Activity {    public static final int TAKE_PHOTO = 1;    public static final int CROP_PHOTO = 2;        private Button takePhoto;    private ImageView picture;    private Uri imageUri; // -         @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);                takePhoto = (Button)findViewById(R.id.take_photo);        picture = (ImageView)findViewById(R.id.picture);//        takePhoto.setText("Hello Thoto");        takePhoto.setOnClickListener(new OnClickListener() {                        @Override            public void onClick(View v) {                 //TODO Auto-generated method stub                File outputImage = new File(Environment.getExternalStorageDirectory(),"tempImage.jpg");                try{                    if(outputImage.exists()){                        outputImage.delete();                                            }                    outputImage.createNewFile();                }catch(IOException e){                    e.printStackTrace();                }                                imageUri = Uri.fromFile(outputImage);                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);                intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);                startActivityForResult(intent, TAKE_PHOTO);            }        });    }    @Override    protected void onActivityResult(int requestCode, int resultCode, Intent data) {        // TODO Auto-generated method stub        super.onActivityResult(requestCode, resultCode, data);        switch(requestCode){        case TAKE_PHOTO:            if(resultCode == RESULT_OK){                Intent intent = new Intent("com.android.camera.action.CROP");                intent.setDataAndType(imageUri, "image/*");                intent.putExtra("scale", 1);                intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);                startActivityForResult(intent, CROP_PHOTO);            }            break;        case CROP_PHOTO:            if(resultCode == RESULT_OK){                try{                    Bitmap bitmap = BitmapFactory.decodeStream(                            getContentResolver().openInputStream(imageUri)                            );                    picture.setImageBitmap(bitmap);                }catch(FileNotFoundException e){                    e.printStackTrace();                }            }            break;        default:            break;        }            }    @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;    }    @Override    public boolean onOptionsItemSelected(MenuItem item) {        // Handle action bar item clicks here. The action bar will        // automatically handle clicks on the Home/Up button, so long        // as you specify a parent activity in AndroidManifest.xml.        int id = item.getItemId();        if (id == R.id.action_settings) {            return true;        }        return super.onOptionsItemSelected(item);    }}
0 0
原创粉丝点击