zoukankan      html  css  js  c++  java
  • android摄像头获取图像——第一弹

    http://www.cnblogs.com/mengyan/archive/2012/09/01/2666636.html

    安卓读取视频的几种方式:

    详细讲述请参考网址:http://www.cnblogs.com/over140/archive/2011/11/16/2251344.html


    一、准备工作
    1.用户权限
    首先要确保在manifest中声明了对摄像头的使用及其他相关的feature;
    manifest中定义:
    (1)Camera权限——应用程序必须对请求摄像头的使用权限,代码:


    <uses-permission android:name="android.permission.CAMERA" />


    (2) Camera Feature——应用程序必须同时声明对camera feature的使用,代码:


    <uses-feature android:name="android.hardware.camera" />


    (3)以上是两个主要的设置,如果有其他额外功能,比如使用自动对焦,还需要使用:


    <uses-feature android:name="android.hardware.camera.autofocus" />


    详细内容可以参考文章前面的网站内容;


    二、实现方法


    1.使用intent通知安卓操作系统
    (1)在主activity中使用intent通知内置的摄像机应用;用户在使用摄像进行拍照过后返回主activity图片数据,在主activity 
    中加入接收数据的方法,对返回的图像进行操纵;
    优点:这种方法比较简单,且利用了android内置的应用,使用于一般性的应用程序开发;
    缺点:不够灵活,不适用自定义的方法,可扩展性较差;
    (2)调用步骤
    通常按以下步骤来提交一个摄像头 intent:
    首先,构建一个摄像头 Intent —— 用以下意图类型之一,创建一个请求图像或视频的Intent,
    MediaStore.ACTION_IMAGE_CAPTURE —— 向内置摄像头程序请求图像的意图活动类型。
    MediaStore.ACTION_VIDEO_CAPTURE —— 向内置摄像头程序请求视频的意图活动类型。
    然后,启动摄像头 Intent ——用startActivityForResult()方法执行摄像头 intent。启动完毕后摄像头应用的用户界面就会显 
    示在屏幕上,用户就可以拍照或摄像了。
    最后,接收Intent结果 —— 在应用程序中设置onActivityResult()方法,用于接收从摄像头 intent返回的数据。当用户拍摄完 
    毕后(或者取消操作),系统会调用此方法。
    (3)实例

    详细代码可见文章顶部网站,本文提供了一个简单的程序:


    package cn.edu.zjut.androidcam;
    
    
    import android.os.Bundle;
    import android.provider.MediaStore;
    import android.app.Activity;
    import android.content.Intent;
    import android.graphics.Bitmap;
    import android.view.View;
    import android.widget.Button;
    import android.widget.ImageView;
    /**
     * 
    * @ClassName: MainActivity 
    * @Description:通过intent调用android内置的摄像应用程序
    * @author Alfred.M 
    * @date 2012-9-1 下午2:35:12 
    *
     */
    public class MainActivity extends Activity {
        private static final int CAMERA_REQUEST = 1888;
        private ImageView imageView;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            this.imageView = (ImageView) this.findViewById(R.id.imageView1);
            Button photoButton = (Button) this.findViewById(R.id.button1);
            
            Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(cameraIntent, CAMERA_REQUEST);
            photoButton.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//构造intent
                    startActivityForResult(cameraIntent, CAMERA_REQUEST);//发出intent,并要求返回调用结果
                }
            });
        }
    
        /**
         * 接收intent传回的信息
         */
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            if (requestCode == CAMERA_REQUEST) {
                //System.exit(0);
                Bitmap photo = (Bitmap) data.getExtras().get("data");
                imageView.setImageBitmap(photo);
            }
        }
    }

    <RelativeLayout 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" >
    
       <TextView
            android:id="@+id/text2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:text="@string/hello_world"
            tools:context=".MainActivity" />
    
        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription="@string/app_name" >
        </ImageView>
    
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignRight="@+id/text2"
            android:layout_below="@+id/imageView1"
            android:layout_marginRight="14dp"
            android:layout_marginTop="79dp"
            android:text="@string/button" />
    </RelativeLayout>



  • 相关阅读:
    MSSQL Join的使用
    MSSQL2008 常用sql语句
    Process使用
    c# 多线程 调用带参数函数
    C# 多线程参数传递
    C# 单例模式代码
    C#调用存储过程
    页面布局
    构建:vue项目配置后端接口服务信息
    浏览器工作原理(二):浏览器渲染过程概述
  • 原文地址:https://www.cnblogs.com/nafio/p/9137304.html
Copyright © 2011-2022 走看看