AndroidStudio:自定义相机预览不会显示
创始人
2024-08-21 14:00:13
0
  1. 确保已经在 Manifest.xml 文件中添加相机权限。

  2. 确保正确设置相机预览视图的大小。

  3. 在 SurfaceView 或 TextureView 上设置 SurfaceHolder 或 SurfaceTexture 作为相机预览的输出目标。

  4. 确保正确实现相机回调,以及在合适的时间调用依赖于相机的方法,比如停止预览或释放相机。

以下是一段基本的代码示例,用于设置相机预览的 SurfaceView:

public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
 
    private Camera camera;

    public CameraPreview(Context context, Camera camera) {
        super(context);
        this.camera = camera;

        // Install a SurfaceHolder.Callback so we get notified when the
        // underlying surface is created and destroyed.
        SurfaceHolder holder = getHolder();
        holder.addCallback(this);
        // deprecated setting, but required on Android versions prior to 3.0
        holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    public void surfaceCreated(SurfaceHolder holder) {
        // The Surface has been created, now tell the camera where to draw the preview.
        try {
            camera.setPreviewDisplay(holder);
            camera.startPreview();
        } catch (IOException e) {
            Log.d("CameraPreview", "Error setting camera preview: " + e.getMessage());
        }
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        // empty. Take care of releasing the Camera preview in your activity.
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
        // If your preview can change or rotate, take care of those events here.
        // Make sure to stop the preview before resizing or reformatting it.
        if (getHolder().getSurface() == null){
          // preview surface does not exist
          return;
        }

        // stop preview before making changes
        try {
            camera.stopPreview();
        } catch (Exception e){
          // ignore: tried to stop a non-existent preview
        }

        // set preview size and make any resize, rotate or

相关内容

热门资讯

科技介绍!蜀渝牌血战到底辅助,... 科技介绍!蜀渝牌血战到底辅助,摸一把跑得快辅助器,详细攻略(有挂分析)1、下载好蜀渝牌血战到底辅助正...
2026版攻略!德普辅助器怎么... 2026版攻略!德普辅助器怎么用,德普之星辅助软件,切实存在有挂(的确有挂)在进入德普之星辅助软件软...
发现玩家!欢乐游戏城攻略,闲逸... 发现玩家!欢乐游戏城攻略,闲逸碰胡金钟罩,详细器(有挂工具)1、闲逸碰胡金钟罩免费脚本咨询教程、闲逸...
第三方插件!德普之星怎么设置埋... 第三方插件!德普之星怎么设置埋牌,德扑之星有辅助软件吗,原来是真的有挂(详细教程)1、超多福利:超高...
9分钟了解!拱趴大菠萝辅助神器... 9分钟了解!拱趴大菠萝辅助神器,雀友会潮汕麻将开挂辅助,详细器(有挂方略)1)雀友会潮汕麻将开挂辅助...
这一现象值得深思!wpk插件辅... 这一现象值得深思!wpk插件辅助,德普之星透视辅助软件下载,原来是有挂(有挂秘诀)小薇(辅助器软件下...
避坑细节!填大坑辅助视频,潮娱... 避坑细节!填大坑辅助视频,潮娱乐鱼虾蟹公式辅助软件,详细教程(发现有挂)亲,关键说明,潮娱乐鱼虾蟹公...
截至目前!wepokerplu... 截至目前!wepokerplus透视脚本免费,德普之星辅助器app,一贯是真的有挂(有挂详细)1、进...
每日必看!微信决胜游戏辅助,蘑... 每日必看!微信决胜游戏辅助,蘑菇云辅助脚本,详细教程(有挂分析)1、微信决胜游戏辅助模拟器是什么优化...
事发当天!约局吧德州有挂吗,德... 事发当天!约局吧德州有挂吗,德扑之星有透视辅助吗,竟然真的有挂(有挂功能)1、德扑之星有透视辅助吗模...