CameraX Demo
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<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" />
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.CameraX"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="android.app.lib_name"
android:value="" />
</activity>
</application>
</manifest>
package com.example.camerax;
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraManager;
import android.os.Build;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.camera.core.CameraSelector;
import androidx.camera.core.Preview;
import androidx.camera.lifecycle.ProcessCameraProvider;
import androidx.camera.view.PreviewView;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import com.google.common.util.concurrent.ListenableFuture;
public class MainActivity extends AppCompatActivity {
private CameraManager mCameraManager;
private PreviewView previewView;
private final int REQUEST_CODE_PERMISSIONS = 10;
private String[] REQUIRED_PERMISSIONS = {Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO, ""};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
previewView = findViewById(R.id.viewFinder);
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.P) {
REQUIRED_PERMISSIONS[2] = Manifest.permission.WRITE_EXTERNAL_STORAGE;
}
// Request camera permissions
if (allPermissionsGranted()) {
startCamera();
} else {
ActivityCompat.requestPermissions(
this, REQUIRED_PERMISSIONS, REQUEST_CODE_PERMISSIONS);
}
// Set up the listeners for take photo and video capture buttons
findViewById(R.id.image_capture_button).setOnClickListener(v -> {
takePhoto();
});
findViewById(R.id.video_capture_button).setOnClickListener(v -> {
captureVideo();
});
}
private void captureVideo() {
}
private void takePhoto() {
}
private void startCamera() {
ListenableFuture<ProcessCameraProvider> future = ProcessCameraProvider.getInstance(this);
future.addListener(() -> {
try {
ProcessCameraProvider cameraProvider = future.get();
bindPreView(cameraProvider);
} catch (Exception e) {
e.printStackTrace();
}
}, ContextCompat.getMainExecutor(this));
}
private void bindPreView(ProcessCameraProvider cameraProvider) {
Preview preview = new Preview.Builder().build();
cameraProvider.bindToLifecycle(this, CameraSelector.DEFAULT_BACK_CAMERA, preview);
preview.setSurfaceProvider(previewView.getSurfaceProvider());
}
private boolean allPermissionsGranted() {
for (int i = 0; i < REQUIRED_PERMISSIONS.length; i++) {
if (ActivityCompat.checkSelfPermission(this, REQUIRED_PERMISSIONS[i]) != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
return true;
}
private void init(){
// 获取摄像头的管理者CameraManager
mCameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
try {
String[] cameraIdList = mCameraManager.getCameraIdList();
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
}
标签:CameraX,java,androidx,Demo,void,private,CameraManager,import,android
From: https://www.cnblogs.com/zybao/p/16968533.html