录制时是否可以获取视频的缩略图

时间:2019-07-16 07:24:54

标签: android

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

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

我还尝试了 TextureView ,该功能在切换相机时不起作用。切换时,我再次调用surfaceCreated
surfaceCreated(SurfaceHolder持有人)方法如下:-

 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(camera)

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;
    }
}

1 个答案:

答案 0 :(得分:1)

尝试以下示例: 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.");
        }
    }

因此,您可以使用mTextureViewmTextureView.getBitmap()获取位图

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