|
| 1 | +package com.frank.camerafilter.camera; |
| 2 | + |
| 3 | +import android.graphics.SurfaceTexture; |
| 4 | +import android.hardware.Camera; |
| 5 | +import android.hardware.Camera.Parameters; |
| 6 | + |
| 7 | +import java.io.IOException; |
| 8 | +import java.util.List; |
| 9 | + |
| 10 | +/** |
| 11 | + * @author xufulong |
| 12 | + * @date 2022/6/17 5:14 下午 |
| 13 | + * @desc |
| 14 | + */ |
| 15 | +public class CameraManager { |
| 16 | + |
| 17 | + private Camera mCamera; |
| 18 | + private int mCameraId = 0; |
| 19 | + private SurfaceTexture mSurfaceTexture; |
| 20 | + |
| 21 | + public Camera getCamera() { |
| 22 | + return mCamera; |
| 23 | + } |
| 24 | + |
| 25 | + public boolean openCamera() { |
| 26 | + return openCamera(mCameraId); |
| 27 | + } |
| 28 | + |
| 29 | + public boolean openCamera(int cameraId) { |
| 30 | + if (mCamera == null) { |
| 31 | + try { |
| 32 | + mCameraId = cameraId; |
| 33 | + mCamera = Camera.open(cameraId); |
| 34 | + setDefaultParams(); |
| 35 | + return true; |
| 36 | + } catch (RuntimeException e) { |
| 37 | + return false; |
| 38 | + } |
| 39 | + } |
| 40 | + return false; |
| 41 | + } |
| 42 | + |
| 43 | + public void releaseCamera() { |
| 44 | + if (mCamera == null) |
| 45 | + return; |
| 46 | + stopPreview(); |
| 47 | + mCamera.release(); |
| 48 | + mCamera = null; |
| 49 | + } |
| 50 | + |
| 51 | + public void switchCamera() { |
| 52 | + if (mCameraId == Camera.CameraInfo.CAMERA_FACING_BACK) { |
| 53 | + mCameraId = Camera.CameraInfo.CAMERA_FACING_FRONT; |
| 54 | + } else { |
| 55 | + mCameraId = Camera.CameraInfo.CAMERA_FACING_BACK; |
| 56 | + } |
| 57 | + releaseCamera(); |
| 58 | + openCamera(mCameraId); |
| 59 | + startPreview(mSurfaceTexture); |
| 60 | + } |
| 61 | + |
| 62 | + private static Camera.Size getLargePictureSize(Camera camera){ |
| 63 | + if(camera != null){ |
| 64 | + List<Camera.Size> sizes = camera.getParameters().getSupportedPictureSizes(); |
| 65 | + Camera.Size temp = sizes.get(0); |
| 66 | + for(int i = 1;i < sizes.size();i ++){ |
| 67 | + float scale = (float)(sizes.get(i).height) / sizes.get(i).width; |
| 68 | + if(temp.width < sizes.get(i).width && scale < 0.6f && scale > 0.5f) |
| 69 | + temp = sizes.get(i); |
| 70 | + } |
| 71 | + return temp; |
| 72 | + } |
| 73 | + return null; |
| 74 | + } |
| 75 | + |
| 76 | + private static Camera.Size getLargePreviewSize(Camera camera){ |
| 77 | + if(camera != null){ |
| 78 | + List<Camera.Size> sizes = camera.getParameters().getSupportedPreviewSizes(); |
| 79 | + Camera.Size temp = sizes.get(0); |
| 80 | + for(int i = 1;i < sizes.size();i ++){ |
| 81 | + if(temp.width < sizes.get(i).width) |
| 82 | + temp = sizes.get(i); |
| 83 | + } |
| 84 | + return temp; |
| 85 | + } |
| 86 | + return null; |
| 87 | + } |
| 88 | + |
| 89 | + public void setDefaultParams() { |
| 90 | + Parameters parameters = mCamera.getParameters(); |
| 91 | + if (parameters.getSupportedFocusModes().contains( |
| 92 | + Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE)) { |
| 93 | + parameters.setFocusMode(Parameters.FOCUS_MODE_CONTINUOUS_PICTURE); |
| 94 | + } |
| 95 | + Camera.Size previewSize = getLargePreviewSize(mCamera); |
| 96 | + parameters.setPreviewSize(previewSize.width, previewSize.height); |
| 97 | + Camera.Size pictureSize = getLargePictureSize(mCamera); |
| 98 | + parameters.setPictureSize(pictureSize.width, pictureSize.height); |
| 99 | + parameters.setRotation(90); |
| 100 | + mCamera.setParameters(parameters); |
| 101 | + } |
| 102 | + |
| 103 | + public void startPreview(SurfaceTexture surfaceTexture) { |
| 104 | + if (mCamera == null) |
| 105 | + return; |
| 106 | + try { |
| 107 | + mCamera.setPreviewTexture(surfaceTexture); |
| 108 | + mSurfaceTexture = surfaceTexture; |
| 109 | + mCamera.startPreview(); |
| 110 | + } catch (IOException e) { |
| 111 | + e.printStackTrace(); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + public void stopPreview() { |
| 116 | + if (mCamera == null) |
| 117 | + return; |
| 118 | + mCamera.setPreviewCallback(null); |
| 119 | + mCamera.stopPreview(); |
| 120 | + } |
| 121 | + |
| 122 | + public int getOrientation() { |
| 123 | + Camera.CameraInfo cameraInfo = new Camera.CameraInfo(); |
| 124 | + Camera.getCameraInfo(mCameraId, cameraInfo); |
| 125 | + return cameraInfo.orientation; |
| 126 | + } |
| 127 | + |
| 128 | + public Camera.Size getPreviewSize() { |
| 129 | + return mCamera.getParameters().getPreviewSize(); |
| 130 | + } |
| 131 | + |
| 132 | + public boolean isFront() { |
| 133 | + return mCameraId == Camera.CameraInfo.CAMERA_FACING_FRONT; |
| 134 | + } |
| 135 | + |
| 136 | +} |
0 commit comments