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、用户打开应用后不用登录就...
重要通知!福建13水插件,吉安... 重要通知!福建13水插件,吉安中至小程序,详细app(有挂秘诀)1、吉安中至小程序透视辅助软件激活码...
避坑细节!牵手跑得软件,打两圈... 避坑细节!牵手跑得软件,打两圈怀疑有外挂,详细工具(真实有挂)1、完成打两圈怀疑有外挂有辅助插件,帮...
最新研发!钱塘十水三攻略,芒果... 最新研发!钱塘十水三攻略,芒果辅助器安卓版,详细方法(有挂秘诀)该软件可以轻松地帮助玩家将芒果辅助器...
推荐一款!哈糖大菠萝辅助,微信... 推荐一款!哈糖大菠萝辅助,微信中至有辅助吗,详细工具(有挂猫腻)1)微信中至有辅助吗辅助插件:进一步...
重大通报!花花生活圈辅助工具安... 重大通报!花花生活圈辅助工具安装教程,丽水双扣茶苑辅助,详细app(有挂工具)1、花花生活圈辅助工具...
查到实测!798大菠萝辅助,边... 查到实测!798大菠萝辅助,边锋干瞪眼脚本,详细技巧(有挂总结)一、边锋干瞪眼脚本游戏安装教程牌型概...
科技新动态!老友广东插件软件,... 科技新动态!老友广东插件软件,奇迹永久免费脚本辅助工具,详细软件(有挂教学)1、上手简单,内置详细流...
推荐一款!来物局怎么开挂,微信... 推荐一款!来物局怎么开挂,微信辅助接单平台黑幕,详细方法(有挂方式)1、让任何用户在无需微信辅助接单...
一分钟了解!潮汕激k脚本,天酷... 一分钟了解!潮汕激k脚本,天酷辅助巡查系统,详细app(有挂分享)1、操作简单,无需天酷辅助巡查系统...