当前位置: 首页 > news >正文

android多媒体学习笔记一

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);
}

   

转载于:https://www.cnblogs.com/retacn-yue/archive/2012/09/04/2761369.html

相关文章:

  • 编解码学习笔记(十三):容器(上篇)
  • Linux进程调度原理
  • 用jasperreports在applet中实现打印功能开发总结
  • Serializable 和 Parcelable 两种序列化
  • javax.servlet.ServletException: BeanUtils.populate的解决
  • 文件系统性能测试
  • BIEE在windows Server 2003 64bit上安装
  • android 蓝牙学习笔记
  • [QT] TCP协议演示
  • content_for对应的yield
  • 四十八、Qt网络(八)TCP(二)
  • GetDlgItem用法
  • 四十七、Qt网络(七)TCP(一)
  • 四十六、Qt网络(六)UDP
  • 评估一款电子邮件营销软件的方法总结
  • “大数据应用场景”之隔壁老王(连载四)
  • 【css3】浏览器内核及其兼容性
  • 【许晓笛】 EOS 智能合约案例解析(3)
  • ECMAScript6(0):ES6简明参考手册
  • ES学习笔记(10)--ES6中的函数和数组补漏
  • Invalidate和postInvalidate的区别
  • miniui datagrid 的客户端分页解决方案 - CS结合
  • Mysql数据库的条件查询语句
  • Rancher-k8s加速安装文档
  • React+TypeScript入门
  • 阿里云前端周刊 - 第 26 期
  • 对超线程几个不同角度的解释
  • 分享几个不错的工具
  • 服务器从安装到部署全过程(二)
  • 复杂数据处理
  • 前端_面试
  • 前端面试之CSS3新特性
  • 浅谈Golang中select的用法
  • 怎样选择前端框架
  • PostgreSQL 快速给指定表每个字段创建索引 - 1
  • 第二十章:异步和文件I/O.(二十三)
  • 分布式关系型数据库服务 DRDS 支持显示的 Prepare 及逻辑库锁功能等多项能力 ...
  • #《AI中文版》V3 第 1 章 概述
  • #DBA杂记1
  • (C语言)求出1,2,5三个数不同个数组合为100的组合个数
  • (Spark3.2.0)Spark SQL 初探: 使用大数据分析2000万KF数据
  • (二)hibernate配置管理
  • (二开)Flink 修改源码拓展 SQL 语法
  • (附源码)ssm考生评分系统 毕业设计 071114
  • (三)Hyperledger Fabric 1.1安装部署-chaincode测试
  • (转)es进行聚合操作时提示Fielddata is disabled on text fields by default
  • (自用)learnOpenGL学习总结-高级OpenGL-抗锯齿
  • .NET Core 和 .NET Framework 中的 MEF2
  • .net 程序 换成 java,NET程序员如何转行为J2EE之java基础上(9)
  • .net 后台导出excel ,word
  • .Net组件程序设计之线程、并发管理(一)
  • .pub是什么文件_Rust 模块和文件 - 「译」
  • .py文件应该怎样打开?
  • .vimrc php,修改home目录下的.vimrc文件,vim配置php高亮显示
  • [ vulhub漏洞复现篇 ] Hadoop-yarn-RPC 未授权访问漏洞复现