試手一下CameraX(APP)

書接上回。

首先還是看谷歌的官方文檔:

https://developer.android.com/media/camera/camerax?hl=zh-cn

https://developer.android.com/codelabs/camerax-getting-started?hl=zh-cn#1

注:這里大部分內容也來自谷歌文檔。

官方文檔用的是Kotlin,和Java也大差不差。看看流程就好。

API 級別設置為 21。

我覺得其實從App這個層面,用camera1,camera2還是camerax其實都并不重要,很多重大的改變對app層應該都是不可見的。為什么還要折騰上層呢?這個就要問谷歌的設計師了。。。

1 環境配置

在Gardle中增加依賴,我理解就是增加底層庫,這幾個應該就是camerax所使用的Framework的so,下面應該是AIDL調用的HAL。這個部分和底層驅動關系就很大了,后面還會單獨寫一篇。

dependencies {def camerax_version = "1.1.0-beta01"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}"
}

同時要增加Java8和viewBinding的支持。

    compileOptions {sourceCompatibility JavaVersion.VERSION_1_8targetCompatibility JavaVersion.VERSION_1_8}buildFeatures {viewBinding true}

在Layout的activity_main.xml中使用PreviewView。這個應該是camerax的控件。

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

增加兩個按鍵,分別是takephoto和capturevideo,并增加按鍵事件。

       // Set up the listeners for take photo and video capture buttonsviewBinding.imageCaptureButton.setOnClickListener { takePhoto() }viewBinding.videoCaptureButton.setOnClickListener { captureVideo() }

在AndroidManifest.xml中增加權限。

<uses-feature android:name="android.hardware.camera.any" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"android:maxSdkVersion="28" />

?在運行時會讓你授權。

剛開始運行時,要檢查權限。

override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults:IntArray) {if (requestCode == REQUEST_CODE_PERMISSIONS) {if (allPermissionsGranted()) {startCamera()} else {Toast.makeText(this,"Permissions not granted by the user.",Toast.LENGTH_SHORT).show()finish()}}
}

2 CameraX調用代碼

?主要用到的的幾個包:

import androidx.camera.core.ImageCapture
import androidx.camera.video.Recorder
import androidx.camera.video.Recording
import androidx.camera.video.VideoCapture
2.1 提供圖像預覽:

大體的流程就是首先取得surface,然后使用cameraProvider.bindToLifecycle,將surface作為參數傳進去。

private fun startCamera() {val cameraProviderFuture = ProcessCameraProvider.getInstance(this)cameraProviderFuture.addListener({// Used to bind the lifecycle of cameras to the lifecycle ownerval cameraProvider: ProcessCameraProvider = cameraProviderFuture.get()// Previewval preview = Preview.Builder().build().also {it.setSurfaceProvider(viewBinding.viewFinder.surfaceProvider)}// Select back camera as a defaultval cameraSelector = CameraSelector.DEFAULT_BACK_CAMERAtry {// Unbind use cases before rebindingcameraProvider.unbindAll()// Bind use cases to cameracameraProvider.bindToLifecycle(this, cameraSelector, preview)} catch(exc: Exception) {Log.e(TAG, "Use case binding failed", exc)}}, ContextCompat.getMainExecutor(this))
}
2.2 拍照:

可以看到,基本上就是圍繞著imageCapture.takePicture這個方法。name,contentValues,outputOptions都是作為參數傳進去。

private fun takePhoto() {// Get a stable reference of the modifiable image capture use caseval imageCapture = imageCapture ?: return// Create time stamped name and MediaStore entry.val name = SimpleDateFormat(FILENAME_FORMAT, Locale.US).format(System.currentTimeMillis())val contentValues = ContentValues().apply {put(MediaStore.MediaColumns.DISPLAY_NAME, name)put(MediaStore.MediaColumns.MIME_TYPE, "image/jpeg")if(Build.VERSION.SDK_INT > Build.VERSION_CODES.P) {put(MediaStore.Images.Media.RELATIVE_PATH, "Pictures/CameraX-Image")}}// Create output options object which contains file + metadataval outputOptions = ImageCapture.OutputFileOptions.Builder(contentResolver,MediaStore.Images.Media.EXTERNAL_CONTENT_URI,contentValues).build()// Set up image capture listener, which is triggered after photo has// been takenimageCapture.takePicture(outputOptions,ContextCompat.getMainExecutor(this),object : ImageCapture.OnImageSavedCallback {override fun onError(exc: ImageCaptureException) {Log.e(TAG, "Photo capture failed: ${exc.message}", exc)}override funonImageSaved(output: ImageCapture.OutputFileResults){val msg = "Photo capture succeeded: ${output.savedUri}"Toast.makeText(baseContext, msg, Toast.LENGTH_SHORT).show()Log.d(TAG, msg)}})
}
2.3 拍視頻:

基本就是圍繞著videoCapture.output。name,contentValues,mediaStoreOutputOptions都是作為參數使用。在output中,好像是使用了lambda函數,弄了一些內置行為。

// Implements VideoCapture use case, including start and stop capturing.
private fun captureVideo() {val videoCapture = this.videoCapture ?: returnviewBinding.videoCaptureButton.isEnabled = falseval curRecording = recordingif (curRecording != null) {// Stop the current recording session.curRecording.stop()recording = nullreturn}// create and start a new recording sessionval name = SimpleDateFormat(FILENAME_FORMAT, Locale.US).format(System.currentTimeMillis())val contentValues = ContentValues().apply {put(MediaStore.MediaColumns.DISPLAY_NAME, name)put(MediaStore.MediaColumns.MIME_TYPE, "video/mp4")if (Build.VERSION.SDK_INT > Build.VERSION_CODES.P) {put(MediaStore.Video.Media.RELATIVE_PATH, "Movies/CameraX-Video")}}val mediaStoreOutputOptions = MediaStoreOutputOptions.Builder(contentResolver, MediaStore.Video.Media.EXTERNAL_CONTENT_URI).setContentValues(contentValues).build()recording = videoCapture.output.prepareRecording(this, mediaStoreOutputOptions).apply {if (PermissionChecker.checkSelfPermission(this@MainActivity,Manifest.permission.RECORD_AUDIO) ==PermissionChecker.PERMISSION_GRANTED){withAudioEnabled()}}.start(ContextCompat.getMainExecutor(this)) { recordEvent ->when(recordEvent) {is VideoRecordEvent.Start -> {viewBinding.videoCaptureButton.apply {text = getString(R.string.stop_capture)isEnabled = true}}is VideoRecordEvent.Finalize -> {if (!recordEvent.hasError()) {val msg = "Video capture succeeded: " +"${recordEvent.outputResults.outputUri}"Toast.makeText(baseContext, msg, Toast.LENGTH_SHORT).show()Log.d(TAG, msg)} else {recording?.close()recording = nullLog.e(TAG, "Video capture ends with error: " +"${recordEvent.error}")}viewBinding.videoCaptureButton.apply {text = getString(R.string.start_capture)isEnabled = true}}}}
}

好了,就到這了。先概要看看就行了。

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/717745.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/717745.shtml
英文地址,請注明出處:http://en.pswp.cn/news/717745.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

常用的字符字符串的讀取方法(C / C++)

一、字符 1、讀取單個字符&#xff1a;直接讀取 //輸入a //讀取 char x; scanf("%c",&x); 2、讀取帶空格的字符 h h h 按格式書寫格式化字符串即可 char a,b,c; scanf("%c %c %c",&a,&b,&c); 3、 處理字符間的換行符 假設要讀取以…

Day14:信息打點-主機架構蜜罐識別WAF識別端口掃描協議識別服務安全

目錄 Web服務器&應用服務器差異性 WAF防火墻&安全防護&識別技術 蜜罐平臺&安全防護&識別技術 思維導圖 章節知識點 Web&#xff1a;語言/CMS/中間件/數據庫/系統/WAF等 系統&#xff1a;操作系統/端口服務/網絡環境/防火墻等 應用&#xff1a;APP對象/…

小程序圖形:echarts-weixin 入門使用

去官網下載整個項目&#xff1a; https://github.com/ecomfe/echarts-for-weixin 拷貝ec-canvs文件夾到小程序里面 index.js里面的寫法 import * as echarts from "../../components/ec-canvas/echarts" const app getApp(); function initChart(canvas, width, h…

Vscode 使用SSH遠程連接樹莓派的教程(解決卡在Downloading with wget)

配置Vscode Remote SSH 安裝OpenSSH 打開Windows開始頁面&#xff0c;直接進行搜索PowerShell&#xff0c;打開第一個Windows PowerShell&#xff0c;點擊以管理員身份運行 輸入指令 Get-WindowsCapability -Online | ? Name -like OpenSSH* 我是已經安裝好了&#xff0c;…

學會玩游戲,智能究竟從何而來?

最近在讀梅拉妮米歇爾《AI 3.0》第三部分第九章&#xff0c;談到學會玩游戲&#xff0c;智能究竟從何而來&#xff1f; 作者: [美] 梅拉妮米歇爾 出版社: 四川科學技術出版社湛廬 原作名: Artificial Intelligence: A Guide for Thinking Humans 譯者: 王飛躍 / 李玉珂 / 王曉…

基于springboot實現計算機類考研交流平臺系統項目【項目源碼+論文說明】

基于springboot實現計算機類考研交流平臺系統演示 摘要 高校的大學生考研是繼高校的高等教育更上一層的表現形式&#xff0c;教育的發展是我們社會的根本&#xff0c;那么信息技術的發展又是改變我們生活的重要因素&#xff0c;生活當中各種各樣的場景都存在著信息技術的發展。…

程序員超強大腦——更好地解決編程問題(二)

概念機器 概念機器是計算機的抽象表征&#xff0c;可以借此分析計算機執行的操作。 程序員不僅經常借助概念機器推理計算機的運行方式&#xff0c;而且往往用它來分析代碼。例如&#xff0c;雖然并不存在能夠出存儲數值的實體&#xff0c;但程序員還是會將變量描述為“保存”…

Debezium發布歷史163

原文地址&#xff1a; https://debezium.io/blog/2023/09/23/flink-spark-online-learning/ 歡迎關注留言&#xff0c;我是收集整理小能手&#xff0c;工具翻譯&#xff0c;僅供參考&#xff0c;筆芯筆芯. Online machine learning with the data streams from the database …

SpringBlade CVE-2022-27360 export-user SQL 注入漏洞分析

漏洞描述 SpringBlade是一個基于Spring Cloud和Spring Boot的開發框架&#xff0c;旨在簡化和加速微服務架構的開發過程。它提供了一系列開箱即用的功能和組件&#xff0c;幫助開發人員快速構建高效可靠的微服務應用。該產品/api/blade-user/export-user接口存在SQL注入。 漏…

Java - List集合與Array數組的相互轉換

一、List 轉 Array 使用集合轉數組的方法&#xff0c;必須使用集合的 toArray(T[] array)&#xff0c;傳入的是類型完全一樣的數組&#xff0c;大小就是 list.size() public static void main(String[] args) throws Exception {List<String> list new ArrayList<S…

無處不在的智慧:探索嵌入式系統的奇妙

無處不在的智慧&#xff1a;探索嵌入式系統的奇妙 嵌入式系統作為當今科技領域中無處不在的一種技術&#xff0c;其奇妙之處正在逐步被揭示和探索。從智能家居到智能穿戴設備&#xff0c;從工業自動化到醫療健康&#xff0c;嵌入式系統已經深入到我們生活和工作的方方面面&…

分布式ID生成策略-雪花算法Snowflake

分布式ID生成策略-雪花算法Snowflake 一、其他分布式ID策略1.UUID2.數據庫自增與優化2.1 優化1 - 共用id自增表2.2 優化2 - 分段獲取id 3.Reids的incr和incrby 二、雪花算法Snowflake1.雪花算法的定義2.基礎雪花算法源碼解讀3.并發1000測試4.如何設置機房和機器id4.雪花算法時鐘…

【misc | CTF】BUUCTF 二維碼

天命&#xff1a;這題使用到腳本暴力破解壓縮包文件里面的密碼&#xff0c;還是比較有意思的 一開始是一個二維碼&#xff0c;掃碼進去有一個假flag 扔進圖片隱寫工具&#xff0c;啥也沒有&#xff0c;都是同一個二維碼 使用工具&#xff1a;foremost&#xff0c;直接分離圖片&…

【詳識JAVA語言】抽象類和接口

抽象類 抽象類概念 在面向對象的概念中&#xff0c;所有的對象都是通過類來描繪的&#xff0c;但是反過來&#xff0c;并不是所有的類都是用來描繪對象的&#xff0c;如果 一個類中沒有包含足夠的信息來描繪一個具體的對象&#xff0c;這樣的類就是抽象類。 比如&#xff1a;…

水印相機小程序源碼

水印相機前端源碼&#xff0c;本程序無需后端&#xff0c;前端直接導入即可&#xff0c;沒有添加流量主功能&#xff0c;大家開通后自行添加 源碼搜索&#xff1a;源碼軟件庫 注意小程序后臺的隱私權限設置&#xff0c;前端需要授權才可使用 真實時間地址拍照記錄&#xff0c…

Endnote x9 最快方法批量導入.enw格式文件

按照網上看到的一個方法直接選中所有enw批量拖拽到 All references 附件不行啊&#xff0c; 以為只能寫bat腳本方式了 經過一番嘗試&#xff0c;驚人的發現拖到下面這個符號的地方就行了&#xff01;&#xff01;&#xff01; 如果不成功的話&#xff0c;可能&#xff1a; 我…

使用typescript實現引入vue3生命周期函數的基礎知識整理

在Vue 3中&#xff0c;生命周期函數被更改為組合式API&#xff0c;并且不再使用官方命名的生命周期鉤子函數。不過&#xff0c;我們仍然可以模擬類似的功能&#xff0c;使用onBeforeMount、onMounted、onBeforeUpdate、onUpdated、onBeforeUnmount、onUnmounted等組合式API。 …

淺談vue的自定義指令

Vue 的自定義指令是一種強大的工具&#xff0c;允許你為 DOM 元素添加自定義行為。自定義指令可以通過 Vue 的 Vue.directive() 全局 API 或組件內的 directives 選項來定義。 下面是如何使用 Vue 的自定義指令的基本步驟&#xff1a; 全局注冊自定義指令 Vue.directive(foc…

js 手寫深拷貝方法

文章目錄 一、深拷貝實現代碼二、代碼講解2.1 obj.constructor(obj)2.2 防止循環引用手寫一個深拷貝是我們常見的面試題,在實現過程中我們需要考慮的類型很多,包括對象、數組、函數、日期等。以下就是深拷貝實現邏輯 一、深拷貝實現代碼 const originalObject = {string: H…

藍橋杯復習之差分

題目&#xff1a;空調 題目鏈接&#xff1a;https://www.acwing.com/problem/content/description/4265/ 思路&#xff1a; 對希望溫度與實際溫度做差&#xff0c;再對這個做差數組做差分。我們的每次操作等價于在差分數組中選一個數加一或者選兩個數一個加一&#xff0c…