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源代码情景分析"一书.在这里主要是记录一些书中没有说清楚的地方. 相同.我们创建一 ...
随机推荐
- STW 团队项目分析
序言 经过我们团队的详细讨论,最终确定我们的项目立意,它包含这我们每个人的观点,我相信我们可以做的很好,Believe......................................... ...
- 堆STL和重载运算符
大根堆: 1.priority_queue<int> q;[默认 2. priority_queue< node,vector<node>,less<node> ...
- Codeforces Round #418 (Div. 2) A+B+C!
终判才知道自己失了智.本场据说是chinese专场,可是请允许我吐槽一下题意! A. An abandoned sentiment from past shabi贪心手残for循环边界写错了竟然还过了 ...
- 第一个Maven案例Hello Maven
Maven目录结构 src:程序源代码 -main -java:java代码 -package:自定义的包 ...
- 经典SQL语句大全、50个常用的sql语句
50个常用的sql语句 Student(S#,Sname,Sage,Ssex) 学生表 Course(C#,Cname,T#) 课程表 SC(S#,C#,score) 成绩表 Teacher(T#,T ...
- 旅行商(sale)
旅行商(sale) 题目描述 camp国有n座城市,由1,2,-,n编号.城市由n–1条双向道路相连.任意两个城市之间存在唯一的道路连通.有m个旅行商,第i个旅行商会从城市ai旅行到城市bi,贩卖ci ...
- 2017-2018-2 20179204《网络攻防实践》第十一周学习总结 SQL注入攻击与实践
第1节 研究缓冲区溢出的原理,至少针对两种数据库进行差异化研究 1.1 原理 在计算机内部,输入数据通常被存放在一个临时空间内,这个临时存放的空间就被称为缓冲区,缓冲区的长度事先已经被程序或者操作系统 ...
- pat 甲级 1064. Complete Binary Search Tree (30)
1064. Complete Binary Search Tree (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...
- Springboot - 在启动完成后执行特定方法
1.实现方式 实现ApplicationRunner接口 实现CommandLineRunner接口 @Component @Slf4j public class AfterServiceStarte ...
- 【HDOJ5538】House Building(计算几何)
题意:给定一个n*m的方阵,第i行第j列的高度为a[i][j],问除了下底面之外其余五面的总表面积 n<=50,0<=a[i][j]<=1000 思路:队友写的,抱大腿 考虑当前方格 ...