捕获时视频水平拉伸

时间:2016-04-26 05:50:00

标签: android video-capture

我正在尝试录制视频,但是当我开始使用媒体录制器录制它时,它会被水平拉伸。我试图应用大量的解决方案但是我的代码还没有用。 这是我迄今为止试图解决此问题的方法。任何人都可以建议任何解决方案。谢谢

 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

1 个答案:

答案 0 :(得分:0)

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

无论您在surfaceChanged()方法中设置预览大小,请将其设置为 MediaRecorder 类。

准备 MediaRecorder

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