CrazyOrr / FFmpegRecorder

An Android video recorder using JavaCV and FFmpeg.
200 stars 48 forks source link

Fullscreen camera preview #53

Open Vibinreji opened 6 years ago

Vibinreji commented 6 years ago

I want make preview based on device width and height.look like its not working can anyone help me to do that

CrazyOrr commented 6 years ago

You can make the preview TextureView fullscreen as long as you can find a supported preview size of the camera which has matching width-height ratio, otherwise the preview will be distorted.

Vibinreji commented 6 years ago

can u provide sample code for that?

levon93 commented 5 years ago

You don't need PREFERRED_PREVIEW_WIDTH and PREFERRED_PREVIEW_HEIGHT variables

add below code in onCreate() and use mPreviewWidth and mPreviewHeight where above variables needed.

DisplayMetrics displayMetrics = getResources().getDisplayMetrics();

        //assigning reverse values, because preview sizes are landscape by default
        //noinspection SuspiciousNameCombination
        mPreviewWidth = displayMetrics.heightPixels;
        //noinspection SuspiciousNameCombination
        mPreviewHeight = displayMetrics.widthPixels;

And also chnage TextureView

public class FullScreenTextureView extends TextureView {

    public FullScreenTextureView(Context context) {
        super(context);
    }

    public FullScreenTextureView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public FullScreenTextureView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int measuredWidth = getMeasuredWidth();
        int measuredHeight = getMeasuredHeight();
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
        if (0 == measuredWidth || 0 == measuredHeight) {
            setMeasuredDimension(width, height);
        } else {
            if (width < height * measuredWidth / measuredHeight) {
                setMeasuredDimension(height * measuredWidth / measuredHeight, height);
            } else {
                setMeasuredDimension(width, width * measuredHeight / measuredWidth);
            }
        }
    }

}