首页 > 解决方案 > 录制时是否可以获取视频的缩略图

问题描述

我正在使用自定义相机录制 40 秒的视频,但我想在录制时每 10 秒获取一次视频的缩略图。

我也使用了PreviewCallback,但由于解锁相机,它在录制时不起作用。

我还尝试了在切换相机时无法使用的TextureView 。在切换的时候,我再次调用了surfaceCreated的
surfaceCreated(SurfaceHolder holder) 方法,如下:-

 public void surfaceCreated(SurfaceHolder holder) {
    CustomCameraUtils.releaseCamera(camera);
    camera = Camera.open(currentCameraId);
    try {
        CustomCameraUtils.setUpCamera(camera, getWindowManager().getDefaultDisplay().getRotation(), currentCameraId);
        camera.setPreviewDisplay(holder);
        camera.setPreviewCallback(preview);
        camera.startPreview();
        previewRunning = true;
    } catch (Exception e) {
        e.printStackTrace();
        CustomCameraUtils.releaseCamera(camera);
        camera = null;


    }

}

CustomCameraUtils.setUpCamera(camera, getWindowManager().getDefaultDisplay().getRotation(), currentCameraId);

public static void setUpCamera(Camera c, int rotation, int currentCameraId) {
    Camera.CameraInfo info = new Camera.CameraInfo();
    Camera.getCameraInfo(currentCameraId, info);

    int degree = 0;
    switch (rotation) {
        case Surface.ROTATION_0:
            degree = 0;
            break;
        case Surface.ROTATION_90:
            degree = 90;
            break;
        case Surface.ROTATION_180:
            degree = 180;
            break;
        case Surface.ROTATION_270:
            degree = 270;
            break;
        default:
            break;
    }

    if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
        // frontFacing
        rotation = (info.orientation + degree) % 330;
        rotation = (360 - rotation) % 360;
    } else {
        // Back-facing
        rotation = (info.orientation - degree + 360) % 360;
    }
    c.setDisplayOrientation(rotation);
    Camera.Parameters params = c.getParameters();
    List<String> focusModes = params.getSupportedFlashModes();
    if (focusModes != null) {
        if (focusModes
                .contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE)) {
            params.setFlashMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
        }
    }

    params.setRotation(rotation);
}

CustomCameraUtils.releaseCamera(相机)

public static void releaseCamera(Camera camera) {
    try {
        if (camera != null) {
            camera.setPreviewCallback(null);
            camera.setErrorCallback(null);
            camera.stopPreview();
            camera.release();
            camera = null;
        }
    } catch (Exception e) {
        e.printStackTrace();
        camera = null;
    }
}

标签: android

解决方案


试试下面的例子: https ://github.com/googlesamples/android-Camera2Video

openCamera(mTextureView.getWidth(), mTextureView.getHeight());

private void openCamera(int width, int height) {
        if (!hasPermissionsGranted(VIDEO_PERMISSIONS)) {
            requestVideoPermissions();
            return;
        }
        final Activity activity = getActivity();
        if (null == activity || activity.isFinishing()) {
            return;
        }
        CameraManager manager = (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE);
        try {
            Log.d(TAG, "tryAcquire");
            if (!mCameraOpenCloseLock.tryAcquire(2500, TimeUnit.MILLISECONDS)) {
                throw new RuntimeException("Time out waiting to lock camera opening.");
            }
            String cameraId = manager.getCameraIdList()[0];

            // Choose the sizes for camera preview and video recording
            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("Cannot get available preview/video sizes");
            }
            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) {
            Toast.makeText(activity, "Cannot access the camera.", Toast.LENGTH_SHORT).show();
            activity.finish();
        } catch (NullPointerException e) {
            // Currently an NPE is thrown when the Camera2API is used but not supported on the
            // device this code runs.
            ErrorDialog.newInstance(getString(R.string.camera_error))
                    .show(getChildFragmentManager(), FRAGMENT_DIALOG);
        } catch (InterruptedException e) {
            throw new RuntimeException("Interrupted while trying to lock camera opening.");
        }
    }

所以你可以从mTextureView使用中获取位图mTextureView.getBitmap()

从 TextureView 中检索位图。 https://developer.android.com/reference/android/view/TextureView.html#getBitmap()


推荐阅读