Android 使用内置的Camera应用程序捕获图像
本Demo的实现效果是调用手机上已安装的照相机来实现拍照的功能,拍好的照片以ImageView形式展示。
目的:学习手机调用安装的相机照相,对大的图片处理有所认识,这里主要用到BitmapFactory和BitmapFactory.Options两个类。
载入并显示一副图像对内存使用情况有显著的影响,Android提供了一个名为BitmapFactory 的有用程序类,该程序提供了一系列的静态方法,同意通过各种来源载入Bitmap图像。
针对我们的需求,将从文件载入图像。并在最初的活动中显示它。幸运的是,BitmapFactory中的可用方法将会调用BitmapFactory.Options类。这使得我们可以定义怎样将Bitmap读入内存。详细而言,当载入图像时。可以设置BitmapFactory应该使用的採样大小。在BitmapFactory.Options中指定inSampleSize參数。
比如。将inSampleSize
= 8时。产生一幅图的大小是原始大小的1/8。要注意的是首先应将BitmapFactoryOptions.inJustDecodeBounds变量设置为true,这将通知BitmapFactory类仅仅需返回该图像的范围。而无需尝试解码图像本身。
最后将BitmapFactory.Options.inJustDecodeBounds设置为false。最后对其进行真正的解码。
实现效果图:
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMjQ0MDIwNw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" height="528" width="297">
源码:
activity_main布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> </LinearLayout>
MainActivity源码:
package com.multimediademo1; import java.io.File;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.Display;
import android.widget.ImageView; public class MainActivity extends Activity {
private final static int CAMERA_RESULT = 0;
private ImageView imageView;
private String imageFilePath; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); imageFilePath = Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/myfavoritepicture.jpg";
File imageFile = new File(imageFilePath);
Uri imageFileUri = Uri.fromFile(imageFile); Intent intent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageFileUri); startActivityForResult(intent, CAMERA_RESULT);
} @Override
protected void onActivityResult(int requestCode, int resultCode,
Intent intent) {
super.onActivityResult(requestCode, resultCode, intent); if (resultCode == RESULT_OK) { imageView = (ImageView) findViewById(R.id.imageView); Display currentDisplay = getWindowManager().getDefaultDisplay();
int dw = currentDisplay.getWidth();
int dh = currentDisplay.getHeight();
// 载入图像的尺寸,而不是图像本身
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
Bitmap bitmap = BitmapFactory.decodeFile(imageFilePath, options);
int heightRatio = (int) Math.ceil(options.outHeight / (float) dh);
int widthRatio = (int) Math.ceil(options.outWidth / (float) dw);
// 假设两个比率都大于1。那么图像的一条边将大于屏幕
if (heightRatio > 1 && widthRatio > 1) {
if (heightRatio > widthRatio) {
// 若高度比率更大,则依据它缩放
options.inSampleSize = heightRatio;
} else {
options.inSampleSize = widthRatio;
}
}
options.inJustDecodeBounds = false;
bitmap = BitmapFactory.decodeFile(imageFilePath, options); imageView.setImageBitmap(bitmap);
} } }
源码下载:
Android 使用内置的Camera应用程序捕获图像的更多相关文章
- 1.1使用内置的Camara应用程序捕捉图像
一: Camara应用程序包含的意图过滤器 <intent-filter> <action android:name="android.media.action.IMAGE ...
- Android获取内置sdcard跟外置sdcard路径
Android获取内置sdcard跟外置sdcard路径.(测试过两个手机,亲测可用) 1.先得到外置sdcard路径,这个接口是系统提供的标准接口. 2.得到上一级文件夹目录 3.得到该目录的所有文 ...
- 微信内置浏览器和小程序的 User Agent 区别及判断方法
通过UA来判断不同的设备或者浏览器是开发者最常用的方式方法,而对于微信开发和小程序也是同样的一个情况,我们可以通过微信内置浏览器 User Agent 信息来判断其具体类型或者设备. 所以子凡就通过徒 ...
- android 怎样内置/预置/预编译文件(运行程序,应用程序,apk, jar, lib 等随意文件)到系统中
方法一: 如果要内置的软件名称为iperf.exe 1. 将iperf.exe放到Codebase的随意一个文件夹下(该文件夹必须可以在搜索Android.mk时被搜索到),比方system/ipe ...
- android获取内置和外置SD卡路径 - z
本文将介绍Android真机环境下如何获取内置和外置SD卡路径. 测试环境:三星Note3,其他手机待测试... 所需权限(AndroidManifest.xml文件里) <uses-permi ...
- 【python基础语法】OS模块处理文件绝对路径,内置的异常类型、捕获、处理(第9天课堂笔记)
import os """ 通过文件的路径去打开文件 相对路径:相对当前的工作路径去定位文件位置 .:代表当前路径 ..:代表上一级路径(父级路径) 绝对路径:相对于电脑 ...
- Android Studio 内置SDK在 unity中使用
1 AndroidStudio 安装好后更新SDK Platforms 2 在 File -> Other Settings -> Default Project Structure 中可 ...
- 使用蓝灯后,IE浏览器以及内置IE浏览器的程序不能使用的解决方案
使用完蓝灯后,每次使用IE浏览器都不能正常使用,于是有了下面的这个方案 1.通过Win+R 打开注册表编辑器(regedit) 进入目录 HKEY_CURRENT_USER \ Software \ ...
- 写一个android内置android程序
当我们编译完毕android源代码之后,就须要对他做点什么事情,我如今正在看老罗的"Android源代码情景分析"一书.在这里主要是记录一些书中没有说清楚的地方. 相同.我们创建一 ...
随机推荐
- 聊聊、RabbitMQ 第一篇
(一)windows 下安装配置 开源的消息中间件有很多,各有各的优缺点,适合自己项目的才是最好的.首先下载 rabbitMQ 安装版本,因为 rabbitMQ 底层语言是 erlang,所以首先要先 ...
- Linux中date命令的各种实用方法
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://521cto.blog.51cto.com/950229/935642 在linu ...
- astyle使用基础教程
astyle使用基础教程 转自: http://babybandf.blog.163.com/blog/static/61993532010112205811797/ astyle是一个我自己常用的开 ...
- 【bzoj1408】[Noi2002]Robot 数论+dp
题目描述 输入 输出 样例输入 3 2 1 3 2 5 1 样例输出 8 6 75 题解 语文题+数论+dp 花了大段讲述什么叫mu,什么叫phi,只是新定义的mu将2看作有平方因子,新定义的phi( ...
- 浅谈后缀自动机SAM
一下是蒟蒻的个人想法,并不很严谨,仅供参考,如有缺误,敬请提出 参考资料: 陈立杰原版课件 litble 某大神 某大神 其实课件讲得最详实了 有限状态自动机 我们要学后缀自动机,我们先来了解一下自动 ...
- Posix线程编程指南
Posix线程编程指南 Posix线程编程指南... 1 一线程创建与取消... 2 线程创建... 2 1.线程与进程... 2 2. 创建线程... 2 3. 线程创建属性... 2 4. 创建的 ...
- id_rsa id_rsa.pub
id_rsa 私钥 id_rsa.pub 公钥 https://blog.csdn.net/qq_36663951/article/details/78749217 https://blog.cs ...
- UIAlertController 实现kvo实现mes文字设置
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"更新提示" message:[NS ...
- 代码怎样重构<1>
原文发布时间为:2011-05-24 -- 来源于本人的百度文章 [由搬家工具导入]
- using要写多少
原文发布时间为:2009-10-25 -- 来源于本人的百度文章 [由搬家工具导入] 有时候未必要像默认的这样要using这么多。。。 using System;using System.Data;u ...