Skip to content

Commit

Permalink
从SurfaceView更换成TextureView
Browse files Browse the repository at this point in the history
固定布局只能为正方形,否则会产生覆盖层偏移
TODO
1. 代码优化
2. 将旋转角度放到gl脚本中
3. 不再固定bu布局为正方形
  • Loading branch information
k committed Aug 10, 2023
1 parent f2fb307 commit bcdec34
Show file tree
Hide file tree
Showing 5 changed files with 311 additions and 186 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ImageFormat;
import android.graphics.Matrix;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.SurfaceTexture;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraCaptureSession;
Expand All @@ -18,13 +21,15 @@
import android.hardware.camera2.CameraManager;
import android.hardware.camera2.CaptureRequest;
import android.hardware.camera2.params.StreamConfigurationMap;
import android.media.ImageReader;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView;
import android.os.Handler;
import android.os.HandlerThread;
import android.util.AttributeSet;
import android.util.Log;
import android.util.Size;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.ViewGroup;
Expand Down Expand Up @@ -55,8 +60,11 @@ public class Camera2SurfaceView extends SurfaceView {
private CameraManager mCameraManager;
private CameraCaptureSession mCameraCaptureSession;
private CameraDevice mCameraDevice;
private CameraCharacteristics characteristics;
private Handler mHandler;

private int orientation;

private Runnable _runnable = new SurfaceRunnable();
private Runnable _stoppable = new SurfaceStoppable();

Expand Down Expand Up @@ -95,11 +103,35 @@ protected void onAttachedToWindow() {
super.onAttachedToWindow();
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas = getHolder().lockCanvas();
// Matrix matrix = new Matrix();
// RectF viewRect = new RectF(0, 0, screenWidth, screenHeight);
// RectF bufferRect = new RectF(0, 0, previewHeight, previewWidth);
// 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(90 * (Surface.ROTATION_90 - 2), centerX, centerY);
canvas.rotate(90f);
getHolder().unlockCanvasAndPost(canvas);
}

public void init(Bitmap bitmap) {
this.bitmap = bitmap;
init();
}

public void stop() {
if (mCameraDevice != null)
{
mCameraCaptureSession.close();
mCameraDevice.close();
}
}

private void init(){
cameraThread = new HandlerThread("Camera2Thread");
cameraThread.start();
Expand Down Expand Up @@ -188,8 +220,9 @@ private void initCamera2() {
assert mCameraManager != null;
String[] CameraIdList = mCameraManager.getCameraIdList();
mCameraId = CameraIdList[0];
CameraCharacteristics characteristics = mCameraManager.getCameraCharacteristics(mCameraId);
characteristics = mCameraManager.getCameraCharacteristics(mCameraId);
characteristics.get(CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL);
orientation = getContext().getResources().getConfiguration().orientation;
StreamConfigurationMap map = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
if(map != null){
mSizes = map.getOutputSizes(SurfaceTexture.class);
Expand Down Expand Up @@ -232,14 +265,22 @@ public void onError(@NonNull CameraDevice cameraDevice, int i) {
private void takePreview() {
try {
final CaptureRequest.Builder builder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
builder.addTarget(videoRenderer.getSurface());
mCameraDevice.createCaptureSession(Collections.singletonList(videoRenderer.getSurface()), new CameraCaptureSession.StateCallback() {
Surface surface = videoRenderer.getSurface();
// Surface surface = this.getHolder().getSurface();
ImageReader imageReader = ImageReader.newInstance(previewWidth, previewHeight, ImageFormat.JPEG,1);
builder.addTarget(surface);
ArrayList<Surface> surfaceArrayList = new ArrayList<>();
surfaceArrayList.add(surface);
surfaceArrayList.add(imageReader.getSurface());
mCameraDevice.createCaptureSession(surfaceArrayList, new CameraCaptureSession.StateCallback() {
@Override
public void onConfigured(@NonNull CameraCaptureSession cameraCaptureSession) {
if (null == mCameraDevice) return;
mCameraCaptureSession = cameraCaptureSession;
builder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
builder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH);
// builder.set(CaptureRequest.JPEG_ORIENTATION, getJpegOrientation(characteristics, orientation));
builder.set(CaptureRequest.JPEG_ORIENTATION, Surface.ROTATION_180);
CaptureRequest previewRequest = builder.build();
try {
mCameraCaptureSession.setRepeatingRequest(previewRequest, null, mHandler);
Expand Down Expand Up @@ -379,6 +420,24 @@ private void drawNonAffine(float bottomLeftX, float bottomLeftY, float bottomRig
}
}

private int getJpegOrientation(CameraCharacteristics c, int deviceOrientation) {
if (deviceOrientation == android.view.OrientationEventListener.ORIENTATION_UNKNOWN) return 0;
int sensorOrientation = c.get(CameraCharacteristics.SENSOR_ORIENTATION);

// Round device orientation to a multiple of 90
deviceOrientation = (deviceOrientation + 45) / 90 * 90;

// Reverse device orientation for front-facing cameras
boolean facingFront = c.get(CameraCharacteristics.LENS_FACING) == CameraCharacteristics.LENS_FACING_FRONT;
if (facingFront) deviceOrientation = -deviceOrientation;

// Calculate desired JPEG orientation relative to camera orientation to make
// the image upright relative to the device orientation
int jpegOrientation = (sensorOrientation + deviceOrientation + 360) % 360;

return jpegOrientation;
}

private class SurfaceRunnable implements Runnable {
@Override
public void run() {
Expand Down
Loading

0 comments on commit bcdec34

Please sign in to comment.