Я пытаюсь создать действие, в котором я могу захватывать видео со своих встроенных камер с помощью Camera2 API.
Я создал метод switchcameramethod, и я пытаюсь переключаться между камерами, что означает, что если задание использует заднюю камеру для показа предварительного просмотра, то при нажатии на кнопку задание должно использовать переднюю камеру для показаПредварительный просмотр, но когда я нажимаю кнопку, ничего не происходит.
Ниже приведены коды, которые я использую. Этот код показывает предварительный просмотр с задней камеры.
public class videopostintent extends AppCompatActivity {
private ImageButton switchcamera;
private static final int REQUEST_CAMERA_PERMISSION_RESULT = 0;
private TextureView mTextureview;
private TextureView.SurfaceTextureListener mSurfaceTextureListener = new TextureView.SurfaceTextureListener() {
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
setupCamera(width,height);
connectcamera();
}
@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
}
@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
return false;
}
@Override
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
}
};
private CameraDevice mCameraDevice;
private CameraDevice.StateCallback mCameraDeviceStateCallBack = new CameraDevice.StateCallback() {
@Override
public void onOpened(@NonNull CameraDevice camera) {
mCameraDevice = camera;
startpreview();
//Toast.makeText(getApplicationContext(),"Now we got connection to the camera",Toast.LENGTH_SHORT).show();
}
@Override
public void onDisconnected(@NonNull CameraDevice camera) {
camera.close();
mCameraDevice = null;
}
@Override
public void onError(@NonNull CameraDevice camera, int error) {
camera.close();
mCameraDevice = null;
}
};
private HandlerThread mbackgroundhandlerthread;
private Handler mbackgroundhandler;
private int mTotalRotation;
private Size mPreviewSize;
private CaptureRequest.Builder mCapturerequestbuilder;
private static SparseIntArray ORIENTATIONS = new SparseIntArray();
static {
ORIENTATIONS.append(Surface.ROTATION_0, 0);
ORIENTATIONS.append(Surface.ROTATION_90, 90);
ORIENTATIONS.append(Surface.ROTATION_180, 180);
ORIENTATIONS.append(Surface.ROTATION_270, 270);
}
private String mcameraId;
private CameraCharacteristics cameraCharacteristics;
private static class CompareSizeByArea implements Comparator<Size> {
@Override
public int compare(Size lhs, Size rhs) {
return Long.signum( (long)(lhs.getWidth() * lhs.getHeight()) -
(long)(rhs.getWidth() * rhs.getHeight()));
}
}
public static final String CAMERA_FRONT = "1";
public static final String CAMERA_BACK = "0";
private String cameraId = CAMERA_FRONT;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_videopostintent);
mTextureview = (TextureView) findViewById(R.id.textureView);
switchcamera = (ImageButton) findViewById(R.id.cameraswitchbutton);
switchcamera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mcameraId == CAMERA_BACK){
cameraId = CAMERA_FRONT;
switchcameramethod(cameraId);
}else{
cameraId = CAMERA_BACK;
switchcameramethod(cameraId);
}
}
});
}
private void switchcameramethod(String cameraId) {
CameraManager cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED){
cameraManager.openCamera(cameraId,mCameraDeviceStateCallBack,mbackgroundhandler);
}else{
if (shouldShowRequestPermissionRationale(Manifest.permission.CAMERA)){
Toast.makeText(this,"App required permission",Toast.LENGTH_LONG).show();
}
requestPermissions(new String[] {android.Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO
}, REQUEST_CAMERA_PERMISSION_RESULT);
}
}else {
cameraManager.openCamera(mcameraId, mCameraDeviceStateCallBack, mbackgroundhandler);
}
}catch (Exception e){
}
}
@Override
protected void onResume() {
super.onResume();
startbackgroundthread();
if(mTextureview.isAvailable()){
setupCamera(mTextureview.getWidth(),mTextureview.getHeight());
connectcamera();
}else{
mTextureview.setSurfaceTextureListener(mSurfaceTextureListener);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_CAMERA_PERMISSION_RESULT){
if(grantResults[0] != PackageManager.PERMISSION_GRANTED){
Toast.makeText(getApplicationContext(),"Applicateion can not run camera servies untill permission is granted",Toast.LENGTH_LONG).show();
}
}
}
@Override
protected void onPause() {
super.onPause();
closeCamera();
stopbackgroungthreade();
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
View decaview = getWindow().getDecorView();
if (hasFocus){
decaview.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
}
}
private void setupCamera(int width, int height) {
CameraManager cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
try {
for(String cameraId : cameraManager.getCameraIdList()){
CameraCharacteristics cameraCharacteristics = cameraManager.getCameraCharacteristics(cameraId);
if(cameraCharacteristics.get(CameraCharacteristics.LENS_FACING) ==
CameraCharacteristics.LENS_FACING_FRONT){
continue;
}
StreamConfigurationMap map = cameraCharacteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
int deviceOrientation = getWindowManager().getDefaultDisplay().getRotation();
mTotalRotation = sensorToDeviceRotation(cameraCharacteristics, deviceOrientation);
boolean swapRotation = mTotalRotation == 90 || mTotalRotation == 270;
int rotatedWidth = width;
int rotatedHeight = height;
mPreviewSize = chooseOptimalSize(map.getOutputSizes(SurfaceTexture.class), rotatedWidth, rotatedHeight);
//Toast.makeText(getApplicationContext(),mPreviewSize.toString(),Toast.LENGTH_SHORT).show();
if(swapRotation) {
rotatedWidth = height;
rotatedHeight = width;
}
mcameraId = cameraId;
return;
}
} catch (CameraAccessException e) {
e.printStackTrace();
}
//Toast.makeText(getApplicationContext(),"Camera back",Toast.LENGTH_SHORT).show();
}
private void connectcamera(){
CameraManager cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED){
cameraManager.openCamera(mcameraId,mCameraDeviceStateCallBack,mbackgroundhandler);
}else{
if (shouldShowRequestPermissionRationale(Manifest.permission.CAMERA)){
Toast.makeText(this,"App required permission",Toast.LENGTH_LONG).show();
}
requestPermissions(new String[] {android.Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO
}, REQUEST_CAMERA_PERMISSION_RESULT);
}
}else {
cameraManager.openCamera(mcameraId, mCameraDeviceStateCallBack, mbackgroundhandler);
}
}catch (Exception e){
}
}
private void startpreview(){
SurfaceTexture surfaceTexture = mTextureview.getSurfaceTexture();
surfaceTexture.setDefaultBufferSize(mPreviewSize.getWidth(), mPreviewSize.getHeight());
Surface previewSurface = new Surface(surfaceTexture);
try {
mCapturerequestbuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
mCapturerequestbuilder.addTarget(previewSurface);
mCameraDevice.createCaptureSession(Arrays.asList(previewSurface),
new CameraCaptureSession.StateCallback() {
@Override
public void onConfigured(CameraCaptureSession session) {
try {
session.setRepeatingRequest(mCapturerequestbuilder.build(),
null, mbackgroundhandler);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
@Override
public void onConfigureFailed(CameraCaptureSession session) {
Log.d("Alpha", "onConfigureFailed: startPreview");
}
}, null);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
private void closeCamera(){
if(mCameraDevice != null){
mCameraDevice.close();
mCameraDevice = null;
}
}
private void startbackgroundthread(){
mbackgroundhandlerthread = new HandlerThread("camera2videoImage");
mbackgroundhandlerthread.start();
mbackgroundhandler = new Handler(mbackgroundhandlerthread.getLooper());
}
private void stopbackgroungthreade(){
mbackgroundhandlerthread.quitSafely();
try {
mbackgroundhandlerthread.join();
mbackgroundhandlerthread = null;
mbackgroundhandler = null;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private static int sensorToDeviceRotation(CameraCharacteristics cameraCharacteristics, int deviceOrientation) {
int sensorOrienatation = cameraCharacteristics.get(CameraCharacteristics.SENSOR_ORIENTATION);
deviceOrientation = ORIENTATIONS.get(deviceOrientation);
return (sensorOrienatation + deviceOrientation + 360) % 360;
}
private static Size chooseOptimalSize(Size[] choices, int width, int height) {
List<Size> bigEnough = new ArrayList<Size>();
for(Size option : choices) {
if(option.getHeight() == option.getWidth() * height / width &&
option.getWidth() >= width && option.getHeight() >= height) {
bigEnough.add(option);
}
}
if(bigEnough.size() > 0) {
return Collections.min(bigEnough, new CompareSizeByArea());
} else {
return choices[0];
}
}
}