调用Camera进行照相并将照片返回到App界面

讲到了

打开手机相机

照相对照片进行 大小处理 显示到自己的App上面

手机横竖屏时 保存View的当前状态


App按钮点击后触发打开Camera的事件

<ol><li><span style="font-family: Arial, Helvetica, sans-serif;"><span style="font-size:18px;">Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);</span></span></li></ol>
然后 ,给intent添加数据,存储在ContentResolver中,方便显示在App中

<ol><li><span style="font-family: Arial, Helvetica, sans-serif;"><span style="font-size:18px;">takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));</span></span></li></ol>
然后定义
<span style="font-size:18px;">startActivityForResult</span>
方法接收返回的数据。
在下面方法中,处理返回来的数据

<span style="font-size:18px;">protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (requestCode) {
		case ACTION_TAKE_PHOTO_B: {
			if (resultCode == RESULT_OK) {
				handleBigCameraPhoto();
			}
			break;
		} // ACTION_TAKE_PHOTO_B

		case ACTION_TAKE_PHOTO_S: {
			if (resultCode == RESULT_OK) {
				handleSmallCameraPhoto(data);
			}
			break;
		} // ACTION_TAKE_PHOTO_S

		case ACTION_TAKE_VIDEO: {
			if (resultCode == RESULT_OK) {
				handleCameraVideo(data);
			}
			break;
		} // ACTION_TAKE_VIDEO
		} // switch
	}</span>

下面方法是对图片进行处理,并显示到界面上。

<span style="font-size:18px;">private void setPic() {

	

		int targetW = mImageView.getWidth();
		int targetH = mImageView.getHeight();


		BitmapFactory.Options bmOptions = new BitmapFactory.Options();
		bmOptions.inJustDecodeBounds = true;
		BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
		int photoW = bmOptions.outWidth;
		int photoH = bmOptions.outHeight;
		
		
		int scaleFactor = 1;
		if ((targetW > 0) || (targetH > 0)) {
			scaleFactor = Math.min(photoW/targetW, photoH/targetH);	
		}

		bmOptions.inJustDecodeBounds = false;
		bmOptions.inSampleSize = scaleFactor;
		bmOptions.inPurgeable = true;

		
		Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
	
		mImageView.setImageBitmap(bitmap);
		
		mImageView.setVisibility(View.VISIBLE);
		
	}</span>




下面这个方法当手机横竖屏时,保存手机当前状态的。

<span style="font-size:18px;">protected void onSaveInstanceState(Bundle outState) {
		outState.putParcelable(BITMAP_STORAGE_KEY, mImageBitmap);
		outState.putBoolean(IMAGEVIEW_VISIBILITY_STORAGE_KEY, (mImageBitmap != null) );
		super.onSaveInstanceState(outState);
	}</span>

和下面的方法是同步使用的

<span style="font-size:18px;">protected void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);
		mImageBitmap = savedInstanceState.getParcelable(BITMAP_STORAGE_KEY);

		mImageView.setImageBitmap(mImageBitmap);
		mImageView.setVisibility(
				savedInstanceState.getBoolean(IMAGEVIEW_VISIBILITY_STORAGE_KEY) ? 
						ImageView.VISIBLE : ImageView.INVISIBLE
		);
		);
	}</span>

下面方法加载完后,用于发送一个扫描指定图片的广播

p<span style="font-size:18px;">rivate void galleryAddPic() {
		    Intent mediaScanIntent = new Intent("android.intent.action.MEDIA_SCANNER_SCAN_FILE");
			File f = new File(mCurrentPhotoPath);
		    Uri contentUri = Uri.fromFile(f);
		    mediaScanIntent.setData(contentUri);
		    this.sendBroadcast(mediaScanIntent);
	}</span>

<span style="font-size:18px;">"android.intent.action.MEDIA_SCANNER_SCAN_FILE")</span><span style="font-size:24px;">该意图</span>
由于扫描工作是在MediaScanner服务中进行的,因此不会阻塞当前程序进程。当扫描大量媒体文件且实时性要求不高的情况下,适合使用该扫描方式。

你可能感兴趣的:(android,图片处理,Camera)