ffmpeg 水印_Camera2全屏预览,视频录制及使用ffmpeg给本地视频添加水印

先上效果图,本文只摘取部分代码, 完整代码请戳 https://github.com/ycy726619/WaterRecord

c2740c586099b23dfb8e874396891ade.png


1.全屏预览 首先先创建一个RecordCameraView继承于TextureView
public class RecordCameraView extends TextureView {}
构造函数
 public RecordCameraView(Context context) {
            this(context, null); }    public RecordCameraView(Context context, AttributeSet attrs) {
            this(context, attrs, 0);    }    public RecordCameraView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);        initView(context);    }
测量预览宽高,这里如果是width < height * mRatioWidth / mRatioHeight那么预览的就是实际拍摄画面。
@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);    int width = MeasureSpec.getSize(widthMeasureSpec);    int height = MeasureSpec.getSize(heightMeasureSpec);    if (0 == mRatioWidth || 0 == mRatioHeight) {
            setMeasuredDimension(width, height);    } else {
            //全屏预览  width > height * mRatioWidth / mRatioHeight        if (width > height * mRatioWidth / mRatioHeight) {
                setMeasuredDimension(width, width * mRatioHeight / mRatioWidth);        } else {
                setMeasuredDimension(height * mRatioWidth / mRatioHeight, height);        }    }}
预览线程
private void startBackgroundThread() {
        mBackgroundThread = new HandlerThread("CameraBackground");    mBackgroundThread.start();    mBackgroundHandler = new Handler(mBackgroundThread.getLooper());}
打开相机
private void openCamera(int width, int height) {
        CameraManager manager = (CameraManager) getContext().getSystemService(Context.CAMERA_SERVICE);    try {
            Log.e(TAG, "tryAcquire");        if (!mCameraOpenCloseLock.tryAcquire(2500, TimeUnit.MILLISECONDS)) {
                throw new RuntimeException("锁定相机开启时间已过。");        }        String cameraId = manager.getCameraIdList()[0];        CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);        StreamConfigurationMap map = characteristics                .get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);        mSensorOrientation = characteristics.get(CameraCharacteristics.SENSOR_ORIENTATION);        if (map == null) {
                throw new RuntimeException("无法获得可用的预览/视频大小");        }        mVideoSize = chooseVideoSize(map.getOutputSizes(MediaRecorder.class));        mPreviewSize = chooseOptimalSize(map.getOutputSizes(SurfaceTexture.class),                width, height, mVideoSize);        int orientation = getResources().getConfiguration().orientation;        if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
                mTextureView.setAspectRatio(mPreviewSize.getWidth(), mPreviewSize.getHeight());        } else {
                mTextureView.setAspectRatio(mPreviewSize.getHeight(), mPreviewSize.getWidth());        }        configureTransform(width, height);        mMediaRecorder = new MediaRecorder();        manager.openCamera(cameraId, mStateCallback, null);    } catch (CameraAccessException e) {
            Log.e(TAG, "无法进入相机");    } catch (NullPointerException e) {
            Log.e(TAG, "此设备不支持Camera2 API");    } catch (InterruptedException e) {
            throw new RuntimeException("试图锁定相机打开时被打断");    }}
相机状态回调
private CameraDevice.StateCallback mStateCallback = new CameraDevice.StateCallback() {
            @Override        public void onOpened(@NonNull CameraDevice cameraDevice) {
                mCameraDevice = cameraDevice;            startPreview();            mCameraOpenCloseLock.release();            if (null != mTextureView)                configureTransform(mTextureView.getWidth(), mTextureView.getHeight());        }        @Override        public void onDisconnected(@NonNull CameraDevice cameraDevice) {
                mCameraOpenCloseLock.release();            cameraDevice.close();            mCameraDevice = null;        }        @Override        public void onError(@NonNull CameraDevice cameraDevice, in
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值