user6623665
user6623665

Reputation: 1

How to setVideoSize when getSupportedVideoSizes return null

When i use MediaRecorder to record a video, I want to set the video size.But the Camera getParameters() return the getSupportedVideoSizes is null and the getSupportedPreviewSizes is fine, it return 1920x1088,1280x960,1280x720,960x540,720x540,720x480,640x480,352x288,320x240,176x144. How ever,if i set the value videoSize of MediaRecorder,the screen of binded SurfaceView will be frozen after a few seconds when i start record.So, what should i do?

Upvotes: 0

Views: 659

Answers (2)

user6623665
user6623665

Reputation: 1

Finally I solve the problem, the preview textureview is in the top of my activity,there are some others behind. I set the textureview's width and height in java code,but if i set this in the XML with dp,everything is ok,so, what is the relationship between the preview textureview's (width and height) and the record video's(width and height)

Upvotes: 0

Giacomo Mattiuzzi
Giacomo Mattiuzzi

Reputation: 305

My litte workaround was this:

    public void setVideoSize(int width, int height) {
    // Just set Preview size...
    Camera.Parameters params = getParameters();
    List<Camera.Size> sizes = params.getSupportedPreviewSizes();
    Camera.Size preferred = params.getPreferredPreviewSizeForVideo();
    if (preferred == null) {
        preferred = sizes.get(0);
    }

    Camera.Size optimalPreview = Util.getOptimalPreviewSize(mContext, sizes,
            width, height);
    setPreviewSize(optimalPreview.width, optimalPreview.height);
}

Where Util.getOptimalPreviewSize was:

/**
     * Returns the optimal preview size for photo shots
     *
     * @param currentActivity
     * @param sizes
     * @param widthImage
     * @param heightImage
     * @return
     */
    public static Size getOptimalPreviewSize(Activity currentActivity,
                                             List<Size> sizes, int widthImage, int heightImage) {
        final double ASPECT_TOLERANCE = 0.05;
        double targetRatio = 0;

        if (sizes == null) return null;

        Camera.Size optimalSize = null;
        double minDiff = Double.MAX_VALUE;

        // Recupero le dimensioni dello schermo per confrontare le dimensioni della
        // preview con quelle dello schermo, in modo tale che a video veda esattamente ciò che poi
        // andrò a fotografare
        int widthScreen = currentActivity.getWindow().getDecorView().getWidth();
        int heightScreen = currentActivity.getWindow().getDecorView().getHeight();

        targetRatio = (double) widthImage / heightImage;

        if (Util.getDisplayRotation(currentActivity) == 0 || Util.getDisplayRotation(currentActivity) == 180) {
            int temp = heightScreen;
            heightScreen = widthScreen;
            widthScreen = temp;
        }

        int targetHeight = heightScreen;

        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) {
                if (size.width <= widthScreen && size.height <= heightScreen) {
                    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;
    }

According with the rotation of the camera (NOT THE SCREEN!!) and the dimension of image/video to capture it will found the best preview size of video/image

Upvotes: 1

Related Questions