合并camera2代码
parent
8cf7934629
commit
13a9f9458f
@ -1,3 +1,7 @@
|
|||||||
<resources>
|
<resources>
|
||||||
<string name="app_name">欣影视频流</string>
|
<string name="app_name">欣影视频流</string>
|
||||||
|
<string name="title_activity_fullscreen">FullscreenActivity</string>
|
||||||
|
<string name="dummy_button">Dummy Button</string>
|
||||||
|
<string name="dummy_content">DUMMY\nCONTENT</string>
|
||||||
|
<string name="title_activity_fullscreen2">FullscreenActivity2</string>
|
||||||
</resources>
|
</resources>
|
||||||
|
@ -0,0 +1,10 @@
|
|||||||
|
<resources>
|
||||||
|
|
||||||
|
<style name="AppTheme.Fullscreen" parent="AppTheme">
|
||||||
|
<item name="android:actionBarStyle">@style/Widget.AppTheme.ActionBar.Fullscreen</item>
|
||||||
|
<item name="android:windowActionBarOverlay">true</item>
|
||||||
|
<item name="android:windowBackground">@null</item>
|
||||||
|
</style>
|
||||||
|
|
||||||
|
|
||||||
|
</resources>
|
@ -0,0 +1,63 @@
|
|||||||
|
package net.ossrs.yasea;
|
||||||
|
|
||||||
|
import android.util.Range;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
//摄像头对象类
|
||||||
|
public class CameraItemData {
|
||||||
|
public String id;//摄像头编号
|
||||||
|
public int sensorOrientation;//摄像头朝向
|
||||||
|
public Range<Integer>[] FPS_RANGES;//FPS
|
||||||
|
public Long MAX_FRAME_DURATION;//最大帧率
|
||||||
|
public int LENS_FACING;//前置后置
|
||||||
|
public List supportedSizes;//支持处理的分辨率
|
||||||
|
|
||||||
|
public List getSupportedSizes() {
|
||||||
|
return supportedSizes;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSupportedSizes(List supportedSizes) {
|
||||||
|
this.supportedSizes = supportedSizes;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getLENS_FACING() {
|
||||||
|
return LENS_FACING;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLENS_FACING(int LENS_FACING) {
|
||||||
|
this.LENS_FACING = LENS_FACING;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getMAX_FRAME_DURATION() {
|
||||||
|
return MAX_FRAME_DURATION;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMAX_FRAME_DURATION(Long MAX_FRAME_DURATION) {
|
||||||
|
this.MAX_FRAME_DURATION = MAX_FRAME_DURATION;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Range<Integer>[] getFPS_RANGES() {
|
||||||
|
return FPS_RANGES;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setFPS_RANGES(Range<Integer>[] FPS_RANGES) {
|
||||||
|
this.FPS_RANGES = FPS_RANGES;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getSensorOrientation() {
|
||||||
|
return sensorOrientation;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSensorOrientation(int sensorOrientation) {
|
||||||
|
this.sensorOrientation = sensorOrientation;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(String id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,258 @@
|
|||||||
|
package net.ossrs.yasea;
|
||||||
|
|
||||||
|
import android.Manifest;
|
||||||
|
import android.app.Activity;
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.pm.PackageManager;
|
||||||
|
import android.content.res.Configuration;
|
||||||
|
import android.hardware.camera2.CameraAccessException;
|
||||||
|
import android.hardware.camera2.CameraCaptureSession;
|
||||||
|
import android.hardware.camera2.CameraCharacteristics;
|
||||||
|
import android.hardware.camera2.CameraDevice;
|
||||||
|
import android.hardware.camera2.CameraManager;
|
||||||
|
import android.hardware.camera2.CaptureRequest;
|
||||||
|
import android.support.v4.app.ActivityCompat;
|
||||||
|
import android.support.v4.content.ContextCompat;
|
||||||
|
import android.util.AttributeSet;
|
||||||
|
import android.util.Log;
|
||||||
|
import android.view.Surface;
|
||||||
|
import android.view.SurfaceHolder;
|
||||||
|
import android.view.SurfaceView;
|
||||||
|
import android.view.ViewGroup;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 本类的主要功能是 : 开启摄像头
|
||||||
|
* <p>
|
||||||
|
* 1,初始化,{@link CameraView#initCamera()}
|
||||||
|
* 2,权限,{@link CameraView#onRequestPermissionsResult(int, String[], int[])}
|
||||||
|
* 3,生命周期,{@link CameraView#onPause()}
|
||||||
|
*
|
||||||
|
* @author koko 2023/10/12 11:38
|
||||||
|
*/
|
||||||
|
public class CameraView extends SurfaceView {
|
||||||
|
private static final String TAG = "CameraView";
|
||||||
|
private static final int CAMERA_PERMISSION_REQUEST_CODE = 22;
|
||||||
|
private CameraDevice mCameraDevice;
|
||||||
|
private CameraCaptureSession mCaptureSession;
|
||||||
|
private CaptureRequest.Builder mPreviewBuilder;
|
||||||
|
public CameraView(Context context) {
|
||||||
|
super(context);
|
||||||
|
initView();
|
||||||
|
}
|
||||||
|
public CameraView(Context context, AttributeSet attrs) {
|
||||||
|
super(context, attrs);
|
||||||
|
initView();
|
||||||
|
}
|
||||||
|
public CameraView(Context context, AttributeSet attrs, int defStyleAttr) {
|
||||||
|
super(context, attrs, defStyleAttr);
|
||||||
|
initView();
|
||||||
|
}
|
||||||
|
public CameraView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
|
||||||
|
super(context, attrs, defStyleAttr, defStyleRes);
|
||||||
|
initView();
|
||||||
|
}
|
||||||
|
private void initView() {
|
||||||
|
SurfaceHolder surfaceHolder = getHolder();
|
||||||
|
surfaceHolder.addCallback(new SurfaceHolder.Callback() {
|
||||||
|
@Override
|
||||||
|
public void surfaceCreated(SurfaceHolder holder) {
|
||||||
|
Log.e(TAG, "surfaceCreated: ");
|
||||||
|
// 初始化相机
|
||||||
|
// 在需要使用相机的地方进行权限检查
|
||||||
|
if (ContextCompat.checkSelfPermission(getContext(), Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
|
||||||
|
// 如果权限未被授予,则请求相机权限
|
||||||
|
ActivityCompat.requestPermissions((Activity) getContext(),
|
||||||
|
new String[]{Manifest.permission.CAMERA},
|
||||||
|
CAMERA_PERMISSION_REQUEST_CODE);
|
||||||
|
} else {
|
||||||
|
// 如果权限已被授予,则直接打开相机
|
||||||
|
openCamera();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
|
||||||
|
Log.e(TAG, "surfaceChanged: ");
|
||||||
|
// 更新相机预览尺寸
|
||||||
|
updatePreview();
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public void surfaceDestroyed(SurfaceHolder holder) {
|
||||||
|
Log.e(TAG, "surfaceDestroyed: ");
|
||||||
|
// 释放相机资源
|
||||||
|
closeCamera();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* 初始化相机
|
||||||
|
*/
|
||||||
|
public void initCamera() {
|
||||||
|
changeCameraOri(getResources().getConfiguration().orientation);
|
||||||
|
}
|
||||||
|
// 处理权限请求的回调
|
||||||
|
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
|
||||||
|
if (requestCode == CAMERA_PERMISSION_REQUEST_CODE) {
|
||||||
|
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
||||||
|
// 相机权限已被授予,打开相机
|
||||||
|
openCamera();
|
||||||
|
} else {
|
||||||
|
// 相机权限被拒绝,显示一个提示消息或执行其他操作
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* 暂停
|
||||||
|
*/
|
||||||
|
protected void onPause() {
|
||||||
|
closeCamera();
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
protected void onConfigurationChanged(Configuration newConfig) {
|
||||||
|
super.onConfigurationChanged(newConfig);
|
||||||
|
changeCameraOri(newConfig.orientation);
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* 横竖屏处理
|
||||||
|
* @param orientation 横竖屏
|
||||||
|
*/
|
||||||
|
private void changeCameraOri(int orientation) {
|
||||||
|
float ratioScreen = 0.28f;
|
||||||
|
float ratioCamera = 16 / 9f;
|
||||||
|
//以窄边为标准
|
||||||
|
//竖屏
|
||||||
|
// int screenWidthPortrait = (int) (com.smart.camera2.PhoneHelper.getScreenWidthReal(getContext()) * ratioScreen);
|
||||||
|
int screenWidthPortrait = (int) 1080;
|
||||||
|
int screenHeightPortrait = (int) (screenWidthPortrait * ratioCamera);
|
||||||
|
//横屏
|
||||||
|
// int screenHeightLandScape = (int) (com.smart.camera2.PhoneHelper.getScreenHeightReal(getContext()) * ratioScreen);
|
||||||
|
int screenHeightLandScape = (int) 1920;
|
||||||
|
int screenWidthLandScape = (int) (screenHeightLandScape * ratioCamera);
|
||||||
|
ViewGroup.LayoutParams layoutParams = getLayoutParams();
|
||||||
|
if (layoutParams == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
switch (orientation) {
|
||||||
|
case Configuration.ORIENTATION_PORTRAIT:
|
||||||
|
Log.i(TAG, "onConfigurationChanged: " + "竖屏");
|
||||||
|
// 竖屏1080-1920
|
||||||
|
layoutParams.width = screenWidthPortrait;
|
||||||
|
layoutParams.height = screenHeightPortrait;
|
||||||
|
break;
|
||||||
|
case Configuration.ORIENTATION_LANDSCAPE:
|
||||||
|
Log.i(TAG, "onConfigurationChanged: " + "横屏");
|
||||||
|
// 横屏1920-1080
|
||||||
|
layoutParams.width = screenWidthLandScape;
|
||||||
|
layoutParams.height = screenHeightLandScape;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
setLayoutParams(layoutParams);
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* 打开相机
|
||||||
|
*/
|
||||||
|
private void openCamera() {
|
||||||
|
CameraManager cameraManager = (CameraManager) getContext().getSystemService(Context.CAMERA_SERVICE);
|
||||||
|
getHolder().setFixedSize(9999, 9999);
|
||||||
|
try {
|
||||||
|
String cameraId = getFrontCameraId(cameraManager);
|
||||||
|
if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
cameraManager.openCamera(cameraId, new CameraDevice.StateCallback() {
|
||||||
|
@Override
|
||||||
|
public void onOpened(CameraDevice camera) {
|
||||||
|
mCameraDevice = camera;
|
||||||
|
Log.e(TAG, "onOpened: ");
|
||||||
|
createCaptureSession();
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public void onDisconnected(CameraDevice camera) {
|
||||||
|
Log.e(TAG, "onDisconnected: ");
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public void onError(CameraDevice camera, int error) {
|
||||||
|
Log.e(TAG, "onError: ");
|
||||||
|
}
|
||||||
|
}, null);
|
||||||
|
} catch (CameraAccessException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* 关闭相机
|
||||||
|
*/
|
||||||
|
private void closeCamera() {
|
||||||
|
Log.e(TAG, "closeCamera: ");
|
||||||
|
if (mCaptureSession != null) {
|
||||||
|
mCaptureSession.close();
|
||||||
|
mCaptureSession = null;
|
||||||
|
}
|
||||||
|
if (mCameraDevice != null) {
|
||||||
|
mCameraDevice.close();
|
||||||
|
mCameraDevice = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* 刷新
|
||||||
|
*/
|
||||||
|
private void updatePreview() {
|
||||||
|
if (mCameraDevice == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (mCaptureSession == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
mCaptureSession.setRepeatingRequest(mPreviewBuilder.build(), null, null);
|
||||||
|
} catch (CameraAccessException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* 获取相机
|
||||||
|
* @param cameraManager cameraManager
|
||||||
|
* @return 相机id
|
||||||
|
* @throws CameraAccessException 异常
|
||||||
|
*/
|
||||||
|
private String getFrontCameraId(CameraManager cameraManager) throws CameraAccessException {
|
||||||
|
String[] cameraIds = cameraManager.getCameraIdList();
|
||||||
|
for (String cameraId : cameraIds) {
|
||||||
|
CameraCharacteristics characteristics = cameraManager.getCameraCharacteristics(cameraId);
|
||||||
|
Integer facing = characteristics.get(CameraCharacteristics.LENS_FACING);
|
||||||
|
if (facing != null && facing == CameraCharacteristics.LENS_FACING_FRONT) {
|
||||||
|
return cameraId;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return cameraIds[0];
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* 创建CaptureSession
|
||||||
|
*/
|
||||||
|
private void createCaptureSession() {
|
||||||
|
try {
|
||||||
|
SurfaceHolder surfaceHolder = getHolder();
|
||||||
|
surfaceHolder.setFixedSize(getWidth(), getHeight());
|
||||||
|
List<Surface> surfaces = new ArrayList<>();
|
||||||
|
surfaces.add(surfaceHolder.getSurface());
|
||||||
|
mPreviewBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
|
||||||
|
mPreviewBuilder.addTarget(surfaceHolder.getSurface());
|
||||||
|
mCameraDevice.createCaptureSession(surfaces, new CameraCaptureSession.StateCallback() {
|
||||||
|
@Override
|
||||||
|
public void onConfigured(CameraCaptureSession session) {
|
||||||
|
mCaptureSession = session;
|
||||||
|
Log.e(TAG, "onConfigured: ");
|
||||||
|
updatePreview();
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public void onConfigureFailed(CameraCaptureSession session) {
|
||||||
|
// 配置会话失败
|
||||||
|
}
|
||||||
|
}, null);
|
||||||
|
} catch (CameraAccessException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue