android Camera捕获数据
来源:互联网 发布:大肚孕照软件下载 编辑:程序博客网 时间:2024/06/05 18:34
介绍:使用android自带的camera应用程序获取图片并显示在自定义的ImageView控件中。
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/takePhoto" /> <ImageView android:id="@+id/imgView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:contentDescription="@string/imgView"/></LinearLayout>
MainActivity.java
package cn.yh.cameratest;import java.io.File;import android.app.Activity;import android.content.Intent;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Color;import android.graphics.BitmapFactory.Options;import android.net.Uri;import android.os.Bundle;import android.os.Environment;import android.view.Display;import android.view.Menu;import android.widget.ImageView;public class MainActivity extends Activity {private final static int CAMERA_RESULT = 0;private ImageView imgview;private String imageFilePath;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//获取SDCard的位置,并在SDCard根下定义图片的名称。imageFilePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/myCameraTest.jpg";File imageFile = new File(imageFilePath);Uri imageFileUri = Uri.fromFile(imageFile);//android.provider.MediaStore.ACTION_IMAGE_CAPTURE:camera启动意图;Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);//android.provider.MediaStore.EXTRA_OUTPUT:给意图附加参数,指定图片保存的路径;intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageFileUri);//使用startActivityForResult方法,在Camera活动结束之后,进入onActivityResult方法;startActivityForResult(intent, CAMERA_RESULT);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}@Overrideprotected void onActivityResult(int requestCode, int resultCode,Intent intent) {// TODO Auto-generated method stubsuper.onActivityResult(requestCode, resultCode, intent);if (resultCode == RESULT_OK) {//1.简单获取到捕捉到的数据,显示时只是显示一副处理过的缩略图。// Bundle bundle = intent.getExtras();// Bitmap bitmap = (Bitmap) bundle.get("data");//// imgview = (ImageView) findViewById(R.id.imgView);// imgview.setImageBitmap(bitmap);/*2.设置图片大小为原图的1/8BitmapFactory.Options bmFactoryOptions = new Options();bmFactoryOptions.inSampleSize = 8;Bitmap bitmap = BitmapFactory.decodeFile(imageFilePath,bmFactoryOptions);*///3.根据屏幕的大小来显示图片Display display = getWindowManager().getDefaultDisplay();int dw = display.getWidth();int dh = display.getHeight();//加载图像的尺寸,而不是图像本身。BitmapFactory.Options bmFactoryOptions = new Options();bmFactoryOptions.inJustDecodeBounds = true;Bitmap bitmap = BitmapFactory.decodeFile(imageFilePath,bmFactoryOptions);int hRatio = (int) Math.ceil(bmFactoryOptions.outHeight/(float)dh);int wRatio = (int) Math.ceil(bmFactoryOptions.outWidth/(float)dw);//判断是按高比率缩放还是宽比例缩放if(hRatio > 1 && wRatio > 1){if(hRatio > wRatio){bmFactoryOptions.inSampleSize = hRatio;}else{bmFactoryOptions.inSampleSize = wRatio;}}//对图像进行真正的解码bmFactoryOptions.inJustDecodeBounds = false;bitmap = BitmapFactory.decodeFile(imageFilePath,bmFactoryOptions);imgview = (ImageView) findViewById(R.id.imgView);imgview.setBackgroundColor(Color.GRAY);imgview.setImageBitmap(bitmap);}}}
- android Camera捕获数据
- Android使用内置Camera应用程序进行图像捕获
- Android 使用内置的Camera应用程序捕获图像
- Android 使用内置的Camera应用程序捕获图像
- android Camera 数据流程分析
- android Camera 数据流程分析
- android Camera 数据流程分析
- android Camera 数据流程分析
- android Camera 数据流程分析
- ios--OpenCV--视频捕获Camera
- android camera YV12数据旋转方法
- rk3188--9.android camera数据流程分析
- Android Camera采集数据缓存问题
- Android Camera 系统架构源码分析(4)---->Camera的数据来源及Camera的管理
- Android Camera 系统架构源码分析(4)---->Camera的数据来源及Camera的管理
- Android图像处理简介の使用内置Camera应用程序进行图像捕获
- 一起学android之从Camera捕获图像讲解(38)
- com.android.camera.Camera
- 0915
- 使用GridView来获取xml文件数据
- rrtrtyyhh
- python容器排序sort()和内建函数sorted()的区别
- CODE 11: Best Time to Buy and Sell Stock II
- android Camera捕获数据
- #pragma 预处理指令详解
- 最长公共子序列模板
- 经典SQL语句大全
- Android动态加载jar、apk的实现
- JS怎么刷新当前页面
- [各种面试题] 困难的数
- CODE 10: Best Time to Buy and Sell Stock III
- java中常见面试题String,StringBuffer,StringBulider 的区别及相应的使用方法介绍