android Camera捕获数据

介绍:使用android自带的camera应用程序获取图片并显示在自定义的ImageView控件中。

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/takePhoto" />

    <ImageView
        android:id="@+id/imgView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:contentDescription="@string/imgView"/>

</LinearLayout>

MainActivity.java

package cn.yh.cameratest;

import java.io.File;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.BitmapFactory.Options;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.Display;
import android.view.Menu;
import android.widget.ImageView;

public class MainActivity extends Activity {

	private final static int CAMERA_RESULT = 0;
	private ImageView imgview;
	private String imageFilePath;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		//获取SDCard的位置,并在SDCard根下定义图片的名称。
		imageFilePath = Environment.getExternalStorageDirectory()
				.getAbsolutePath() + "/myCameraTest.jpg";
		File imageFile = new File(imageFilePath);
		Uri imageFileUri = Uri.fromFile(imageFile);
		//android.provider.MediaStore.ACTION_IMAGE_CAPTURE:camera启动意图;
		Intent intent = new Intent(
				android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
		//android.provider.MediaStore.EXTRA_OUTPUT:给意图附加参数,指定图片保存的路径;
		intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageFileUri);
		//使用startActivityForResult方法,在Camera活动结束之后,进入onActivityResult方法;
		startActivityForResult(intent, CAMERA_RESULT);
	}

	@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
	protected void onActivityResult(int requestCode, int resultCode,
			Intent intent) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, intent);
		if (resultCode == RESULT_OK) {
			//1.简单获取到捕捉到的数据,显示时只是显示一副处理过的缩略图。
			// Bundle bundle = intent.getExtras();
			// Bitmap bitmap = (Bitmap) bundle.get("data");
			//
			// imgview = (ImageView) findViewById(R.id.imgView);
			// imgview.setImageBitmap(bitmap);
			
			/*2.设置图片大小为原图的1/8
			BitmapFactory.Options bmFactoryOptions = new Options();
			bmFactoryOptions.inSampleSize = 8;
			Bitmap bitmap = BitmapFactory.decodeFile(imageFilePath,
					bmFactoryOptions);
			*/
			//3.根据屏幕的大小来显示图片
			Display display = getWindowManager().getDefaultDisplay();
			int dw = display.getWidth();
			int dh = display.getHeight();
			//加载图像的尺寸,而不是图像本身。
			BitmapFactory.Options bmFactoryOptions = new Options();
			bmFactoryOptions.inJustDecodeBounds = true;
			Bitmap bitmap = BitmapFactory.decodeFile(imageFilePath,
					bmFactoryOptions);
			int hRatio = (int) Math.ceil(bmFactoryOptions.outHeight/(float)dh);
			int wRatio = (int) Math.ceil(bmFactoryOptions.outWidth/(float)dw);
			//判断是按高比率缩放还是宽比例缩放
			if(hRatio > 1 && wRatio > 1){
				if(hRatio > wRatio){
					bmFactoryOptions.inSampleSize = hRatio;
				}else{
					bmFactoryOptions.inSampleSize = wRatio;
				}
			}
			//对图像进行真正的解码
			bmFactoryOptions.inJustDecodeBounds = false;
			bitmap = BitmapFactory.decodeFile(imageFilePath,
					bmFactoryOptions);
			imgview = (ImageView) findViewById(R.id.imgView);
			imgview.setBackgroundColor(Color.GRAY);
			imgview.setImageBitmap(bitmap);
		}
	}

}



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值