- 添加依赖
-
def camerax_version = "1.0.0-beta07"
implementation "androidx.camera:camera-camera2:$camerax_version"
implementation "androidx.camera:camera-lifecycle:$camerax_version"
implementation "androidx.camera:camera-view:1.0.0-alpha14"
- 声明权限
-
<uses-feature android:name="android.hardware.camera.any" />
<uses-permission android:name="android.permission.CAMERA" />
- 预览布局
-
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.camera.view.PreviewView
android:id="@+id/previewView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
- 代码部署
-
package com.melon.camerademo;
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.camera.core.CameraSelector;
import androidx.camera.core.ImageAnalysis;
import androidx.camera.core.ImageCapture;
import androidx.camera.core.ImageProxy;
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;
import java.nio.ByteBuffer;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class MyActivity extends AppCompatActivity {
private static final String TAG = "MyActivity";
private static final int CAMERA_REQUEST_CODE = 100;
private static final CameraSelector MY_CAMERA = CameraSelector.DEFAULT_BACK_CAMERA;
private PreviewView mPreviewView;
private ImageCapture mImageCapture;
ExecutorService mCameraExecutor = Executors.newSingleThreadExecutor();
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
//启动摄像头
startCamera();
}
private void initView() {
mPreviewView = findViewById(R.id.previewView);
}
private void startCamera() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "没有摄像头权限", Toast.LENGTH_LONG).show();
Log.e(TAG, "没有摄像头权限");
//权限申请
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, CAMERA_REQUEST_CODE);
return;
}
Log.d(TAG, "有了摄像头权限");
ListenableFuture<ProcessCameraProvider> cameraProviderFuture = ProcessCameraProvider.getInstance(this);
cameraProviderFuture.addListener(new Runnable() {
@Override
public void run() {
try {
ProcessCameraProvider cameraProvider = cameraProviderFuture.get();
Preview preview = new Preview.Builder().build();
preview.setSurfaceProvider(mPreviewView.createSurfaceProvider());
mImageCapture = new ImageCapture.Builder().build();
//图像分析
ImageAnalysis imageAnalyzer = new ImageAnalysis.Builder().build();
imageAnalyzer.setAnalyzer(mCameraExecutor, new ImageAnalysis.Analyzer() {
@Override
public void analyze(@NonNull ImageProxy image) {
// 分析
ByteBuffer byteBuffer = image.getPlanes()[0].getBuffer();
int pixelStride = image.getPlanes()[0].getPixelStride();
int rowStride = image.getPlanes()[0].getRowStride();
ByteBuffer byteBuffer1 = image.getPlanes()[1].getBuffer();
int pixelStride1 = image.getPlanes()[1].getPixelStride();
int rowStride1 = image.getPlanes()[1].getRowStride();
ByteBuffer byteBuffer2 = image.getPlanes()[2].getBuffer();
int pixelStride2 = image.getPlanes()[2].getPixelStride();
int rowStride2 = image.getPlanes()[2].getRowStride();
int rotationDegrees = image.getImageInfo().getRotationDegrees();
int format = image.getFormat(); //CameraX默认出图格式:YUV_420_888 YYYY UV VU,数据分别在image.getPlanes()[0],image.getPlanes()[1],image.getPlanes()[2]中
int width = image.getWidth();
int height = image.getHeight();
Log.d(TAG, "capacity: " + byteBuffer.capacity()
+ ", pixelStride: " + pixelStride
+ ", rowStride: " + rowStride
+ ", capacity1: " + byteBuffer1.capacity()
+ ", pixelStride1: " + pixelStride1
+ ", rowStride1: " + rowStride1
+ ", capacity2: " + byteBuffer2.capacity()
+ ", pixelStride2: " + pixelStride2
+ ", rowStride2: " + rowStride2
+ ", rotationDegrees: " + rotationDegrees
+ ", format: " + format
+ ", width: " + width
+ ", height: " + height
);
image.close();
}
});
cameraProvider.unbindAll();
cameraProvider.bindToLifecycle(MyActivity.this, MY_CAMERA, preview, mImageCapture, imageAnalyzer);
} catch (Exception e) {
Log.e(TAG, "error: " + e.getMessage());
}
}
}, ContextCompat.getMainExecutor(this));
}
}
|