Android --- CameraX讲解

预备知识

surface   surfaceView   SurfaceHolder

surface 是什么?

一句话来说: surface是一块用于填充图像数据的内存。

surfaceView 是什么?

它是一个显示surface 的View。 在app中仍在 ViewHierachy 中,但在wms 中可以理解为Server端,它与宿主窗口是分离的。(它拥有独立的绘制表面 , 它不与其它宿主窗口共享一个绘制表面,可以单独在一个线程进行绘制,并不会占用主线程的资源,这样,绘制就会比较高效,游戏、视频播放,还有最近几年比较热门的直播,都可以用surface  )

surfaceHolder 是什么?

 它是一个接口,给持有surface的对象使用。可以控制surface的大小和格式。编辑surface中的像素,以及监听surface的变化这个接口通过surfaceView获取。

CameraX

CameraX是Jetpack 支持库

特点:

1、利用的是 camera2 的功能

2、它具有生命周期的感应,使用更加简单,代码量也减少了不少。可以灵活的录制视频和拍照。

3、抹平设备兼容问题。

使用

第一步:

引入依赖

dependencies {
  def camerax_version = "1.2.0-alpha04"
  implementation "androidx.camera:camera-core:${camerax_version}"
  implementation "androidx.camera:camera-camera2:${camerax_version}"
  implementation "androidx.camera:camera-lifecycle:${camerax_version}"
  implementation "androidx.camera:camera-video:${camerax_version}"

  implementation "androidx.camera:camera-view:${camerax_version}"
  implementation "androidx.camera:camera-extensions:${camerax_version}"
}

使用 ViewBinding,因此请使用以下代码(在 android{} 代码块末尾)启用它:

android {
	buildFeatures {
	   viewBinding true
	}
}

第二步:

添加布局控件。   androidx.camera.view.PreViewView

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <androidx.camera.view.PreviewView
        android:id="@+id/viewFinder"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <Button
        android:id="@+id/image_capture_button"
        android:layout_width="110dp"
        android:layout_height="110dp"
        android:layout_marginEnd="50dp"
        android:layout_marginBottom="50dp"
        android:elevation="2dp"
        android:text="@string/take_photo"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@id/vertical_centerline" />

    <Button
        android:id="@+id/video_capture_button"
        android:layout_width="110dp"
        android:layout_height="110dp"
        android:layout_marginStart="50dp"
        android:layout_marginBottom="50dp"
        android:elevation="2dp"
        android:text="@string/start_capture"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toEndOf="@id/vertical_centerline" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/vertical_centerline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent=".50" />

</androidx.constraintlayout.widget.ConstraintLayout>

因为 build.gradle 中设置了 viewBinding = true,所以会为每个布局都生成对应的绑定类(即 activity_main.xml 自动生成 ActivityMainBinding 类)。

第三步:

设置检查相机权限:

在 MainActivity.kt 中设置检查相机权限,设置 Button 的响应事件:

package com.bignerdranch.android.cameraxapp

import android.Manifest
import android.content.pm.PackageManager
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.camera.core.ImageCapture
import androidx.camera.video.Recorder
import and
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值