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

android多媒体学习笔记一

2012-09-04 10:47 176 查看
1 使用内置的camera应用程序捕获图像

/**

* 拍摄一张照片保存到sd卡上

*/

private void takeAndSaveImage() {

// 取得路径

imageFilePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/myfavoritepicture.jpg";

Log.i("tag", "sd card path:" + imageFilePath);

File imageFile = new File(imageFilePath);

Uri imageFileUri = Uri.fromFile(imageFile);

Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

i.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageFileUri);

startActivityForResult(i, CAMERA_RESULT);

}

/**

* 显示一张相片

*/

private void displayImage() {

// 取得屏幕大小

Display display = getWindowManager().getDefaultDisplay();

int dw = display.getWidth();

int dh = display.getHeight();

Log.i("tag", "dw:" + dw + " dh:" + dh);

// 加载图像的尺寸而不是图像本身

bmpOptions = new BitmapFactory.Options();

// 如果为true,只须返回图像的范围,不须尝试解码图像本身

bmpOptions.inJustDecodeBounds = true;

// options.inSampleSize = 8;

Bitmap bitmap = BitmapFactory.decodeFile(imageFilePath, bmpOptions);

int hRatio = (int) Math.ceil(bmpOptions.outHeight / (float) dh);

int wRatio = (int) Math.ceil(bmpOptions.outWidth / (float) dw);

Log.i("tag", "hRatio" + hRatio);

Log.i("tag", "wRatio" + wRatio);

// 如果两个比率都大于1

// 那么图像的一条边将大小屏幕

if (hRatio > 1 && wRatio > 1) {

if (hRatio > wRatio) {

Log.i("tag", "hRatio" + hRatio);

// 若高度比率更大,则根据它缩放

bmpOptions.inSampleSize = hRatio;

} else {

Log.i("tag", "wRatio" + wRatio);

// 反之则根据宽度缩放

bmpOptions.inSampleSize = wRatio;

}

}

// 对图像进行解码

bmpOptions.inJustDecodeBounds = false;

bitmap = BitmapFactory.decodeFile(imageFilePath, bmpOptions);

// 显示图片

imageView.setImageBitmap(bitmap);

}

注:所需权限

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

2 图像存储和元数据

获取图像的uri

使用MediaStore存储图像

/**

* 取得图像的uri

*

* 如果是存储在SD卡上 EXTERNAL_CONTENT_URI 如果是存储在设备内存中 INTERNAL_CONTENT_URI

*/

private void getImageURI() {

// 在contentValues映射中保存图像的名称和描述

ContentValues contentValues = new ContentValues();

contentValues.put(Media.DISPLAY_NAME, "this is a test title");

contentValues.put(Media.DESCRIPTION, "this is a test description");

contentValues.put(Media.MIME_TYPE, "image/jpeg");

imageFileUri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, contentValues);

// 方法二:后期添加元数据

// ContentValues contentValues = new ContentValues();

// contentValues.put(Media.DISPLAY_NAME, "this is a test title");

// contentValues.put(Media.DESCRIPTION, "this is a test description");

// contentValues.put(Media.MIME_TYPE, "image/jpeg");

// getContentResolver().update(imageFileUri, contentValues, null, null);

Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageFileUri);

startActivity(intent);

}

使用MediaStore检索图像章

/**

* 检索保存的图像

*

* @throws FileNotFoundException

*/

private void findBitmap() throws FileNotFoundException {

BitmapFactory.decodeStream(getContentResolver().openInputStream(imageFileUri), null, bmpOptions);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: