您好,登錄后才能下訂單哦!
怎樣進(jìn)行Android CameraX打開攝像頭預(yù)覽,相信很多沒有經(jīng)驗(yàn)的人對(duì)此束手無(wú)策,為此本文總結(jié)了問(wèn)題出現(xiàn)的原因和解決方法,通過(guò)這篇文章希望你能解決這個(gè)問(wèn)題。
目標(biāo)很簡(jiǎn)單,用CameraX打開攝像頭預(yù)覽,實(shí)時(shí)顯示在界面上??纯碈ameraX有沒有Google說(shuō)的那么好用。先按最簡(jiǎn)單的來(lái),把預(yù)覽顯示出來(lái)。
模塊gradle的一些配置,使用的Android SDK版本為31,啟用了databinding
apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' apply plugin: 'kotlin-kapt' android { compileSdkVersion 31 buildToolsVersion "31.0.0" defaultConfig { minSdkVersion 21 targetSdkVersion 31 } dataBinding { enabled = true } }
引入CameraX依賴(CameraX 核心庫(kù)是用camera2實(shí)現(xiàn)的),目前主要用1.1.0-alpha11版本
dependencies { implementation "androidx.camera:camera-core:1.1.0-alpha11" implementation "androidx.camera:camera-camera2:1.1.0-alpha11" implementation "androidx.camera:camera-lifecycle:1.1.0-alpha11" implementation "androidx.camera:camera-view:1.0.0-alpha31" implementation "androidx.camera:camera-extensions:1.0.0-alpha31" }
使用1.0.2版本的CameraX核心庫(kù)會(huì)報(bào)錯(cuò),找不到getOrCreateInstance
方法。
??? bug "NoSuchMethodError getOrCreateInstance"
```log CrashHandler: In thread: Thread[main,5,main] UncaughtException detected: java.lang.NoSuchMethodError: No static method getOrCreateInstance(Landroid/content/Context;)Lcom/google/common/util/concurrent/ListenableFuture; in class Landroidx/camera/core/CameraX; or its super classes (declaration of 'androidx.camera.core.CameraX' appears in /data/app/com.rustfisher.tutorial2020-1/base.apk) at androidx.camera.lifecycle.ProcessCameraProvider.getInstance(ProcessCameraProvider.java:149) at com.rustfisher.tutorial2020.camera.SimplePreviewXAct.onCreate(SimplePreviewXAct.java:36) at android.app.Activity.performCreate(Activity.java:6161) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1112) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2507) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2640) at android.app.ActivityThread.access$800(ActivityThread.java:182) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1493) at android.os.Handler.dispatchMessage(Handler.java:111) at android.os.Looper.loop(Looper.java:194) at android.app.ActivityThread.main(ActivityThread.java:5682) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:963) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:758) ```
需要?jiǎng)討B(tài)申請(qǐng)android.permission.CAMERA
權(quán)限
<uses-permission android:name="android.permission.CAMERA" />
本文略過(guò)動(dòng)態(tài)申請(qǐng)權(quán)限的地方
CameraX為開發(fā)者貼心地準(zhǔn)備了androidx.camera.view.PreviewView
把它放在一個(gè)FrameLayout里,如下的act_simple_preivew_x.layout
<?xml version="1.0" encoding="utf-8"?> <layout> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent"> <androidx.camera.view.PreviewView android:id="@+id/previewView" android:layout_width="match_parent" android:layout_height="match_parent" /> </FrameLayout> </layout>
在activity中開啟相機(jī)預(yù)覽
// SimplePreviewXAct.java import android.os.Bundle; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; import androidx.camera.core.Camera; import androidx.camera.core.CameraSelector; import androidx.camera.core.Preview; import androidx.camera.lifecycle.ProcessCameraProvider; import androidx.core.content.ContextCompat; import androidx.databinding.DataBindingUtil; import androidx.lifecycle.LifecycleOwner; import com.google.common.util.concurrent.ListenableFuture; // import com.rustfisher.tutorial2020.R; // import com.rustfisher.tutorial2020.databinding.ActSimplePreivewXBinding; import java.util.concurrent.ExecutionException; public class SimplePreviewXAct extends AppCompatActivity { private ActSimplePreivewXBinding mBinding; private ListenableFuture<ProcessCameraProvider> cameraProviderFuture; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); mBinding = DataBindingUtil.setContentView(this, R.layout.act_simple_preivew_x); cameraProviderFuture = ProcessCameraProvider.getInstance(this); cameraProviderFuture.addListener(() -> { try { ProcessCameraProvider cameraProvider = cameraProviderFuture.get(); bindPreview(cameraProvider); } catch (ExecutionException | InterruptedException e) { // 這里不用處理 } }, ContextCompat.getMainExecutor(this)); } void bindPreview(@NonNull ProcessCameraProvider cameraProvider) { Preview preview = new Preview.Builder().build(); CameraSelector cameraSelector = new CameraSelector.Builder() .requireLensFacing(CameraSelector.LENS_FACING_BACK) .build(); preview.setSurfaceProvider(mBinding.previewView.getSurfaceProvider()); Camera camera = cameraProvider.bindToLifecycle(this, cameraSelector, preview); } }
注意我們這里使用的是androidx.appcompat.app.AppCompatActivity
為了獲得ProcessCameraProvider,用ProcessCameraProvider.getInstance
方法拿到一個(gè)cameraProviderFuture
。
在cameraProviderFuture
完成后取出ProcessCameraProvider(cameraProvider
)。
要開啟預(yù)覽,通過(guò)Preview.Builder
構(gòu)建一個(gè)Preview。用CameraSelector來(lái)選擇后置攝像頭。
Preview的SurfaceProvider由layout中的androidx.camera.view.PreviewView提供。
cameraProvider.bindToLifecycle
綁定上后,啟動(dòng)攝像頭預(yù)覽
運(yùn)行到手機(jī)上,打開這個(gè)Activity就可以看到攝像頭預(yù)覽。圖像寬高比正常,沒有拉伸現(xiàn)象。
榮耀 EMUI 3.1 Lite,Android 5.1 運(yùn)行正常
Redmi 9A,MIUI 12.5.1穩(wěn)定版,Android 10 運(yùn)行正常
一加5,H2OS 10.0.3,Android 10 運(yùn)行正常
在layout里加2個(gè)按鈕,控制相機(jī)開關(guān)
<?xml version="1.0" encoding="utf-8"?> <layout> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorPrimaryDark" android:gravity="center" android:orientation="horizontal" android:padding="4dp"> <Button android:id="@+id/start" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="打開" /> <Button android:id="@+id/end" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="12dp" android:text="關(guān)閉" /> </LinearLayout> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent"> <androidx.camera.view.PreviewView android:id="@+id/previewView" android:layout_width="match_parent" android:layout_height="match_parent" /> </FrameLayout> </LinearLayout> </layout>
根layout換成LinearLayout
修改bindPreview
方法,先檢查傳入的ProcessCameraProvider是否為空
private void bindPreview(ProcessCameraProvider cameraProvider) { if (cameraProvider == null) { Toast.makeText(getApplicationContext(), "沒獲取到相機(jī)", Toast.LENGTH_SHORT).show(); return; } Preview preview = new Preview.Builder().build(); CameraSelector cameraSelector = new CameraSelector.Builder() .requireLensFacing(CameraSelector.LENS_FACING_BACK) .build(); preview.setSurfaceProvider(mBinding.previewView.getSurfaceProvider()); Camera camera = cameraProvider.bindToLifecycle(this, cameraSelector, preview); }
修改后的activity部分代碼
import android.os.Bundle; import android.widget.Toast; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; import androidx.camera.core.Camera; import androidx.camera.core.CameraSelector; import androidx.camera.core.Preview; import androidx.camera.lifecycle.ProcessCameraProvider; import androidx.core.content.ContextCompat; import androidx.databinding.DataBindingUtil; import com.google.common.util.concurrent.ListenableFuture; // import com.rustfisher.tutorial2020.R; // import com.rustfisher.tutorial2020.databinding.ActSimplePreivewXBinding; import java.util.concurrent.ExecutionException; public class SimplePreviewXAct extends AppCompatActivity { private ActSimplePreivewXBinding mBinding; private ListenableFuture<ProcessCameraProvider> mCameraProviderFuture; private ProcessCameraProvider mCameraProvider; private boolean mRunning = false; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); mBinding = DataBindingUtil.setContentView(this, R.layout.act_simple_preivew_x); mCameraProviderFuture = ProcessCameraProvider.getInstance(this); mCameraProviderFuture.addListener(() -> { try { mCameraProvider = mCameraProviderFuture.get(); } catch (ExecutionException | InterruptedException e) { // 這里不用處理 } }, ContextCompat.getMainExecutor(this)); mBinding.start.setOnClickListener(v -> { if (mCameraProvider != null && !mRunning) { bindPreview(mCameraProvider); } }); mBinding.end.setOnClickListener(v -> { mCameraProvider.unbindAll(); mRunning = false; }); } private void bindPreview(ProcessCameraProvider cameraProvider) { if (cameraProvider == null) { Toast.makeText(getApplicationContext(), "沒獲取到相機(jī)", Toast.LENGTH_SHORT).show(); return; } Preview preview = new Preview.Builder().build(); CameraSelector cameraSelector = new CameraSelector.Builder() .requireLensFacing(CameraSelector.LENS_FACING_BACK) .build(); preview.setSurfaceProvider(mBinding.previewView.getSurfaceProvider()); Camera camera = cameraProvider.bindToLifecycle(this, cameraSelector, preview); mRunning = true; } }
拿到mCameraProvider
后不要立刻綁定生命周期。
如果要開啟預(yù)覽,則調(diào)用bindPreview(mCameraProvider)
。記錄一下現(xiàn)在相機(jī)已經(jīng)開啟預(yù)覽mRunning = true
。
如果要停止預(yù)覽,則解綁生命周期mCameraProvider.unbindAll()
。這個(gè)方法需要在主線程調(diào)用。
運(yùn)行起來(lái)后,可以用按鈕來(lái)控制相機(jī)預(yù)覽的開關(guān)。相比之前,PreviewView的高度變小了一點(diǎn)(讓了點(diǎn)位置給按鈕)。
但視頻寬高比例正常,沒有被拉伸。默認(rèn)的配置下,還有自動(dòng)對(duì)焦的功能。
從簡(jiǎn)單的打開相機(jī)預(yù)覽來(lái)看,CameraX簡(jiǎn)化了開發(fā)者的工作。提供了PreviewView,開發(fā)者不需要自定義SurfaceView或者TextureView。實(shí)時(shí)預(yù)覽中,相機(jī)能夠自動(dòng)對(duì)焦。
看完上述內(nèi)容,你們掌握怎樣進(jìn)行Android CameraX打開攝像頭預(yù)覽的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。