您的位置:首页 > 移动开发 > Android开发

Android多媒体高级编程(一)——Camera和简单的图像处理

2015-07-16 09:55 495 查看
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);
		}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: