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

相关内容

热门资讯

正版透视器玲珑透视器多功能AI... 正版透视器玲珑透视器多功能AI教程,玲珑总是真的有透视器教程1、实时玲珑透视辅助更新:用户可以随时随...
正版开挂天天摸麻将开挂多功能A... 正版开挂天天摸麻将开挂多功能AI工具,天天摸麻将果然是有开挂工具在进入天天摸麻将软件靠谱后,参与本局...
正版外挂星聚汇外挂多功能AI脚... 正版外挂星聚汇外挂多功能AI脚本,星聚汇确实有外挂脚本1、完成星聚汇有辅助插件,帮助玩家取得所有比赛...
正版开挂盛世开挂多功能AI软件... 正版开挂盛世开挂多功能AI软件,盛世一直真的有开挂软件盛世辅助器是一种具有地方特色的麻将游戏,要想赢...
正版外挂团团竞技外挂多功能AI... 正版外挂团团竞技外挂多功能AI工具,团团竞技确实是真的有外挂工具1、许多玩家不知道团团竞技辅助怎么退...
正版外挂趣玩娱乐外挂多功能AI... 正版外挂趣玩娱乐外挂多功能AI攻略,趣玩娱乐好像真的是有外挂攻略1、每一步都需要思考,不同水平的挑战...
正版辅助哥哥打大A辅助多功能A... 您好,哥哥打大A这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很多玩家在这...
正版外挂胡乐安庆麻将外挂多功能... 正版外挂胡乐安庆麻将外挂多功能AI攻略,胡乐安庆麻将一贯真的有外挂攻略1、金币登录送、破产送、升级送...
正版开挂川渝互娱开挂多功能AI... 正版开挂川渝互娱开挂多功能AI攻略,川渝互娱竟然是有开挂攻略1、进入到川渝互娱是否有挂之后,能看到左...
正版辅助器兴动互娱辅助器多功能... 正版辅助器兴动互娱辅助器多功能AI插件,兴动互娱竟然存在有辅助器插件1、在兴动互娱插件功能辅助器技巧...