Android多媒體高級編程(一)——Camera和簡單的圖像處理

1.外置存儲卡和內置存儲卡

使用Environment.getExternalStorageDirectory().getAbsolutePath()來獲取安卓存儲卡位置,根據在設置裏的默認存儲位置,來獲取不同的路徑,如果設置默認存儲在內置存儲卡,在路徑爲/storage/emulated/0(android 4.2.2),如果是外部存儲,則路徑爲/storage/sdcard1

2.使用照相機

a.使用Intent intent = new Intent(android.privider.MediaStroe.ACTION_IMAGE_CAPTURE)意圖來啓動照相機,結果返回在"data"數據裏。可以使用intent.getExtras().get(“data”)來獲取Bitmap對象(需要強制轉換)。默認返回的圖片是被壓縮的,因爲拍照的圖片內存過大,直接加載大圖,可能會導致OOM。

b.將照片儲存。需要在intent中添加附加信息。intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,uri)。傳入值是一個uri對象,可以使用如下。

String imagePath = Environment.getExternalStorageDirectory().getAbsoutePath()+"myimage.jpg";File image = new File(imagePath);Uri uri = Uri.fromFile(image);

此方法從文件中解析一個uri。

c.使用BitmapFactory.Options選項,加載圖片

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class ImageDisplayActivity extends Activity {

	private ImageView iv;
	private LinearLayout ll;
	private int screenW, screenH;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT,
				LayoutParams.FILL_PARENT);
		LayoutParams params2 = new LayoutParams(LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT);
		ll = new LinearLayout(this);
		iv = new ImageView(this);
		addContentView(iv, params);
		String imgPath = getIntent().getStringExtra("path");
		// 新建圖片工廠類
		BitmapFactory.Options options = new BitmapFactory.Options();
		// 只需返回圖像範圍,無需解碼圖像本身
		options.inJustDecodeBounds = true;
		screenW = getWindowManager().getDefaultDisplay().getWidth();
		screenH = getWindowManager().getDefaultDisplay().getHeight();
		Bitmap bitmap = BitmapFactory.decodeFile(imgPath, options);
		// int imageW = bitmap.getWidth();
		// int imageH = bitmap.getHeight();
		//解碼後,options中存儲圖片的尺寸,而不再bitmap中。
		int imageW = options.outWidth;
		int imageH = options.outHeight;
		setRatio(imageW, imageH, options);
		options.inJustDecodeBounds = false;
		Bitmap bmp = BitmapFactory.decodeFile(imgPath, options);
		iv.setImageBitmap(bmp);
	}

	private void setRatio(int imageW, int imageH, BitmapFactory.Options options) {
		int ratioW = (int) Math.ceil(imageW / screenW);
		int ratioH = (int) Math.ceil(imageH / screenH);
		if (ratioW >= 1 && ratioH >= 1) {
			options.inSampleSize = (ratioW >= ratioH) ? ratioW : ratioH;
		} else if (ratioW >= 1 || ratioH >= 1) {
			options.inSampleSize = (ratioW >= 1) ? ratioW : ratioH;
		} else {
			options.inSampleSize = 1;
		}
	}

}
d.拍照獲取大圖,可以先將拍照的圖片保存至存儲卡,然後進行解碼顯示。

e.元數據是關於數據的數據:他可以包括文件本身的數據信息,如它的大小和名稱,MediaStore允許設置各種各樣的其他信息,如標題,經度和緯度等。爲了將圖像插入標準位置,需要獲取MediaStore對象。由於是插入圖片,因此使用的方法時insert,改調用返回一個Uri,可以利用該Uri來寫入圖像文件的二進制數據。

ContentValues values = new ContentValues();
			values.put(Media.DISPLAY_NAME, "this is dispaly_name");
			values.put(Media.DESCRIPTION, "this is descpription");
			values.put(Media.MIME_TYPE, "image/jpeg");
			values.put(Media.TITLE, "this is title");
                        //傳入的contentvalues是與圖像相關聯的元數據的信息,鍵值必須是指定的名稱,如上所示
                        Uri imgUri = getContentResolver().insert(
					Media.EXTERNAL_CONTENT_URI, values);
			Intent intent3 = new Intent(
					android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
			intent3.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imgUri);
f.使用MediaStore檢索圖像。MediaStore和其他所有的內容提供者都以類似數據庫的方式運作,從中獲取數據,將得到一個Cursor遊標。爲了執行實際的查詢,需要使用活動中的managedQuery方法來查詢。第一個參數是Uri,隨後是所要查詢的列名稱的數組,後跟一條限定where的字句,和where字句中的參數,最後是orderby字句。

String[] columns = { Media.DATA, Media.DISPLAY_NAME, Media.TITLE };
		cursor = managedQuery(Media.EXTERNAL_CONTENT_URI, columns, null, null,
				null);
		dataIndex = cursor.getColumnIndexOrThrow(Media.DATA);
		nameIndex = cursor.getColumnIndexOrThrow(Media.DISPLAY_NAME);
		titleIndex = cursor.getColumnIndexOrThrow(Media.TITLE);
		if (cursor.moveToFirst()) {
			String path = cursor.getString(dataIndex);
			Bitmap bitmap = getBitmap(path);
			title.setText(cursor.getString(titleIndex));
			img.setImageBitmap(bitmap);
		}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章