使用内置的Camera捕获图像


内置照相机获取图像

意图:

Intent i = new Intent("android.media.action.IMAGE_CAPTURE");
		startActivityForResult(i, REQ_CAMERA);

从Camera获取返回的数据:

Bundle bundle = data.getExtras();
				Bitmap bitmap = (Bitmap) bundle.get("data");
				iv_camera_img.setImageBitmap(bitmap);


这种方式显示的图片并不是原图,是一个系统处理过的图要想获得原图:

意图:

imagePath = Environment.getExternalStorageDirectory().getAbsolutePath()+"/image.jpg";//获取图片路径
		File file = new File(imagePath);
		Uri imageuri = Uri.fromFile(file);
		
		Intent i = new Intent("android.media.action.IMAGE_CAPTURE");
		i.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageuri);
		startActivityForResult(i, REQ_CAMERA_BIG);

获取图片显示:

Display display = getWindowManager().getDefaultDisplay();
				int dw = display.getWidth();
				int dh = display.getHeight();
				//加载图像的尺寸而不是本身
				BitmapFactory.Options bfo =new BitmapFactory.Options(); 
				bfo.inJustDecodeBounds = true;
				Bitmap btm = BitmapFactory.decodeFile(imagePath, bfo);
				
				int ratiow = (int) Math.ceil(bfo.outWidth/(float)dw);
				int ratioh = (int) Math.ceil(bfo.outHeight/(float)dh);
				
				if(ratiow>1&&ratioh>1){
					if(ratioh>ratiow){
						bfo.inSampleSize = ratioh;   //产生一副原始图像的1/ratioh的图像
					}else{
						bfo.inSampleSize = ratiow; 
					}
				}
				bfo.inJustDecodeBounds = false;
				
				btm = BitmapFactory.decodeFile(imagePath, bfo);
				
				iv_camera_img.setImageBitmap(btm);

可以获取更大的图片显示。

记得要加读写权限

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值