本文目的
使用 Camera API 进行视频的采集,分别使用 SurfaceView、TextureView 来预览 Camera 数据,取到 NV21 的数据回调
SurfaceView优缺点
优点:
可以在一个独立的线程中进行绘制,不会影响主线程
使用双缓冲机制,播放视频时画面更流畅
缺点:
Surface不在View hierachy中,它的显示也不受View的属性控制,所以不能进行平移,缩放等变换,也不能放在其它ViewGroup中。SurfaceView
不能嵌套使用。
TextureView作用
TextureView可用于显示内容流,内容流可以是视频或者OpenGL的场景。内容流可来自应用进程或是远程其它进程。
Textureview必须在硬件加速开启的窗口中使用。
注意:若是软解,TextureView不会显示东西。
TextureView优点及缺点
优点:
支持移动、旋转、缩放等动画,支持截图
缺点:
必须在硬件加速的窗口中使用,占用内存比SurfaceView高,在5.0以前在主线程渲染,5.0以后有单独的渲染线程。
准备工作
添加相机权限
<uses-permission android:name="android.permission.CAMERA" />
注意:camera预览回调中默认使用NV21格式
UI 准备
<!-- 全屏显示 -->
<style name="FullScreenTheme" parent="AppTheme">
<item name="windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
</style>
承载预览图像
<FrameLayout
android:id="@+id/camera_preview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
使用 SurfaceView 预览 Camera,取到NV21数据
- 自定义CameraPreview继承SurfaceView,实现SurfaceHolder.Callback接口
- 获取NV21数据,Camera.setPreviewCallback() 要放在Camera.startPreview() 之前。使用Camera.PreviewCallback获取预览数据回调。默认是NV21格式。
- surfaceChanged中,camera启动预览前可以进行设置,例如设置尺寸,调整方向
/**
* camera预览视图
* Created by on 2020/2/26.
*/
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
private static final String TAG = "CameraPreview";
private SurfaceHolder mHolder;
private Camera mCamera;
private int mFrameCount = 0;
public CameraPreview(Context context) {
super(context);
}
public CameraPreview(Context context, Camera camera) {
super(context);
mCamera = camera;
mHolder = getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void setCamera(Camera c) {
this.mCamera = c;
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
// 开启预览
try {
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();
} catch (IOException e) {
Log.d(TAG, "设置相机预览失败: " + e.getMessage());
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// 可在此释放camera
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
// 若需要旋转、更改大小或重新设置,请确保证已停止预览
if (mHolder.getSurface() == null) {
return;
}
try {
mCamera.stopPreview();
} catch (Exception e) {
// 在此停止不存在的预览
}
Camera.Parameters parameters = mCamera.getParameters();
// ImageFormat.NV21 == 17
if (this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) {
mCamera.setDisplayOrientation(90);
} else {
mCamera.setDisplayOrientation(0);
}
try {
mCamera.setPreviewDisplay(mHolder);
mCamera.setPreviewCallback(mCameraPreviewCallback); // 回调要放在 startPreview() 之前
mCamera.startPreview();
} catch (Exception e) {
Log.d(TAG, "开启相机预览失败: " + e.getMessage());
}
}
private Camera.PreviewCallback mCameraPreviewCallback = new Camera.PreviewCallback() {
@Override
public void onPreviewFrame(byte[] data, Camera camera) {
mFrameCount++;
Log.d(TAG, "onPreviewFrame: data.length=" + data.length + ", frameCount=" + mFrameCount);
}
};
}
为了防止阻塞UI线程,在子线程中打开camera。camera常放在try catch中使用。
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private Camera mCamera;
private CameraPreview mPreview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new InitCameraThread().start();
}
@Override
protected void onResume() {
if (null == mCamera) {
if (safeCameraOpen()) {
mPreview.setCamera(mCamera); // 重新获取camera操作权
} else {
Log.e(TAG, "无法操作camera");
}
}
super.onResume();
}
@Override
protected void onPause() {
super.onPause();
releaseCamera();
}
private boolean safeCameraOpen() {
boolean qOpened = false;
try {
releaseCamera();
mCamera = Camera.open();
qOpened = (mCamera != null);
} catch (Exception e) {
Log.e(TAG, "打开相机失败");
e.printStackTrace();
}
return qOpened;
}
private void releaseCamera() {
if (mCamera != null) {
mCamera.setPreviewCallback(null);
mCamera.release(); // release the camera for other applications
mCamera = null;
}
}
private class InitCameraThread extends Thread {
@Override
public void run() {
super.run();
if (safeCameraOpen()) {
Log.d(TAG, "开启摄像头");
runOnUiThread(new Runnable() {
@Override
public void run() {
mPreview = new CameraPreview(MainActivity.this, mCamera);
FrameLayout preview = findViewById(R.id.camera_preview);
preview.addView(mPreview);
}
});
}
}
}
}
使用 TextureView 预览 Camera,取到NV21数据
- 使用TextureView很简单:获取到它的SurfaceTexture,使用SurfaceTexture呈现内容。
- CameraPreview继承了TextureView,外部需要传入camera实例。在onSurfaceTextureAvailable中,配置camera,比如设置图像方向。
- 通过设置Camera.PreviewCallback来取得预览数据。
import java.io.IOException;
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.SurfaceTexture;
import android.hardware.Camera;
import android.util.Log;
import android.view.TextureView;
/**
* camera预览视图
* Created by on 2020/2/26.
*/
public class CameraPreview extends TextureView implements TextureView.SurfaceTextureListener {
private static final String TAG = "CameraPreview";
private Camera mCamera;
public CameraPreview(Context context) {
super(context);
}
public CameraPreview(Context context, Camera camera) {
super(context);
mCamera = camera;
}
public void setCamera(Camera camera) {
this.mCamera = camera;
}
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
Log.d(TAG, "TextureView onSurfaceTextureAvailable");
if (this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) {
mCamera.setDisplayOrientation(90);
} else {
mCamera.setDisplayOrientation(0);
}
try {
mCamera.setPreviewCallback(mCameraPreviewCallback);
mCamera.setPreviewTexture(surface); // 使用SurfaceTexture
mCamera.startPreview();
} catch (IOException ioe) {
// Something bad happened
}
}
@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
Log.d(TAG, "TextureView onSurfaceTextureSizeChanged"); // Ignored, Camera does all the work for us
}
@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
Log.d(TAG, "TextureView onSurfaceTextureDestroyed");
mCamera.stopPreview();
mCamera.release();
return true;
}
@Override
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
}
private Camera.PreviewCallback mCameraPreviewCallback = new Camera.PreviewCallback() {
@Override
public void onPreviewFrame(byte[] data, Camera camera) {
Log.d(TAG, "onPreviewFrame: data.length=" + data.length);
}
};
}
操作界面TextureAct。获取camera操作权,初始化CameraPreview并添加到布局中。第一次获取camera时在子线程中操作。
在onPause中释放camera,onResume中尝试取回camera控制权。这样应用暂时退回后台时,其他应用可以操作摄像头
public class TextureAct extends AppCompatActivity {
private static final String TAG = "TextureAct";
private Camera mCamera;
private CameraPreview mPreview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_texture);
new InitCameraThread().start();
}
@Override
protected void onResume() {
if (null == mCamera) {
if (safeCameraOpen()) {
mPreview.setCamera(mCamera); // 重新获取camera操作权
} else {
Log.e(TAG, "无法操作camera");
}
}
super.onResume();
}
@Override
protected void onPause() {
super.onPause();
releaseCamera();
}
@Override
protected void onDestroy() {
super.onDestroy();
releaseCamera();
}
private boolean safeCameraOpen() {
boolean qOpened = false;
try {
releaseCamera();
mCamera = Camera.open();
qOpened = (mCamera != null);
} catch (Exception e) {
Log.e(TAG, "打开相机失败");
e.printStackTrace();
}
return qOpened;
}
private void releaseCamera() {
if (mCamera != null) {
mCamera.setPreviewCallback(null);
mCamera.release(); // release the camera for other applications
mCamera = null;
}
}
private class InitCameraThread extends Thread {
@Override
public void run() {
super.run();
if (safeCameraOpen()) {
Log.d(TAG, "TextureAct 开启摄像头");
runOnUiThread(new Runnable() {
@Override
public void run() {
mPreview = new CameraPreview(TextureAct.this, mCamera);
mPreview.setSurfaceTextureListener(mPreview);
FrameLayout preview = findViewById(R.id.camera_preview);
preview.addView(mPreview);
}
});
}
}
}
}
- Textureview必须在硬件加速开启的窗口中使用。android:hardwareAccelerated=“true” 默认的这个属性就是true,无需再设置。
- 每接到一帧数据,就会调用一次onSurfaceTextureUpdated()。通过这个接口。能够将上来的SurfaceTexture送给OpenGL再去处理。