Skip to content

Video_Crop

zanxiaofei edited this page Mar 22, 2018 · 2 revisions

短视频画布裁剪功能说明

SDK提供以下接口来完成视频裁剪的相关功能:

  1. KSYEditKit#setScalingMode(int):裁剪模式选择(KSYEditKit#SCALING_MODE_BEST_FIT、KSYEditKit#SCALING_MODE_CROP、KSYEditKit#SCALING_MODE_FULL_FILL(默认))

SCALING_MODE_FULL_FILL 为默认模式,默认会按照原始视频比例来计算分辨率

  1. KSYEditKit#setScaleType(int type):画布比例选择(KSYEditKit#SCALE_TYPE_9_16(默认)、KSYEditKit#SCALE_TYPE_3_4、 KSYEditKit#SCALE_TYPE_1_1)
  2. KSYEditKit#setPreviewCrop(float left, float top, float w, float h):SCALING_MODE_CROP模式时设置画布裁剪范围
  3. KSYEditKit#getPreviewCropRect():SCALING_MODE_CROP模式时获取画布裁剪信息
  4. KSYEditKit#setScale(float):任意画布比例
  5. KSYEditKit#getCropScale():SCALING_MODE_CROP模式时获取可裁剪范围

填充模式

  1. 设置视频模式为填充模式
//默认为KSYEditKit#SCALING_MODE_FULL_FILL模式
mEditKit.setScalingMode(KSYEditKit#SCALING_MODE_BEST_FIT);

/**
     * set crop mode
     *
     * @param mode
     * @see KSYEditKit#SCALING_MODE_BEST_FIT  填充模式
     * @see KSYEditKit#SCALING_MODE_CROP   裁剪模式
     * @see KSYEditKit#SCALING_MODE_FULL_FILL  默认   
     */
    public void setScalingMode(int mode)
  1. 设置画布比例
//默认为屏幕比例(SCALE_TYPE_9_16)
mEditKit.setScalingMode(KSYEditKit#SCALE_TYPE_3_4);

 /**
     * set crop type
     *
     * @param type
     * @see KSYEditKit#SCALE_TYPE_9_16
     * @see KSYEditKit#SCALE_TYPE_3_4
     * @see KSYEditKit#SCALE_TYPE_1_1
     */
    public void setScaleType(int type)
  1. 根据画布比例更新画布
 //默认全屏显示预览
        int previewWidth = mScreenWidth;
        int previewHeight = mScreenHeight;

        //根据不同比例来更新预览区域大小
        //to 3:4
        if (mScaleType == KSYEditKit.SCALE_TYPE_3_4) {
            previewHeight = previewWidth * 4 / 3;
        }
        //to 1:1
        if (mScaleType == KSYEditKit.SCALE_TYPE_1_1) {
            previewHeight = previewWidth;
        }

        RelativeLayout.LayoutParams previewParams = (RelativeLayout.LayoutParams) mPreviewLayout
                .getLayoutParams();
        previewParams.height = previewHeight;
        previewParams.width = previewWidth;

        mPreviewLayout.setLayoutParams(previewParams);

裁剪模式

  1. 设置视频模式为裁剪模式
//默认为KSYEditKit#SCALING_MODE_FULL_FILL模式
mEditKit.setScalingMode(KSYEditKit#SCALING_MODE_CROP);
  1. 设置画布比例
//默认为屏幕比例(SCALE_TYPE_9_16)
mEditKit.setScalingMode(KSYEditKit#SCALE_TYPE_3_4);
  1. 根据画布比例更新画布
 //默认全屏显示预览
        int previewWidth = mScreenWidth;
        int previewHeight = mScreenHeight;

        //根据不同比例来更新预览区域大小
        //to 3:4
        if (mScaleType == KSYEditKit.SCALE_TYPE_3_4) {
            previewHeight = previewWidth * 4 / 3;
        }
        //to 1:1
        if (mScaleType == KSYEditKit.SCALE_TYPE_1_1) {
            previewHeight = previewWidth;
        }

        RelativeLayout.LayoutParams previewParams = (RelativeLayout.LayoutParams) mPreviewLayout
                .getLayoutParams();
        previewParams.height = previewHeight;
        previewParams.width = previewWidth;

        mPreviewLayout.setLayoutParams(previewParams);
  1. 裁剪画布
        //裁剪模式下,并且有多余的区域需要裁剪才进行裁剪,否则不进行裁剪
        if (mEditKit.getCropScale() <= 0 || mScaleMode != KSYEditKit.SCALING_MODE_CROP) {
            return;
        }
        RelativeLayout.LayoutParams previewParams = (RelativeLayout.LayoutParams) mPreviewLayout
                .getLayoutParams();
        int previewHeight = previewParams.height;
        int previewWidth = previewParams.width;
        //只有裁剪模式才需要更新预览的显示区域

        if (!mEditKit.getIsLandscape()) {
            //竖屏模式,上下裁剪,
            //更新窗口位置参数
            float newY = (mLastRawY - mPreviewTouchStartY);
            mMaxCrop = previewParams.topMargin;
            mMinCrop = 0.f - mEditKit.getCropScale() * (float) previewHeight + previewParams
                    .topMargin;
            //不能超出可裁剪范围
            if (newY > mMaxCrop) {
                newY = mMaxCrop;
            }

            if (newY < mMinCrop) {
                newY = mMinCrop;
            }

            float top = newY / previewHeight;
            //更新裁剪区域更新预览显示  
            mEditKit.setPreviewCrop(0.f, 0.f - top, 1.f, 0.f);
        } else {
            //横屏模式左右裁剪
            float newX = (mLastRawX - mPreviewTouchStartX);
            mMaxCrop = 0.f;
            mMinCrop = 0.f - mEditKit.getCropScale() * (float) previewWidth + previewParams
                    .leftMargin;
            //不能超出可裁剪范围
            if (newX > mMaxCrop) {
                newX = mMaxCrop;
            }

            if (newX < mMinCrop) {
                newX = mMinCrop;
            }

            float left = newX / previewWidth;
            //根据裁剪范围,跟新预览的显示区域  
            mEditKit.setPreviewCrop(0.f - left, 0.f, 0.f, 1.f);
        }

========================= interface begin=================================
 /**
     * crop preview
     *
     * @param left The percentage of the video clipping to the left
     * @param top  The percentage of the video clipping to the top
     */
    public void setPreviewCrop(float left, float top, float w, float h)
==========================interface end ================================
Clone this wiki locally