gpt4 book ai didi

android - 视频在捕捉时水平拉伸(stretch)

转载 作者:太空狗 更新时间:2023-10-29 14:49:11 26 4
gpt4 key购买 nike

我正在尝试录制视频,但是当我开始使用媒体录制器录制它时,它会水平拉伸(stretch)。我尝试应用了很多解决方案,但我的代码还没有任何效果。到目前为止,这是我为解决此问题所做的尝试。谁能提出任何解决方案。谢谢

 public class MyCameraSurfaceView extends SurfaceView implements
SurfaceHolder.Callback {

private SurfaceHolder mHolder;
private Camera mCamera;

public MyCameraSurfaceView(Context context, Camera camera) {
super(context);
mCamera = camera;

// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = getHolder();
mHolder.addCallback(this);
// deprecated setting, but required on Android versions prior to 3.0
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format,
int width, int height) {
// If your preview can change or rotate, take care of those events
// here.
// Make sure to stop the preview before resizing or reformatting it.
System.out.println("inside surfaceChanged");
if (mHolder.getSurface() == null) {
return;
}
try {
mCamera.stopPreview();
} catch (Exception e) {
e.printStackTrace();
}
try {
mCamera.setDisplayOrientation(90);
mCamera.setPreviewDisplay(mHolder);

Camera.Parameters parameters = mCamera.getParameters();
parameters.setPreviewSize(mPreviewSize.width, mPreviewSize.height);
mCamera.setParameters(parameters);

////////////////////////////////////////////////////////////
/*if (mCamera != null) {
mSupportedPreviewSizes = mCamera.getParameters().getSupportedPreviewSizes();
Camera.Parameters parameters = mCamera.getParameters();
parameters.setPreviewSize(mPreviewSize.width, mPreviewSize.height);
mCamera.setParameters(parameters);
}*/

mCamera.startPreview();
} catch (Exception e) {
e.printStackTrace();
}
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
// The Surface has been created, now tell the camera where to draw
// the preview.
System.out.println("inside surfaceCreated");
try {
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();
} catch (Exception e) {
e.printStackTrace();
}
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {

}

public void setCamera(Camera camera) {
// method to set a camera instance
mCamera = camera;
}

public void refreshCamera(Camera camera) {
if (mHolder.getSurface() == null) {
// preview surface does not exist
return;
}
// stop preview before making changes
try {
mCamera.stopPreview();
} catch (Exception e) {
// ignore: tried to stop a non-existent preview
}
// set preview size and make any resize, rotate or
// reformatting changes here
// start preview with new settings
setCamera(camera);
try {
mCamera.setDisplayOrientation(90);
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
} catch (Exception e) {
Log.d(VIEW_LOG_TAG,
"Error starting camera preview: " + e.getMessage());
}
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
System.out.println("inside onMeasure");
final int width = resolveSize(getSuggestedMinimumWidth(), widthMeasureSpec);
final int height = resolveSize(getSuggestedMinimumHeight(), heightMeasureSpec);
setMeasuredDimension(width, height);
if (mSupportedPreviewSizes != null) {
mPreviewSize = getOptimalPreviewSize(mSupportedPreviewSizes, width, height);
}
}
}

private Camera.Size getOptimalPreviewSize(List<Camera.Size> sizes, int w, int h) {
final double ASPECT_TOLERANCE = 0.1;
double targetRatio = (double) h / w;

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;
}


private boolean prepareMediaRecorder() {
try {
myCamera = getCameraInstance();
setCameraDisplayOrientation(VideoRecordingActivity.this, cameraId, myCamera);
myCamera.unlock();
mediaRecorder = new MediaRecorder();
mediaRecorder.setCamera(myCamera);
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
if (cameraId == 1) {
mediaRecorder.setProfile(CamcorderProfile
.get(CamcorderProfile.QUALITY_LOW));
mediaRecorder.setOrientationHint(270);
} else if (cameraId == 0) {
mediaRecorder.setProfile(CamcorderProfile
.get(CamcorderProfile.QUALITY_HIGH));
mediaRecorder.setOrientationHint(orientation);
}

prRecordedFile = new File(Environment.getExternalStorageDirectory()
+ "/VideoApp/Videos/"
+ String.valueOf(System.currentTimeMillis()) + ".mp4");
mediaRecorder.setOutputFile(prRecordedFile.getPath());
// mediaRecorder.setVideoSize(320, 240);

mediaRecorder.setVideoFrameRate(15); // might be auto-determined

mediaRecorder.setMaxDuration(900000); // Set max duration 900
// sec=15min.
mediaRecorder.setMaxFileSize(500000000); // Set max file size
mediaRecorder.setPreviewDisplay(myCameraSurfaceView.getHolder()
.getSurface());

mediaRecorder.prepare();
} catch (IllegalStateException e) {
releaseMediaRecorder();
return false;
} catch (Exception e) {
releaseMediaRecorder();
return false;
}
return true;
}
enter code here

最佳答案

这解决了我的问题,你可以试试这个:

无论您在 surfaceChanged() 方法中设置什么预览大小,都要在 MediaRecorder 类中进行设置。

在您准备 MediaRecorder

时添加此行
mMediaRecorder.setVideoSize(mPreviewSize.width, mPreviewSize.height);

关于android - 视频在捕捉时水平拉伸(stretch),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36856257/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com