GLSurfaceView 支持与Camera2Api使用,请给出示例代码
时间: 2023-12-10 20:05:06 浏览: 132
以下是一个简单的示例代码,演示如何在GLSurfaceView中使用Camera2API预览摄像头数据:
```
public class CameraPreviewGLSurfaceView extends GLSurfaceView {
private CameraDevice cameraDevice;
private CameraCaptureSession cameraCaptureSession;
private SurfaceTexture surfaceTexture;
private Surface surface;
private int textureId;
public CameraPreviewGLSurfaceView(Context context, AttributeSet attrs) {
super(context, attrs);
setEGLContextClientVersion(2);
setRenderer(new CameraPreviewRenderer());
setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
}
private class CameraPreviewRenderer implements GLSurfaceView.Renderer, CameraDevice.StateCallback {
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
setupCamera();
openCamera();
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
configureTransform(width, height);
}
@Override
public void onDrawFrame(GL10 gl) {
if (surfaceTexture == null) {
return;
}
surfaceTexture.updateTexImage();
GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
GLES20.glUseProgram(/* your shader program */);
/* bind your vertex and texture coordinate buffers */
GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, textureId);
/* draw your geometry */
GLES20.glDrawArrays(/* your draw mode */, 0, /* your vertex count */);
}
@Override
public void onSurfaceDestroyed(GL10 gl) {
closeCamera();
}
@Override
public void onOpened(CameraDevice cameraDevice) {
this.cameraDevice = cameraDevice;
createCameraPreviewSession();
}
@Override
public void onDisconnected(CameraDevice cameraDevice) {
cameraDevice.close();
CameraPreviewGLSurfaceView.this.cameraDevice = null;
}
@Override
public void onError(CameraDevice cameraDevice, int error) {
cameraDevice.close();
CameraPreviewGLSurfaceView.this.cameraDevice = null;
}
private void setupCamera() {
CameraManager cameraManager = (CameraManager) getContext().getSystemService(Context.CAMERA_SERVICE);
try {
String cameraId = cameraManager.getCameraIdList()[0];
CameraCharacteristics characteristics = cameraManager.getCameraCharacteristics(cameraId);
StreamConfigurationMap map = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
Size[] outputSizes = map.getOutputSizes(SurfaceTexture.class);
Size largest = Collections.max(Arrays.asList(outputSizes), new CompareSizesByArea());
surfaceTexture = new SurfaceTexture(textureId);
surfaceTexture.setDefaultBufferSize(largest.getWidth(), largest.getHeight());
surface = new Surface(surfaceTexture);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
private void openCamera() {
CameraManager cameraManager = (CameraManager) getContext().getSystemService(Context.CAMERA_SERVICE);
try {
String cameraId = cameraManager.getCameraIdList()[0];
cameraManager.openCamera(cameraId, this, null);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
private void createCameraPreviewSession() {
try {
cameraDevice.createCaptureSession(Arrays.asList(surface), new CameraCaptureSession.StateCallback() {
@Override
public void onConfigured(CameraCaptureSession session) {
cameraCaptureSession = session;
try {
CaptureRequest.Builder builder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
builder.addTarget(surface);
CaptureRequest captureRequest = builder.build();
cameraCaptureSession.setRepeatingRequest(captureRequest, null, null);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
@Override
public void onConfigureFailed(CameraCaptureSession session) {
cameraCaptureSession = null;
}
}, null);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
private void closeCamera() {
if (cameraCaptureSession != null) {
cameraCaptureSession.close();
cameraCaptureSession = null;
}
if (cameraDevice != null) {
cameraDevice.close();
cameraDevice = null;
}
if (surface != null) {
surface.release();
surface = null;
}
if (surfaceTexture != null) {
surfaceTexture.release();
surfaceTexture = null;
}
}
private void configureTransform(int viewWidth, int viewHeight) {
if (surfaceTexture == null) {
return;
}
Matrix matrix = new Matrix();
RectF viewRect = new RectF(0, 0, viewWidth, viewHeight);
RectF bufferRect = new RectF(0, 0, surfaceTexture.getSurfaceTexture().getBufferSize().getWidth(), surfaceTexture.getSurfaceTexture().getBufferSize().getHeight());
float centerX = viewRect.centerX();
float centerY = viewRect.centerY();
bufferRect.offset(centerX - bufferRect.centerX(), centerY - bufferRect.centerY());
matrix.setRectToRect(viewRect, bufferRect, Matrix.ScaleToFit.FILL);
matrix.postRotate(/* your rotation angle */, centerX, centerY);
surfaceTexture.setTransform(matrix);
}
}
}
```
这个示例代码演示了如何使用Camera2API在GLSurfaceView中预览摄像头数据。其中,setupCamera()方法用于设置摄像头的参数,openCamera()方法用于打开摄像头,createCameraPreviewSession()方法用于创建摄像头预览会话,configureTransform()方法用于配置变换矩阵以适应屏幕旋转。注意,这个示例代码没有包括如何处理摄像头权限请求等必要的步骤,需要在实际应用中加以处理。
阅读全文