java - 从TextureView中提取要保存的视频文件

标签 java android android-camera2

目前,我正在使用camera2 api 构建一个相机应用程序。我录制视频并将该文件发送到另一个 Activity ,以确保我们可以正确方向并观看视频。然后更改后的视频或图片将保存到设备中。

当我使用静态图像时,它可以工作,因为我可以拉出位图图像,然后像这样重新保存图像:

 public String saveImage() {

    //Getting a new file name and file path
    //Should we delete these images after were done with them?
    File newImageFile = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    File newImageFolder = new File(newImageFile, "camera2VideoImage");
    if (newImageFolder.exists())
    {
        newImageFolder.mkdirs();
    }

    String timestamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String prepend = "Image_" + timestamp + "_";
    File imageFile = null;
    try {
        imageFile = File.createTempFile(prepend, ".jpg", newImageFolder);
    } catch (IOException e) {
        e.printStackTrace();
    }
    String newFileName = imageFile.getAbsolutePath();


    Bitmap bitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap();


    try {
        FileOutputStream fos = new FileOutputStream(newFileName);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    //returning the new file path.
    return newFileName;
}

这很有效,他们可以查看图像并可以选择更改图像的方向,以防图像困惑,然后我们重新保存。

我正在尝试对视频执行相同的操作,但不确定如何从纹理 View 中提取视频,以便我可以使用正确的方向再次保存,因为前置摄像头有时会上下颠倒,具体取决于手机。

录音保存方法:

 public String saveVideo() {

    //Getting a new file name and file path
    //Should we delete these images after were done with them?
    File newVideoFile = getExternalFilesDir(Environment.DIRECTORY_MOVIES);
    File newVideoFolder = new File(newVideoFile, "camera2VideoImage");
    if (newVideoFolder.exists())
    {
        newVideoFolder.mkdirs();
    }

    String timestamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String prepend = "Video_" + timestamp + "_";
    File videoFile = null;
    try {
        videoFile = File.createTempFile(prepend, ".jpg", newVideoFolder);
    } catch (IOException e) {
        e.printStackTrace();
    }
    String newFileName = videoFile.getAbsolutePath();


    textureView.getBitmap();

    FileOutputStream fos = new FileOutputStream(newFileName);

    //returning the new file path.
    return newFileName;
}

如何从纹理 View 中获取校正后的视频然后保存它,就像上面的 Image 方法一样?

最佳答案

这不是推荐的方式; TextureView 上的 getBitmap 成本很高,不太适合 30fps 视频录制。

但是如果你真的想尝试,你需要将位图提供给 MediaRecorder ;您可以使用 MediaRecorder.getSurface() 来实现此目的,然后锁定 Surface Canvas 并将位图绘制到其中。

但是,如果性能较差,或者 MediaRecorder Surface 不接受 RGB 位图,我不会感到惊讶。

通常,您希望将相机 API 直接连接到 MediaRecorder 或 MediaCodec Surface。如果您确实需要在中间编辑帧,那么使用 GPU 通常是性能最高的选项,尽管为此需要编写大量代码。

关于java - 从TextureView中提取要保存的视频文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59953420/

相关文章:

Android Camera2预览偶尔旋转90度

android - 从 Camera2 图像中裁剪出一个矩形

java - 加密xml文件

java - alpha 和 a 的 Maven 版本排序

Java MethodHandle api 似乎产生了不正确的类型

android - 如何解决形状圆形矩形太大而无法在 android TextBox 中呈现为纹理

java - 如何正确实现自动登录接口(interface),Liferay 6.2

android - 将 Titanium 应用程序转换为模块

android - 检查TextView是否已满

android - Camera2 仅适用于某些设备