我正在开发用于摄像机的Android应用程序,我正在为我的应用程序使用新的CameraX Api。这个api确实很容易实现,并且在我的应用中一切正常,但是问题出在我的前置摄像头录制中。如果我打开前置摄像头,它会显示镜像,这对于每个摄像头视图都是正常的,但是如果我使用前置摄像头录制视频,则录制的视频会水平翻转。我如何防止视频翻转?我想像Snapchat或Instagram一样录制视频
private void startCamera() {
CameraX.unbindAll();
Rational aspectRatio = new Rational (textureView.getWidth(), textureView.getHeight());
Size screen = new Size(textureView.getWidth(), textureView.getHeight()); //size of the screen
PreviewConfig pConfig = new PreviewConfig.Builder().setLensFacing(lensFacing).setTargetAspectRatio(aspectRatio).setTargetResolution(screen).build();
final Preview preview = new Preview(pConfig);
preview.setOnPreviewOutputUpdateListener(
new Preview.OnPreviewOutputUpdateListener() {
//to update the surface texture we have to destroy it first then re-add it
@Override
public void onUpdated(Preview.PreviewOutput output){
ViewGroup parent = (ViewGroup) textureView.getParent();
parent.removeView(textureView);
parent.addView(textureView, 0);
textureView.setSurfaceTexture(output.getSurfaceTexture());
updateTransform();
}
});
ImageCaptureConfig imageCaptureConfig = new ImageCaptureConfig.Builder().setLensFacing(lensFacing).setCaptureMode(ImageCapture.CaptureMode.MIN_LATENCY)
.setTargetRotation(getWindowManager().getDefaultDisplay().getRotation()).build();
final ImageCapture imgCap = new ImageCapture(imageCaptureConfig);
VideoCaptureConfig videoCaptureConfig = new VideoCaptureConfig.Builder().setLensFacing(lensFacing)
.setAudioRecordSource(MediaRecorder.AudioSource.MIC)
.setTargetRotation(getWindowManager().getDefaultDisplay().getRotation()).build();
final VideoCapture videoCapture = new VideoCapture(videoCaptureConfig);
findViewById(R.id.vidStart).setOnClickListener(new View.OnClickListener() {
@SuppressLint("RestrictedApi")
@Override
public void onClick(View v) {
File file = new File(Environment.getExternalStorageDirectory() + "/" + System.currentTimeMillis() + ".mp4");
imgCap.takePicture(file, new ImageCapture.OnImageSavedListener() {
@Override
public void onImageSaved(@NonNull File file) {
String msg = "Pic captured at " + file.getAbsolutePath();
Toast.makeText(getBaseContext(), msg,Toast.LENGTH_LONG).show();
}
@Override
public void onError(@NonNull ImageCapture.UseCaseError useCaseError, @NonNull String message, @Nullable Throwable cause) {
String msg = "Pic capture failed : " + message;
Toast.makeText(getBaseContext(), msg,Toast.LENGTH_LONG).show();
if(cause != null){
cause.printStackTrace();
}
}
});
videoCapture.startRecording(file, new VideoCapture.OnVideoSavedListener() {
@Override
public void onVideoSaved(@NonNull File file) {
String msg = "Vid captured at " + file.getAbsolutePath();
Toast.makeText(getBaseContext(), msg,Toast.LENGTH_LONG).show();
}
@Override
public void onError(VideoCapture.UseCaseError useCaseError, String message, @Nullable Throwable cause) {
}
});
}
});
findViewById(R.id.vidStop).setOnClickListener(new View.OnClickListener() {
@SuppressLint("RestrictedApi")
@Override
public void onClick(View v) {
videoCapture.stopRecording();
}
});
//bind to lifecycle:
CameraX.bindToLifecycle(this, preview, videoCapture);
}
答案 0 :(得分:0)
需要翻转输出。在大多数设备中,令人困惑的部分是预览阶段而不是录制阶段。
在使用前置摄像头预览时,类似镜面的预览便于用户使用前置摄像头。
您需要阅读此SO答案以进行深入了解。 how-to-make-video-captured-by-front-camera-not-being-inverse-android