安卓拍摄视频如何全屏适配?
1,最近遇到个问题,用camera录制的视频,在拍摄预览过程中是全屏的,到文件管理器中播放就不是的了,会比拍摄时看到的宽一些,困扰了很久,不知道怎么解决,恳请大佬指点一下?
2,代码:
public class MainActivity extends AppCompatActivity {
private SurfaceView surfaceview;
private MediaRecorder mr;
private Button bt_start;
private Button bt_stop;
private boolean isRecording = false;
private Camera mCamera;
private int width;
private int height;
private Camera.Size previewSize;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
surfaceview = (SurfaceView) findViewById(R.id.surfaceview);
bt_start = (Button) findViewById(R.id.bt_start);
bt_stop = (Button) findViewById(R.id.bt_stop);
WindowManager wm = (WindowManager) this
.getSystemService(Context.WINDOW_SERVICE);
width = wm.getDefaultDisplay().getWidth();
height = wm.getDefaultDisplay().getHeight();
SurfaceHolder holder = surfaceview.getHolder();
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
holder.setFixedSize(width, height);
surfaceview.setKeepScreenOn(true);
}
public void start(View v){
//实际的权限申请不应该这么写
new RxPermissions(this)
.request(Manifest.permission.CAMERA,
Manifest.permission.CAPTURE_SECURE_VIDEO_OUTPUT,
Manifest.permission.RECORD_AUDIO,
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE)
.subscribe(new Consumer<Boolean>() {
@Override
public void accept(Boolean aBoolean) throws Exception {
if(aBoolean){
Toast.makeText(MainActivity.this,"已授权",Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(MainActivity.this,"未授权",Toast.LENGTH_SHORT).show();
}
}
});
try {
mCamera = Camera.open();
Camera.Parameters parameters = mCamera.getParameters();
mCamera.setDisplayOrientation(90);
mCamera.setPreviewDisplay(surfaceview.getHolder());
mCamera.startPreview();
List<Camera.Size> mSupportedPreviewSizes = parameters.getSupportedPreviewSizes();
if (mSupportedPreviewSizes != null) {
previewSize = getOptimalPreviewSize(mSupportedPreviewSizes,
Math.max(width, height), Math.min(width, height));
}
Camera.Parameters mParams = mCamera.getParameters();
mParams.setPreviewSize(previewSize.width, previewSize.height);
// mCamera.setDisplayOrientation(90);
List<String> focusModes = mParams.getSupportedFocusModes();
if(focusModes.contains("continuous-video")){
mParams.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
}
mCamera.setParameters(mParams);
mCamera.unlock();
mr = new MediaRecorder();
mr.setCamera(mCamera);
mr.reset();
mr.setPreviewDisplay(surfaceview.getHolder().getSurface());
mr.setVideoSource(MediaRecorder.VideoSource.CAMERA);// 视频源
mr.setAudioSource(MediaRecorder.AudioSource.MIC);// 音频源
mr.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);// 视频输出格式
mr.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);// 音频格式
mr.setVideoSize(previewSize.width,previewSize.height);
mr.setVideoFrameRate(30);
mr.setVideoEncodingBitRate(3*1024*1024);// 设置帧频率,然后就清晰了
mr.setOrientationHint(90);// 输出旋转90度,保持竖屏录制
mr.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);// 视频录制格式
File file = new File(Environment.getExternalStorageDirectory(),"zz"+System.currentTimeMillis()+".mp4");
mr.setOutputFile(file.getAbsolutePath());
mr.setOnErrorListener(new MediaRecorder.OnErrorListener() {
@Override
public void onError(MediaRecorder mr, int what, int extra) {
isRecording = false;
mr.stop();
mr.release();
mr = null;
isRecording = false;
bt_start.setEnabled(true);
bt_stop.setEnabled(false);
Toast.makeText(MainActivity.this,"录制出错!",Toast.LENGTH_SHORT).show();
}
});
//设置预览显示
mr.setPreviewDisplay(surfaceview.getHolder().getSurface());
mr.prepare();
mr.start();
bt_start.setEnabled(false);
bt_stop.setEnabled(true);
Toast.makeText(MainActivity.this,"录制开始!",Toast.LENGTH_SHORT).show();
} catch (Exception e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
}
public void stop(View v){
mr.stop();
bt_start.setEnabled(true);
bt_stop.setEnabled(false);
}
@Override
protected void onDestroy() {
//TODO Auto-generated method stub
super.onDestroy();
if(mr!= null){
mr.release();
mr = null;
}
}
public Camera.Size getOptimalPreviewSize(List<Camera.Size> sizes, int w, int h) {
final double ASPECT_TOLERANCE = 0.1;
double targetRatio = (double) w / h;
if (sizes == null) {
return null;
}
Camera.Size optimalSize = null;
double minDiff = Double.MAX_VALUE;
int targetHeight = h;
for (Camera.Size size : sizes) {
double ratio = (double) size.width / size.height;
if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE)
continue;
if (Math.abs(size.height - targetHeight) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
if (optimalSize == null) {
minDiff = Double.MAX_VALUE;
for (Camera.Size size : sizes) {
if (Math.abs(size.height - targetHeight) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
}
return optimalSize;
}
}
3,图片:
拍摄图:
在文件夹中播放的图:
4,会看到实际上的图片会宽一些,应该怎么解决这个问题,想做成全屏的。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也就是预览和实际不相同,你获取屏幕的宽和高的方法可能存在一定的差距,我没记错的话这个数值和实际有所出入。
不过我自己也没尝试过是否因为这个原因,可以参考https://blog.csdn.net/gh86091...
看是否有所帮助
getOptimalPreviewSize 的第一个参数是硬件所支持的尺寸列表,后面长宽是你想要显示的UI尺寸。
所以预览的图片显示是经过修正的,并不是摄像头看到的实际尺寸。
个人想法
方案1.只是用完全匹配的尺寸,即UI尺寸和硬件尺寸相同比例的size (不太好,特别是低档机的硬件支持数少,会很模糊)
方案2.拿到拍摄后的图后修图。不直接用系统相机生成的图,用工具类裁剪后保存。