camerax

In this tutorial, we’ll be discussing at length upon Android CameraX API. CameraX was one of the popular releases in Google IO 2019. Let’s see what it has in store for us.

在本教程中,我们将详细讨论Android CameraX API。 CameraX是Google IO 2019中最受欢迎的版本之一。让我们看看它为我们提供的功能。

什么是Android CameraX? (What is Android CameraX?)

Developing Camera Apps has always been hard. Getting a hold of the Camera APIs was never easy.

开发Camera Apps一直很困难。 掌握Camera API绝非易事。

Hence, CameraX which is introduced in the Jetpack Support Library is a respite. It aims at making the development process easier.

因此,Jetpack支持库中引入的CameraX是一个喘息的机会。 它旨在简化开发过程。

Besides API complexity, while developing camera applications, we had to tackle a lot of scenarios such as:

除了API的复杂性,在开发相机应用程序时,我们还必须解决许多情况,例如:

  • OS Versions操作系统版本
  • Device Model specifications – Android Market is fragmented and has a vast variety of device configurations. Same Application can behave differently on different phones especially Samsung.设备型号规范 – Android电子市场是零散的,具有各种各样的设备配置。 同一应用程序在不同的手机(尤其是三星)上的行为可能有所不同。

In this regards, CameraX strives to be more consistent across devices. CameraX basically uses Camera2 API under the hood but with a better implementation process. It is backward compatible until Android Lollipop (SDK 21).

在这方面,CameraX努力使设备之间的一致性更高。 CameraX基本上在后台使用Camera2 API,但实现过程更好。 它向后兼容,直到Android Lollipop(SDK 21)。

How did Google do this?

Google如何做到这一点?

Using an Automated CameraX test lab with a number of devices from various manufacturers.

将Automated CameraX测试实验室与各种制造商的许多设备一起使用。

CameraX has tackled the following issues as per Google IO 2019.

根据Google IO 2019,CameraX解决了以下问题。

Source: Google IO 2019

资料来源:Google IO 2019

Android CameraX:抽象Camera2 API (Android CameraX : Abstracting Camera2 API)

Camera2 API provided us a lot of fine grain control over the sensors. More so, it required a lot of boilerplate code. It required communication with the HAL (Hardware Acceleration Layer) in order to access the hardware and drivers of the camera.

Camera2 API为我们提供了许多对传感器的精细控制。 更重要的是,它需要大量样板代码。 为了访问摄像机的硬件和驱动程序,它需要与HAL(硬件加速层)进行通信。

What CameraX essentially does is abstract all of this.

CameraX本质上所做的就是抽象所有这些。

Hence CameraX provides ease of use, thanks to more readability and less boilerplate code.

因此,由于具有更高的可读性和更少的样板代码,CameraX提供了易用性。

Android CameraX:用例 (Android CameraX : Use Cases)

CameraX has come up with a use case-based approach to focus on the task you need to get done instead of spending time managing device-specific configurations.

CameraX提出了一种基于用例的方法来专注于您需要完成的任务,而不是花费时间管理特定于设备的配置。

The three core use cases are:

三个核心用例是:

  • Preview – What you see. The Camera Feed.预览 –您所看到的。 相机供稿。
  • Image Analysis – What you do. Processing the Camera Feed.图像分析 –您的工作。 处理相机供稿。
  • Image Capture – What you keep. Capturing the photo/video.图像捕获 –您保留的内容。 捕获照片/视频。

Android CameraX扩展 (Android CameraX Extensions)

Extensions basically allow us to use device-native camera features directly in our custom camera application with just a few lines of code.

扩展基本上使我们仅需几行代码就可以直接在自定义相机应用程序中使用设备本机相机功能。

Features like portrait mode, depth, Bokeh effect if supported by the device can be integrated into the use cases easily.

如果设备支持,还可以将肖像模式,深度,散景效果等功能轻松集成到用例中。

onResume() and CameraX.bindToLifecycle()来开始和停止onPause(), we can do that, using onResume()CameraX.bindToLifecycle().onPause()的相机预览以及其他用例,而不必这样做。

Enough talk. Let’s deep dive into CameraX code now.

聊够了。 现在让我们深入研究CameraX代码。

In the section below, we’ll be implementing two use cases – Preview and Image Capture. Image Analyzer would be covered in a separate tutorial.

在下面的部分中,我们将实现两个用例-预览和图像捕获。 图像分析器将在单独的教程中介绍。

CameraX示例项目结构 (CameraX Example Project Structure)

Android Camerax Project Structure

Android Camerax项目结构

CameraX实施代码 (CameraX Implementation Code)

Let’s set up the build.gradle first:

让我们首先设置build.gradle

implementation 'androidx.appcompat:appcompat:1.1.0-alpha03'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
def cameraxVersion = "1.0.0-alpha02"
implementation "androidx.camera:camera-core:${cameraxVersion}"
implementation "androidx.camera:camera-camera2:${cameraxVersion}"

We need to add the permissions for Camera and External Storage in the AndroidManifest.xml file.

我们需要在AndroidManifest.xml文件中添加“摄像机和外部存储”的权限。

The code for the activity_main.xml layout is given below:

下面给出了activity_main.xml布局的代码:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="https://schemas.android.com/apk/res/android"xmlns:app="https://schemas.android.com/apk/res-auto"xmlns:tools="https://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><TextureViewandroid:id="@+id/view_finder"android:layout_width="match_parent"android:layout_height="match_parent"app:layout_constraintTop_toTopOf="parent"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintEnd_toEndOf="parent" /><ImageButtonandroid:id="@+id/imgCapture"android:layout_width="72dp"android:layout_height="72dp"android:layout_margin="24dp"app:srcCompat="@android:drawable/ic_menu_camera"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent" /></androidx.constraintlayout.widget.ConstraintLayout>

The code for the MainActivity.java class is given below.

下面给出MainActivity.java类的代码。

package com.journaldev.androidcamerax;import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.camera.core.CameraX;
import androidx.camera.core.ImageCapture;
import androidx.camera.core.ImageCaptureConfig;
import androidx.camera.core.Preview;
import androidx.camera.core.PreviewConfig;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.lifecycle.LifecycleOwner;import android.content.pm.PackageManager;
import android.graphics.Matrix;
import android.os.Bundle;
import android.os.Environment;
import android.util.Rational;
import android.util.Size;
import android.view.Surface;
import android.view.TextureView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;import java.io.File;public class MainActivity extends AppCompatActivity {private int REQUEST_CODE_PERMISSIONS = 101;private final String[] REQUIRED_PERMISSIONS = new String[]{"android.permission.CAMERA", "android.permission.WRITE_EXTERNAL_STORAGE"};TextureView textureView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);textureView = findViewById(R.id.view_finder);if(allPermissionsGranted()){startCamera(); //start camera if permission has been granted by user} else{ActivityCompat.requestPermissions(this, REQUIRED_PERMISSIONS, REQUEST_CODE_PERMISSIONS);}}private void startCamera() {CameraX.unbindAll();Rational aspectRatio = new Rational (textureView.getWidth(), textureView.getHeight());Size screen = new Size(textureView.getWidth(), textureView.getHeight()); //size of the screenPreviewConfig pConfig = new PreviewConfig.Builder().setTargetAspectRatio(aspectRatio).setTargetResolution(screen).build();Preview preview = new Preview(pConfig);preview.setOnPreviewOutputUpdateListener(new Preview.OnPreviewOutputUpdateListener() {//to update the surface texture we  have to destroy it first then re-add it@Overridepublic void onUpdated(Preview.PreviewOutput output){ViewGroup parent = (ViewGroup) textureView.getParent();parent.removeView(textureView);parent.addView(textureView, 0);textureView.setSurfaceTexture(output.getSurfaceTexture());updateTransform();}});ImageCaptureConfig imageCaptureConfig = new ImageCaptureConfig.Builder().setCaptureMode(ImageCapture.CaptureMode.MIN_LATENCY).setTargetRotation(getWindowManager().getDefaultDisplay().getRotation()).build();final ImageCapture imgCap = new ImageCapture(imageCaptureConfig);findViewById(R.id.imgCapture).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {File file = new File(Environment.getExternalStorageDirectory() + "/" + System.currentTimeMillis() + ".png");imgCap.takePicture(file, new ImageCapture.OnImageSavedListener() {@Overridepublic void onImageSaved(@NonNull File file) {String msg = "Pic captured at " + file.getAbsolutePath();Toast.makeText(getBaseContext(), msg,Toast.LENGTH_LONG).show();}@Overridepublic void onError(@NonNull ImageCapture.UseCaseError useCaseError, @NonNull String message, @Nullable Throwable cause) {String msg = "Pic capture failed : " + message;Toast.makeText(getBaseContext(), msg,Toast.LENGTH_LONG).show();if(cause != null){cause.printStackTrace();}}});}});//bind to lifecycle:CameraX.bindToLifecycle((LifecycleOwner)this, preview, imgCap);}private void updateTransform(){Matrix mx = new Matrix();float w = textureView.getMeasuredWidth();float h = textureView.getMeasuredHeight();float cX = w / 2f;float cY = h / 2f;int rotationDgr;int rotation = (int)textureView.getRotation();switch(rotation){case Surface.ROTATION_0:rotationDgr = 0;break;case Surface.ROTATION_90:rotationDgr = 90;break;case Surface.ROTATION_180:rotationDgr = 180;break;case Surface.ROTATION_270:rotationDgr = 270;break;default:return;}mx.postRotate((float)rotationDgr, cX, cY);textureView.setTransform(mx);}@Overridepublic void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {if(requestCode == REQUEST_CODE_PERMISSIONS){if(allPermissionsGranted()){startCamera();} else{Toast.makeText(this, "Permissions not granted by the user.", Toast.LENGTH_SHORT).show();finish();}}}private boolean allPermissionsGranted(){for(String permission : REQUIRED_PERMISSIONS){if(ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED){return false;}}return true;}
}

PreviewConfig is where we configure the preview which is the live camera feed.

PreviewConfig是我们配置预览的地方,它是实时摄像机的提要。

In the builder, we can set stuff like Aspect Ratios, Lens front or back, and target resolution.

在构建器中,我们可以设置纵横比,镜头前后或目标分辨率等内容。

The Preview is displayed on a TextureView.

预览显示在TextureView

ImageCaptureConfiguration.Builder() is configuration for image when captured. We set different configuration like MIN_LATENCY or MAX_QUALITY.

ImageCaptureConfiguration.Builder()是捕获图像时的配置。 我们设置了不同的配置,例如MIN_LATENCY或MAX_QUALITY。

An output screenshot of the application in action is given below:

运行中的应用程序的输出屏幕截图如下:

Android Camerax Output

Android Camerax输出

That brings an end to this tutorial. You can download the project from the link below or view the full source code from the Github Link provided underneath.

这样就结束了本教程。 您可以从下面的链接下载项目,也可以从下面提供的Github链接查看完整的源代码。

AndroidCameraXAndroidCameraX
Github Project LinkGithub项目链接

翻译自: https://www.journaldev.com/30132/android-camerax-overview

camerax

camerax_Android CameraX概述相关推荐

  1. camerax_Android CameraX OpenCV图像处理

    camerax In this tutorial, we'll be integrating OpenCV in our Android Application. We have already di ...

  2. 现在在Android 14中

    Welcome to Now in Android, your ongoing guide to what's new and notable in the world of Android deve ...

  3. jetpack的camerax_Android开发-Jetpack组件CameraX

    CameraX 是一个 Jetpack 支持库,旨在帮助您简化相机应用的开发工作.它提供一致且易于使用的 API 界面,适用于大多数 Android 设备,并可向后兼容至 Android 5.0(AP ...

  4. Jetpack系列之CameraX 相机

    (一)概述 CameraX 是Jetpack的相机组件,具有强大的参数支持和兼容性,可以实现视频拍摄.预览等操作,且API简单易用. // cameraX api 'androidx.camera:c ...

  5. Java 多线程概述

    多线程技术概述 1.线程与进程 进程:内存中运行的应用程序,每个进程都拥有一个独立的内存空间. 线程:是进程中的一个执行路径,共享一个内存空间,线程之间可以自由切换.并发执行,一个进程最少有一个线程, ...

  6. 【SpringMVC】概述

    概述: SpringMVC:是基于spring的一个框架, 实际上就是spring的一个模块, 专门是做web开发的.                       理解是servlet的一个升级 Sp ...

  7. 梯度下降优化算法概述

    本文原文是 An overview of gradient descent optimization algorithms,同时作者也在 arXiv 上发了一篇同样内容的 论文. 本文结合了两者来翻译 ...

  8. Redis概述和基础

    Redis 1.NoSQL NoSQL = Not Only SQL(不仅仅是SQL) 泛指非关系型数据库的,随着web2.0互联网的诞生!传统的关系型数据库很难对付web2.0时代!尤其是超大规模的 ...

  9. OpenCL™(开放计算语言)概述

    OpenCL™(开放计算语言)概述 异构系统并行编程的开准 OpenCL™(开放计算语言)是一种开放的.免版税的标准,用于对超级计算机.云服务器.个人计算机.移动设备和嵌入式平台中的,各种加速器进行跨 ...

最新文章

  1. 某程序员毕业进UC,被阿里收购!跳去优酷土豆,又被阿里收购!再跳去饿了么,还被阿里收购!难道阿里想收购的是他?...
  2. 学python可以做什么产品-学习Python到底有什么用?
  3. Collections.binarySearch用法
  4. java输出不同颜色_Java设计模式-策略模式、状态模式
  5. linux-ubuntu txt乱码
  6. html5 平移,Html5 canvas绘图旋转和平移
  7. P4859 已经没有什么好害怕的了
  8. 照片转3d模型_云从科技3D人体重建技术刷新3项纪录!仅凭照片即可生成精细模型...
  9. html中黄色的代码是什么,HTML黄色欧美形式创意展示网页模板代码
  10. Win7复制文件时出现:“您需要权限来执行操作!”(终极解决方法!)
  11. 小程序如何隐藏滚动条
  12. vue中的几个动效网站
  13. 自己动手编写仿QQ的app -1注册界面by sdust iot zhl
  14. CSS核心内容-标准流、盒子模型、浮动、定位
  15. 数学建模的基本办法和步骤 ##数模学习1
  16. 八、C语言的基本结构—选择结构
  17. 数学建模入门书籍介绍
  18. 《股票成交量操作策略》
  19. 法宣在线积分小程序获取积分数以源码python技巧
  20. WebRTC源码下载

热门文章

  1. MySQL常用日期时间函数
  2. ios 根据文字数量计算UILabel高度(已修改)
  3. android 的几个黄色警告解决办法(转)
  4. JAVA中的hasNextInt()方法多次调用只有一个结果的原因
  5. 法拉利杀手Koenigsegg CCX
  6. IP 层收发报文简要剖析1-ip报文的输入
  7. BZOJ.3261.最大异或和(可持久化Trie)
  8. 第三百八十一节,Django+Xadmin打造上线标准的在线教育平台—xadmin全局配置
  9. 使用 v-cloak 防止页面加载时出现 vuejs 的变量名
  10. CSS和JS引用图片(资源)的路径问题