Android 中使用 SurfaceTexture 的相机预览示例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11539139/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:58:18  来源:igfitidea点击:

Example of Camera preview using SurfaceTexture in Android

android

提问by AndroDev

I am trying to render camera preview using SurfaceTexture. I read the document but unable to understand how it works.

我正在尝试使用SurfaceTexture渲染相机预览。我阅读了文档,但无法理解它是如何工作的。

Can anyone provide one sample example(very basic one) or link which uses SurfaceTextureto preview camera. I googled this but not found what I am looking for.

任何人都可以提供一个示例示例(非常基本的示例)或使用SurfaceTexture预览相机的链接。我用谷歌搜索了这个,但没有找到我要找的东西。

Thanks in advance.

提前致谢。

回答by Thiago M Rocha

If you want to use the Camera with TextureSurface you can implement SurfaceTextureListenerinterface. You'll have to implement 4 methods:

如果您想将 Camera 与 TextureSurface 一起使用,您可以实现SurfaceTextureListener接口。您必须实现 4 种方法:

1) onSurfaceTextureAvailable- Here you setup your camera

1) onSurfaceTextureAvailable- 在这里你设置你的相机

2)onSurfaceTextureSizeChanged- In your case, the Android's camera will handle this method

2) onSurfaceTextureSizeChanged- 在您的情况下,Android 的相机将处理此方法

3)onSurfaceTextureDestroyed- Here you destroy all camera stuff.

3) onSurfaceTextureDestroyed- 在这里你销毁所有相机的东西。

4) onSurfaceTextureUpdated- Update your texture here when you have something to change!

4) onSurfaceTextureUpdated- 当您需要更改某些内容时,请在此处更新您的纹理!

Check the example below:

检查下面的例子:

    public class MainActivity extends Activity implements SurfaceTextureListener{

    private Camera mCamera;
    private TextureView mTextureView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mTextureView = new TextureView(this);
        mTextureView.setSurfaceTextureListener(this);

        setContentView(mTextureView);
    }

    @Override
    public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
        mCamera = Camera.open();

        Camera.Size previewSize = mCamera.getParameters().getPreviewSize();
        mTextureView.setLayoutParams(new FrameLayout.LayoutParams(
                previewSize.width, previewSize.height, Gravity.CENTER));

        try {
            mCamera.setPreviewTexture(surface);
        } catch (IOException t) {
        }

        mCamera.startPreview();

    }

    @Override
    public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
        // Ignored, the Camera does all the work for us
    }

    @Override
    public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
        mCamera.stopPreview();
        mCamera.release();
        return true;
    }

    @Override
    public void onSurfaceTextureUpdated(SurfaceTexture surface) {
        // Update your view here!
    }
}

Two more things: Don't forget to add the camera permissions in your project's manifest and the SurfaceTextureis available from API 11.

另外两件事:不要忘记在项目的清单中添加相机权限,并且SurfaceTexture可以从 API 11 获得。

回答by Kamal

public class VideoCapture extends Activity implements OnClickListener, SurfaceHolder.Callback {
MediaRecorder recorder;
SurfaceHolder holder;
boolean recording = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

recorder = new MediaRecorder();
initRecorder();

SurfaceView cameraView = new SurfaceView(this);
holder = cameraView.getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

cameraView.setClickable(true);
cameraView.setOnClickListener(this);
}

private void initRecorder() {
recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);

CamcorderProfile cpHigh = CamcorderProfile
        .get(CamcorderProfile.QUALITY_HIGH);
recorder.setProfile(cpHigh);
recorder.setOutputFile("/sdcard/videocapture_example.mp4");
recorder.setMaxDuration(50000); // 50 seconds
recorder.setMaxFileSize(5000000); // Approximately 5 megabytes
}

private void prepareRecorder() {
recorder.setPreviewDisplay(holder.getSurface());

try {
    recorder.prepare();
} catch (IllegalStateException e) {
    e.printStackTrace();
    finish();
} catch (IOException e) {
    e.printStackTrace();
    finish();
}
}

public void onClick(View v) {
if (recording) {
    recorder.stop();
    recording = false;

    // Let's initRecorder so we can record again
    initRecorder();
    prepareRecorder();
} else {
    recording = true;
    recorder.start();
}
}

public void surfaceCreated(SurfaceHolder holder) {
prepareRecorder();
}

public void surfaceChanged(SurfaceHolder holder, int format, int width,
    int height) {
}

public void surfaceDestroyed(SurfaceHolder holder) {
if (recording) {
    recorder.stop();
    recording = false;
}
recorder.release();
finish();
}
}