PreviewView
是 Android Jetpack CameraX 庫中的一個組件,用于預覽相機捕獲的圖像和視頻。要使用 PreviewView
,請按照以下步驟操作:
在 build.gradle
(app模塊)文件中添加以下依賴項:
dependencies {
implementation 'androidx.camera:camera-core:1.0.0'
implementation 'androidx.camera:camera-preview:1.0.0'
implementation 'androidx.camera:camera-camera2:1.0.0'
implementation 'androidx.camera:camera-lifecycle:1.0.0'
implementation 'androidx.camera:camera-view:1.0.0'
}
在布局文件(例如 activity_main.xml
)中添加 PreviewView
組件:
<androidx.camera.core.PreviewView
android:id="@+id/previewView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
在 Activity 或 Fragment 中配置 CameraX。首先,獲取 PreviewView
的引用,然后創建一個 CameraSelector
,選擇一個相機設備(后置或前置)。接下來,創建一個 ImageAnalysisConfig
和一個 ImageAnalyzer
,用于分析圖像。最后,使用 CameraProvider
創建一個 Preview
和一個 ImageAnalyzer
,并將它們綁定到 PreviewView
。
以下是一個簡單的示例:
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.camera.core.CameraSelector;
import androidx.camera.core.ImageAnalysis;
import androidx.camera.core.ImageAnalyzer;
import androidx.camera.core.ImageProxy;
import androidx.camera.core.Preview;
import androidx.camera.core.SurfaceTextureProvider;
import androidx.camera.lifecycle.ProcessCameraProvider;
import androidx.camera.view.PreviewView;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class MainActivity extends AppCompatActivity {
private static final int PERMISSION_REQUEST_CODE = 1;
private PreviewView previewView;
private ProcessCameraProvider cameraProvider;
private ImageAnalyzer imageAnalyzer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
previewView = findViewById(R.id.previewView);
// 檢查相機權限
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, PERMISSION_REQUEST_CODE);
} else {
bindPreviewView();
}
}
private void bindPreviewView() {
// 創建一個 SurfaceTextureProvider,用于預覽視圖
SurfaceTextureProvider surfaceTextureProvider = SurfaceTextureProvider.createSurfaceTextureProvider(previewView.getSurfaceTexture());
// 創建一個 ImageAnalyzer
imageAnalyzer = new SimpleImageAnalyzer();
// 創建一個 Preview
Preview preview = new Preview.Builder().setSurfaceProvider(surfaceTextureProvider).build();
// 創建一個 CameraSelector
CameraSelector cameraSelector = new CameraSelector.Builder().requireLensFacing(CameraSelector.LENS_FACING_BACK).build();
// 創建一個 ProcessCameraProvider
cameraProvider = ProcessCameraProvider.getInstance(this);
// 將 Preview 和 ImageAnalyzer 綁定到 PreviewView
previewView.setSurfaceProvider(surfaceTextureProvider);
cameraProvider.unbindAll();
cameraProvider.bindToLifecycle(this, cameraSelector, preview, imageAnalyzer);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == PERMISSION_REQUEST_CODE) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
bindPreviewView();
} else {
// 處理權限請求失敗的情況
}
}
}
}
在這個示例中,我們創建了一個簡單的 ImageAnalyzer
,用于分析圖像。你可以根據需要替換為你自己的實現。
現在,當你運行應用程序時,你應該能夠看到相機的預覽圖像。你可以根據需要對 PreviewView
進行進一步的定制,例如更改其大小、位置等。