Android开发学习之路--Camera之初体验
顾名思义Camera就是拍照和录像的功能,像微信里面,我们想拍照传一下照片,就可以通过camera来拍照,然后存储照片,发送给好友。那么微信的app里面是不会直接通过camera api来实现的,因为系统一般都会有camera这个程序,那么直接调用camera app来实现拍照的功能不是很方便嘛,这里我们学习下。其实最终camera调用到android底层的是v4l2的接口,关于v4l2,还有android的camera的框架以后有机会再好好研究研究。
调用系统自带的camera需要用到intent,通过MediaStore获取照片路径,下面来试一下,新建工程CameraPictureTest,为layout添加代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_margin="10dp"> <ImageView
android:id="@+id/picture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/> <Button
android:id="@+id/take_photo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="拍照"/> </LinearLayout>
编写代码如下:
package com.example.jared.camerapicturetest; 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.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView; import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException; public class MainActivity extends AppCompatActivity { public static final int TAKE_PHOTO = 1;
public static final int CROP_PICTURE = 2; private Button takePhoto;
private ImageView picture;
private Uri imageUri; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); takePhoto = (Button)findViewById(R.id.take_photo);
takePhoto.setOnClickListener(new myOnClickListener()); picture = (ImageView)findViewById(R.id.picture);
picture.setOnClickListener(new myOnClickListener());
} private class myOnClickListener implements View.OnClickListener {
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.take_photo:
setTakePhoto();
break;
default:
break;
}
}
} public void setTakePhoto() {
File outputImage = new File(Environment.getExternalStorageDirectory(), "test.jpg");
try {
if(outputImage.exists()) {
outputImage.delete();
}
outputImage.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
imageUri = Uri.fromFile(outputImage);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, TAKE_PHOTO);
} @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case TAKE_PHOTO:
if(resultCode == RESULT_OK) {
Intent intent1 = new Intent("com.android.camera.action.CROP");
intent1.setDataAndType(imageUri, "image/*");
intent1.putExtra("scale", true);
intent1.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent1, CROP_PICTURE);
}
break;
case CROP_PICTURE:
if(resultCode == RESULT_OK) {
try {
Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver()
.openInputStream(imageUri));
picture.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
break;
default:
break;
}
}
}
这里首先确定了保存的路径为根目录下的test.jpg,然后通过intent,传入这个路径的Uri,打开相机进行拍照,这里有对拍照的返回,如果返回成功,那么就调用CROP的功能对照片进行裁剪,进入到裁减后返回成功就把图片显示在layout创建的ImageView中。
具体需要真机显示,这里再插播一段关于真机屏幕在mac电脑上的显示,具体可以参考这篇文章,将你的安卓手机屏幕共享到PC或Mac上。通过一个chrome的Vysor插件来实现,需要android的5.0以上的版本才可以。
好了,下面看下显示的效果:
效果基本上出来了,很不错的插件。微信里面很多不是直接拍照发送的,还有通过选择相册的图片,已经拍好的照片来发送图片的,那么接着我们来实现这个功能,首先layout添加了choosephoto:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_margin="10dp"> <ImageView
android:id="@+id/picture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/> <Button
android:id="@+id/take_photo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="拍照"/> <Button
android:id="@+id/choose_photo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="选取照片"/>
</LinearLayout>
接着修改MainActivity代码如下:
package com.example.jared.camerapicturetest; 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.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView; import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException; public class MainActivity extends AppCompatActivity { public static final int TAKE_PHOTO = 1;
public static final int CROP_PICTURE = 2; private Button takePhoto;
private Button choosePhoto;
private ImageView picture;
private Uri imageUri; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); takePhoto = (Button)findViewById(R.id.take_photo);
takePhoto.setOnClickListener(new myOnClickListener()); choosePhoto = (Button)findViewById(R.id.choose_photo);
choosePhoto.setOnClickListener(new myOnClickListener()); picture = (ImageView)findViewById(R.id.picture);
picture.setOnClickListener(new myOnClickListener());
} private class myOnClickListener implements View.OnClickListener {
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.take_photo:
setTakePhoto();
break;
case R.id.choose_photo:
setChoosePhoto();
default:
break;
}
}
} public void setChoosePhoto() {
File outputImage1 = new File(Environment.getExternalStorageDirectory(), "test1.jpg");
try {
if(outputImage1.exists()) {
outputImage1.delete();
}
outputImage1.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
imageUri = Uri.fromFile(outputImage1);
Intent intent1 = new Intent("android.intent.action.GET_CONTENT");
intent1.setType("image/*");
intent1.putExtra("crop", true);
intent1.putExtra("scale", true);
intent1.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent1, CROP_PICTURE); } public void setTakePhoto() {
File outputImage = new File(Environment.getExternalStorageDirectory(), "test.jpg");
try {
if(outputImage.exists()) {
outputImage.delete();
}
outputImage.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
imageUri = Uri.fromFile(outputImage);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, TAKE_PHOTO);
} @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case TAKE_PHOTO:
if(resultCode == RESULT_OK) {
Intent intent1 = new Intent("com.android.camera.action.CROP");
intent1.setDataAndType(imageUri, "image/*");
intent1.putExtra("scale", true);
intent1.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent1, CROP_PICTURE);
}
break;
case CROP_PICTURE:
if(resultCode == RESULT_OK) {
try {
Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver()
.openInputStream(imageUri));
picture.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
break;
default:
break;
}
}
}
基本上和拍照也差不多,然后我们运行下看看效果:
点击选择照片按钮,我们会进入到相册的app里面,然后选择一张照片,然后裁剪后保存,如上图所示。
附:参考《第一行代码》
Android开发学习之路--Camera之初体验的更多相关文章
- Android开发学习之路--Activity之初体验
环境也搭建好了,android系统也基本了解了,那么接下来就可以开始学习android开发了,相信这么学下去肯定可以把android开发学习好的,再加上时而再温故下linux下的知识,看看androi ...
- Android开发学习之路--React-Native之初体验
近段时间业余在学node.js,租了个阿里云准备搭建后端,想用node.js,偶尔得知react-native可以在不同平台跑,js在iOS和android上都可以运行ok,今天就简单学习下rea ...
- Android开发学习之路--Service之初体验
android最后一个组件便是service了,终于学习到最后一个组件了,从年前的开发环境的搭建,到现在学到最后一个组件花了三周的时间,期间记录的点点滴滴,照着书本学习编写的代码都受益匪浅,这里要感谢 ...
- Android开发学习之路--RxAndroid之初体验
学了一段时间android,看了部分的项目代码,然后想想老是学基础也够枯燥乏味的,那么就来学习学习新东西吧,相信很多学java的都听说过RxJava,那么android下也有RxAndroid. Rx ...
- Android开发学习之路--传感器之初体验
说到传感器,还是有很多的,有加速度啊,光照啊,磁传感器等等.当然android手机之所以称为智能手机,少不了这几款传感器的功劳了.下面就学习下了,这里主要学习光照,加速度和磁. 新建工程emSenso ...
- Android开发学习之路--UI之初体验
之前都是学习Activity,对于布局都没有做过学习,这里就简单学习下吧.下面看下Android Studio下有哪些控件: 这里分为Widgets,Text Fields,Containers,Da ...
- Android开发学习之路--Notification之初体验
一般当我们收到短信啊,微信啊,或者有些app的提醒,我们都会在通知栏收到一天简单的消息,然后点击消息进入到app里面,其实android中有专门的Notification的类可以完成这个工作,这里就实 ...
- Android开发学习之路--Android Studio cmake编译ffmpeg
最新的android studio2.2引入了cmake可以很好地实现ndk的编写.这里使用最新的方式,对于以前的android下的ndk编译什么的可以参考之前的文章:Android开发学习之路– ...
- Android开发学习之路--网络编程之xml、json
一般网络数据通过http来get,post,那么其中的数据不可能杂乱无章,比如我要post一段数据,肯定是要有一定的格式,协议的.常用的就是xml和json了.在此先要搭建个简单的服务器吧,首先呢下载 ...
随机推荐
- Python中模块之time&datetime的功能介绍
time&datetime的功能介绍 1. time模块 1. 时间的分类 1. 时间戳:以秒为单位的整数 2. 时间字符格式化:常见的年月日时分秒 3. 时间元祖格式:9大元素,每个元素对应 ...
- 谷歌开发者:看可口可乐公司是怎么玩转TensorFlow的?
在这篇客座文章中,可口可乐公司的 Patrick Brandt 将向我们介绍他们如何使用 AI 和 TensorFlow 实现无缝式购买凭证. 可口可乐的核心忠诚度计划于 2006 年以 MyCoke ...
- jsp根据参数默认选中radio
<% int vol = (Integer)request.getAttribute("cardtype") ; %> <input type="rad ...
- js原生获取元素的css属性
习惯了用jQuery的css()的方法获取元素的css属性,突然不用jquery了,当要获得元素的css时候,我瞬间停顿了一下,咦?咋获取元素的css值?比如获取元素的width.是这样么?docum ...
- 使用Vitrualbox虚拟Windows Server 2016系统的一些常见问题
所有的问题都是基于有路由器的网络环境下进行设置的,所有的vitrualbox都是安装在win7或者win2008系统上进行的. 1.无法创建x64系统? 解决方法:1)进入主板bios设置,开启cpu ...
- 通过内核修改centos密码
在开机启动的时候按键盘上的"E"键会进入如下界面. 选择相应的内核,再次按"E",出现下图,选择第二项,再次按"E"键 经过第二步,这个画面 ...
- FJUT寒假作业第三周数蚂蚁(记录第一道并查集)
http://210.34.193.66:8080/vj/Contest.jsp?cid=162#P7 思路:用并查集合并集合,最后遍历,找到集合的根的个数. 并查集是森林,森林中的每一颗树是一个集合 ...
- 1. 两数之和 LeetCode
给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 示例: 给定 nums = [, , , ], target = 因为 n ...
- Docker的Etcd项目
etcd 是 CoreOS 团队发起的一个管理配置信息和服务发现(service discovery)的项目,在这一章里面,我们将介绍该项目的目标,安装和使用,以及实现的技术. Docker的etcd ...
- windows上安装nodejs,升级npm,安装webpack指南
安装nodejs https://nodejs.org/en/ 安装webpack和其他一些常用的 npm install -g node-gyp webpack coffee-script 监控 w ...