Camera APP(预览、拍照、录像)

本文详细介绍了如何使用Android Camera2 API创建一个应用,实现相机预览、拍照和录像功能。首先解释了API的概念,然后通过创建名为MyCamera的工程,逐步演示了从布局设计到功能实现的全过程,包括TextureView的自定义、预览功能的实现、拍照逻辑以及录像功能的实现。
摘要由CSDN通过智能技术生成

一、API:Application Programming Interface,应用程序接口,是一些预先定义的接口(如函数、HTTP接口),或指软件系统不同组成部分衔接的约定。用于提供应用程序与开发人员基于某软件或硬件访问的一组例程,而又无需访问源码,或理解内部工作机制的细节。是程序能取得操作系统服务的唯一途径。

二、Camera Demo实现步骤(使用Camera2 API):

        创建一个工程名为MyCamera,实现预览、拍照、录像以及简单设置的功能。

1. 设计界面,修改布局文件activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/black">

    <!--用于显示画面的控件:自定义TextureView-->
    <com.example.mycamera.AutoFitTextureView
        android:id="@+id/text_ture"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="13pt"/>

    <RelativeLayout
        android:id="@+id/app_root"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <!--录像计时显示的数字-->
        <TextView
            android:id="@+id/timerView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_marginTop="8pt"
            android:layout_centerHorizontal="true"
            android:textSize="30sp"
            android:textColor="@color/white"
            android:visibility="gone" />

        <!--录像计时显示的红色图标-->
        <ImageView
            android:id="@+id/timerButton"
            android:layout_width="8dp"
            android:layout_height="8dp"
            android:layout_marginTop="16.5pt"
            android:layout_marginRight="3pt"
            android:layout_toLeftOf="@id/timerView"
            android:background="@drawable/timer_button"
            android:visibility="gone" />

        <!--定时拍摄菜单-->
        <TextView
            android:id="@+id/timerPictureItem"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="定时拍摄"
            android:textColor="@color/white"
            android:textSize="9pt" />

        <!--闪光灯模式菜单-->
        <TextView
            android:id="@+id/flashMenu"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/timerPictureItem"
            android:layout_marginLeft="10pt"
            android:text="闪光灯"
            android:textSize="9pt"
            android:textColor="@color/white"/>

        <!--拍摄比例选择菜单-->
        <TextView
            android:id="@+id/ratioMenu"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/flashMenu"
            android:layout_marginLeft="10pt"
            android:text="照片比例"
            android:textSize="9pt"
            android:textColor="@color/white"/>

        <!--延时摄影模式开启关闭菜单-->
        <TextView
            android:id="@+id/delayVideo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/ratioMenu"
            android:layout_marginLeft="10pt"
            android:text="特殊摄影"
            android:textSize="9pt"
            android:textColor="@color/white"
            android:visibility="gone"/>

        <!--定时拍摄时倒计时显示的数字-->
        <TextView
            android:id="@+id/timerPicture"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:textSize="50sp"
            android:textColor="@color/white"
            android:visibility="gone" />

        <!--拍照、录像功能词条的布局-->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_marginBottom="125dp"
            android:orientation="horizontal"
            android:visibility="visible">

            <!--控制拍照功能布局的文本框-->
            <TextView
                android:id="@+id/paizhao"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginRight="5dp"
                android:layout_weight="1"
                android:gravity="right"
                android:text="拍照"
                android:textColor="#FFFFFF"
                android:textSize="9pt"
                android:visibility="visible" />

            <!--控制录像功能布局的文本框-->
            <TextView
                android:id="@+id/luxiang"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_weight="1"
                android:text="录像"
                android:textColor="#BFBFBF"
                android:textSize="9pt"
                android:visibility="visible" />

        </LinearLayout>

        <!--拍照按钮控件-->
        <Button
            android:id="@+id/take_photo"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="35dp"
            android:background="@drawable/round_button" />
        <!--摄像头切换按钮控件-->
        <ImageView
            android:id="@+id/switch_camera"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_alignParentRight="true"
            android:layout_alignParentBottom="true"
            android:layout_marginRight="58dp"
            android:layout_marginBottom="58dp"
            android:src="@drawable/ic_exchange" />
        <!--缩略图显示控件-->
        <ImageView
            android:id="@+id/showPicture"
            android:layout_width="45dp"
            android:layout_height="45dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentBottom="true"
            android:layout_marginLeft="53dp"
            android:layout_marginBottom="53dp" />

        <!--录像的控件-->
        <!--开始录像按钮控件-->
        <Button
            android:id="@+id/take_video"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:layout_alignParen
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值