Android如何显示前置摄像头(仅适用于镜子)

Posted

技术标签:

【中文标题】Android如何显示前置摄像头(仅适用于镜子)【英文标题】:Android how to show front camera (for mirror only) 【发布时间】:2019-04-27 21:27:18 【问题描述】:

我想在仅用于镜子的布局中显示前置摄像头,我只想要那个,并且在摄像头布局上我可能有几个简单的按钮。 注意:我不想拍照或任何东西。只是想展示镜子的前置摄像头。 有没有最好的方法来实现它。 我曾经尝试过下面的代码,但相机显示错误的旋转类似于横向模式并且图像被拉伸。

    surfaceView.holder.addCallback(object : SurfaceHolder.Callback 
            private var mCamera:Camera? = null
            override fun surfaceDestroyed(holder: SurfaceHolder) 
                mCamera?.stopPreview()
                mCamera?.release()
                mCamera = null
            

            override fun surfaceCreated(holder: SurfaceHolder) 
                mCamera = getCameraInstance()

                try 
                    mCamera?.setPreviewDisplay(holder)
                 catch (exception: IOException) 
                    mCamera?.release()
                    mCamera = null
                

            

            override fun surfaceChanged(holder: SurfaceHolder, format: Int, width: Int,
                                        height: Int) 
                mCamera?.startPreview()
            
        )

fun getCameraInstance(): Camera? 
        var c: Camera? = null
        try 
            c = openFrontFacingCamera()
         catch (e: Exception) 
        

        return c
    

    private fun openFrontFacingCamera(): Camera? 
        var cameraCount = 0
        var cam: Camera? = null
        val cameraInfo = Camera.CameraInfo()
        cameraCount = Camera.getNumberOfCameras()
        for (camIdx in 0 until cameraCount) 
            Camera.getCameraInfo(camIdx, cameraInfo)
            if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) 
                try 
                    cam = Camera.open(camIdx)
                 catch (e: RuntimeException) 
//                    Log.e(FragmentActivity.TAG, "Camera failed to open: " + e.localizedMessage)
                

            
        

        return cam
    

【问题讨论】:

【参考方案1】:

以下代码 sn-p 帮助打开前置摄像头

private Camera openFrontFacingCamera() 
    int cameraCount = 0;
    Camera cam = null;
    Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
    cameraCount = Camera.getNumberOfCameras();
    for (int camIdx = 0; camIdx<cameraCount; camIdx++) 
        Camera.getCameraInfo(camIdx, cameraInfo);
        if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) 
            try 
                cam = Camera.open(camIdx);
             catch (RuntimeException e) 
                Log.e("Your_TAG", "Camera failed to open: " + e.getLocalizedMessage());
            
        
    
    return cam; 

setDisplayOrientation()

方法可用于旋转相机视图。试试下面的代码 sn-p 来管理相机方向。

int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
   int degrees = 0;

   switch (rotation) 
       case Surface.ROTATION_0: degrees = 0; break;
       case Surface.ROTATION_90: degrees = 90; break;
       case Surface.ROTATION_180: degrees = 180; break;
       case Surface.ROTATION_270: degrees = 270; break;
   

   int result;
   if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) 
       result = (info.orientation + degrees) % 360;
       result = (360 - result) % 360;  // compensate the mirror
    else   // back-facing
       result = (info.orientation - degrees + 360) % 360;
   
   camera.setDisplayOrientation(result);

【讨论】:

我已经尝试过代码,但它的工作方式很奇怪,相机以横向模式显示并且图像被拉伸 camera.setDisplayOrientation(90) 可以解决我所有的问题,非常感谢。

以上是关于Android如何显示前置摄像头(仅适用于镜子)的主要内容,如果未能解决你的问题,请参考以下文章

iPad uiimagepickercontroller 仅前置摄像头

在android webrtc中切换相机(后/前)

如何使用HTML5实现利用摄像头拍照上传功能

Android翻盖前置摄像头镜头翻转视频

是否可以在Android中同时使用前置和后置摄像头[重复]

android Camera如何判断当前使用的摄像头是前置还是后置