码迷,mamicode.com
首页 > 移动开发 > 详细

Android Camera

时间:2015-07-19 16:32:08      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:api   android   camera   

Android调用系统api使用照相机功能,实现拍照获取图片以及从照相机库中获取指定图片的功能。

以下是示例代码:


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
	<ImageView android:layout_width="match_parent"
	    android:layout_height="100dip"
	    android:id="@+id/image"
	    />
 	<Button 
 	    android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="testPopupWindow"
        android:layout_below="@id/image"
        />

</RelativeLayout>


/**
 * 实现Popup弹出窗口并实现调用系统照相机功能
 * @author dream
 *
 */
public class TestPopupWindow extends Activity {
	private SelectPopupWindow popupWindow;
	private ImageView image;
	private Button btn;
	private Uri fileUri;     
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main1);
		init();
	}
	
	private void init(){
		image = (ImageView) findViewById(R.id.image);
		btn = (Button) findViewById(R.id.btn);
		btn.setOnClickListener(onClickListener);
		fileUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "temp.jpg")); //图片存放路径
	}
	
	View.OnClickListener onClickListener = new View.OnClickListener() {
		
		@Override
		public void onClick(View v) {
			switch(v.getId()){
			case R.id.btn:
				test();
				break;
			case R.id.takephoto:
				Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
				intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
				startActivityForResult(intent, 100);
				break;
			case R.id.selectfromalbum:
				Intent intent1 = new Intent(Intent.ACTION_PICK);
				intent1.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
				startActivityForResult(intent1, 200);
				break;
				
			}
		}
	};
	
	
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if(requestCode == 100 && resultCode == RESULT_OK){
			int width = image.getWidth();
			int height = image.getHeight();
			BitmapFactory.Options opts = new BitmapFactory.Options();
			opts.inJustDecodeBounds = true;
			BitmapFactory.decodeFile(fileUri.getPath(), opts);
			int w = opts.outWidth;
			int h = opts.outHeight;
			int factor;
			if(w>width && h>height){
				factor = Math.min(w/width, h/height);  //根据ImageView的大小按一定比例缩小图片
			}else {
				factor = 1;
			}
			opts.inSampleSize = factor;
			opts.inJustDecodeBounds = false;
			Bitmap bm = BitmapFactory.decodeFile(fileUri.getPath(), opts);
			image.setImageBitmap(bm);
		} else if(requestCode == 200 && resultCode == RESULT_OK){
			int width = image.getWidth();
			int height = image.getHeight();
			BitmapFactory.Options opts = new BitmapFactory.Options();
			opts.inJustDecodeBounds = true;
			Uri uri = data.getData();
			try {
				InputStream in = getContentResolver().openInputStream(uri);
				BitmapFactory.decodeStream(in, null, opts);
				int w = opts.outWidth;
				int h = opts.outHeight;
				int factor;
				if(w>width && h>height){
					factor = Math.min(w/width, h/height);  //根据ImageView的大小按一定比例缩小图片
				}else {
					factor = 1;
				}
				opts.inSampleSize = factor;
				opts.inJustDecodeBounds = false;
				in = getContentResolver().openInputStream(uri);   //需要再次获取,因为前面流已经改变了
				Bitmap bm = BitmapFactory.decodeStream(in, null, opts);
				image.setImageBitmap(bm);
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		
	}

	public void test(){
		popupWindow = new SelectPopupWindow(this, onClickListener);
		popupWindow.showAtLocation(findViewById(R.id.main), 
				Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0);
	}
	
}


运行结果:

技术分享

技术分享


技术分享


技术分享


技术分享



技术分享








版权声明:本文为博主原创文章,未经博主允许不得转载。

Android Camera

标签:api   android   camera   

原文地址:http://blog.csdn.net/u012432475/article/details/46955141

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!