Android相機教程含代碼

Android相機教程

相機 主要用于拍攝照片和視頻。愛掏網 - it200.com我們可以通過使用相機 API 的方法來控制相機。愛掏網 - it200.com

Android提供了兩種使用相機的方式:

  1. 通過相機意圖
  2. 通過相機 API

主要有四個類我們將討論。愛掏網 - it200.com

意圖

通過 MediaStore 類的2個常量的幫助下,我們可以在不使用相機類的實例的情況下拍攝照片和視頻。愛掏網 - it200.com

  1. ACTION_IMAGE_CAPTURE
  2. ACTION_VIDEO_CAPTURE

相機

這是相機 API 的主要類,可用于拍攝照片和視頻。愛掏網 - it200.com

SurfaceView

它表示相機的表面視圖或實時預覽。愛掏網 - it200.com

MediaRecorder

它用于使用相機錄制視頻。愛掏網 - it200.com正如我們在媒體框架的先前示例中看到的那樣,它也可以用于錄制音頻文件。愛掏網 - it200.com

Android相機應用程序通過相機意圖的示例

在這個示例中,我們編寫了簡單的代碼,使用相機捕捉圖像,并使用imageview顯示圖像。愛掏網 - it200.com

activity_main.xml

從面板中拖動一個imageview和一個button,現(xiàn)在xml文件將如下所示:

<RelativeLayout xmlns:androclass="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

   <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="Take a Photo" >
    </Button>

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@+id/button1"
        android:layout_alignParentTop="true"
        android:src="https://deepinout.com/android/android-top-articles/@drawable/ic_launcher" >
    </ImageView>
</RelativeLayout>

Activity類

讓我們編寫代碼,使用攝像頭捕獲圖像并在圖像視圖上顯示它。愛掏網 - it200.com

package com.example.simplecamera;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {
     private static final int CAMERA_REQUEST = 1888;
     ImageView imageView;
     public void onCreate(Bundle savedInstanceState) {

         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);

         imageView = (ImageView) this.findViewById(R.id.imageView1);
         Button photoButton = (Button) this.findViewById(R.id.button1);

         photoButton.setOnClickListener(new View.OnClickListener() {

         @Override
         public void onClick(View v) {
              Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
              startActivityForResult(cameraIntent, CAMERA_REQUEST);
         }
        });
       }

     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      if (requestCode == CAMERA_REQUEST) {
       Bitmap photo = (Bitmap) data.getExtras().get("data");
       imageView.setImageBitmap(photo);
      }
   }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}

輸出:

聲明:所有內容來自互聯(lián)網搜索結果,不保證100%準確性,僅供參考。如若本站內容侵犯了原著者的合法權益,可聯(lián)系我們進行處理。
發(fā)表評論
更多 網友評論0 條評論)
暫無評論

返回頂部

主站蜘蛛池模板: 亚洲va久久久噜噜噜久久| 欧美亚洲国产精品久久高清| 成年无码av片在线| 婷婷色香五月综合激激情| 国产91精品系列在线观看| 久久丫精品久久丫| 蜜桃视频无码区在线观看| 日本不卡一二三| 国产一级一级毛片| 中文字幕久久网| 综合网激情五月| 小帅男同志chinesecouple| 免费日产乱码卡一卡2卡三卡四| 一二三区免费视频| 久久久久久久久人体| 最近中字视频在线观看| 国产成人教育视频在线观看| 久久精品人妻中文系列| 贱妇汤如丽全篇小说| 投资6000能开一个sf吗| 办公室娇喘的短裙老师在线视频| √天堂中文官网8在线| 爱穿丝袜的麻麻3d漫画免费| 国产高清在线视频| 午夜电影成人福利| www.尤物视频.com| 色综合久久一本首久久| 成人在线观看不卡| 免费A级毛片在线播放不收费| 中文字幕在线视频精品| 精品人妻中文无码AV在线| 夜夜爽77777妓女免费看| 亚洲成人黄色网址| 黑人巨大精品播放| 成年女人18级毛片毛片免费 | 亚洲综合15p| 一级毛片在线播放免费| 草莓视频污在线观看| 性欧美wideos| 亚洲福利视频一区二区三区| caopon国产在线视频|