【Android实战之旅 004】Android摄像头基础
来源:互联网 发布:linux 重启后挂载失败 编辑:程序博客网 时间:2024/04/27 04:22
Camera在图像识别、二维码、拍照和扫描中应用非常广泛。那么我们如何在自己的APP中使用相机呢?主要有两种方式:1.调用系统相机或者是具有相机功能的应用。2.自定义相机
案例一:调用系统相机获取拍摄图片
在这个案例中我们分别实现了调用系统相机获得缩略图和调用系统相机获得原图
首先的activity_main.xml中定义两个Button和一个ImageView
<?xml version="1.0" encoding="utf-8"?><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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context="com.davebobo.camerademo.MainActivity"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="StartCamera1" android:onClick="startCamera1"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="StartCamera2" android:onClick="startCamera2"/> <ImageView android:id="@+id/iv" android:layout_width="match_parent" android:layout_height="match_parent" /></LinearLayout>在MainActivity.java通过两种不同的方式获取拍摄到的图片
package com.davebobo.camerademo;import android.content.Intent;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.net.Uri;import android.os.Environment;import android.provider.MediaStore;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.ImageView;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;public class MainActivity extends AppCompatActivity { private static int REQ_1 = 1; private static int REQ_2 = 2; private ImageView mImageView; private String mFilePath; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mImageView = (ImageView) findViewById(R.id.iv); mFilePath = Environment.getExternalStorageDirectory().getPath(); mFilePath = mFilePath + "/" + "temp.png"; } //调用系统相机取得缩略图 public void startCamera1(View view){ Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(intent, REQ_1); } //调用系统相机取得原图 public void startCamera2(View view){ Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); Uri photoUri = Uri.fromFile(new File(mFilePath)); intent.putExtra(MediaStore.EXTRA_OUTPUT,photoUri);//更改拍照后的系统存储路径 startActivityForResult(intent, REQ_2); } @Override //重写onActivityResult方法 接收从另外一个Activity中返回的数据 protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode==RESULT_OK){ if (requestCode == REQ_1){ Bundle bundle = data.getExtras(); Bitmap bitmap = (Bitmap) bundle.get("data");//缩略图质量很低,得不到拍到的真实效果 mImageView.setImageBitmap(bitmap); }else if (requestCode ==REQ_2){ FileInputStream fis = null; try { fis= new FileInputStream(mFilePath); Bitmap bitmap = BitmapFactory.decodeStream(fis); mImageView.setImageBitmap(bitmap); } catch (FileNotFoundException e) { e.printStackTrace(); }finally { try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } } } }}
这里我们需要保存图片到sd卡所以需要在AndroidManifest.xml添加权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
运行结果
我们也可以通过Android Device Monitor查看我们保存在SD卡中的图片
0 0
- 【Android实战之旅 004】Android摄像头基础
- 0915Android基础摄像头
- Android摄像头基础
- Android摄像头基础(一)
- android 之摄像头
- android 之摄像头
- android 之摄像头
- android 之摄像头
- (转)android 之摄像头
- Android开发之摄像头
- android 之摄像头
- android之调用摄像头
- Android之使用摄像头拍照
- android之使用摄像头拍照
- Android之调用本地摄像头
- Android开发之摄像头操作
- Android摄像头照相机技术-android学习之旅(八)
- Android摄像头
- SQL面试题目汇总
- android 调用webservice出错
- 机器学习算法之七:5分钟上手SVM
- sql 查询科目成绩以及平均成绩
- 深入理解Redis中的主键失效及其实现机制
- 【Android实战之旅 004】Android摄像头基础
- Leetcode 283 Move zeros
- 如何查看当前ubuntu的版本信息
- CSS3的calc()使用
- redis 学习笔记(7)-cluster 客户端(jedis)代码示例
- 索引
- 关于Android studio的文件冲突问题
- iOS View的Frame和bounds之区别,setbounds使用(深入探究)
- Oracle trunc 函数处理日期格式,日期类型