2015-11-18 122 views
1

如何在Camera2 API上启用前置摄像头。谁都可以帮忙?我有这个Camera2 API代码。这只设置设备的主要相机,我想要启用前后相机上的一个按钮click.What是LENS_FACING_FRONT,我是新的android编程。如何在Camera2 API中启用前置摄像头?

private void setUpCameraOutputs(int width, int height) { 
    Activity activity = getActivity(); 
    CameraManager manager = (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE); 
    try { 
     for (String cameraId : manager.getCameraIdList()) { 
      CameraCharacteristics characteristics 
        = manager.getCameraCharacteristics(cameraId); 

      // We don't use a front facing camera in this sample. 
      Integer facing = characteristics.get(CameraCharacteristics.LENS_FACING); 
      if (facing != null && facing == CameraCharacteristics.LENS_FACING_FRONT) { 
       continue; 
      } 

      StreamConfigurationMap map = characteristics.get(
        CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP); 
      if (map == null) { 
       continue; 
      } 

      // For still image captures, we use the largest available size. 
      Size largest = Collections.max(
        Arrays.asList(map.getOutputSizes(ImageFormat.JPEG)), 
        new CompareSizesByArea()); 
      mImageReader = ImageReader.newInstance(largest.getWidth(), largest.getHeight(), 
        ImageFormat.JPEG, /*maxImages*/2); 
      mImageReader.setOnImageAvailableListener(
        mOnImageAvailableListener, mBackgroundHandler); 

      // Danger, W.R.! Attempting to use too large a preview size could exceed the camera 
      // bus' bandwidth limitation, resulting in gorgeous previews but the storage of 
      // garbage capture data. 
      mPreviewSize = chooseOptimalSize(map.getOutputSizes(SurfaceTexture.class), 
        width, height, largest); 

      // We fit the aspect ratio of TextureView to the size of preview we picked. 
      int orientation = getResources().getConfiguration().orientation; 
      if (orientation == Configuration.ORIENTATION_LANDSCAPE) { 
       mTextureView.setAspectRatio(
         mPreviewSize.getWidth(), mPreviewSize.getHeight()); 
      } else { 
       mTextureView.setAspectRatio(
         mPreviewSize.getHeight(), mPreviewSize.getWidth()); 
      } 

      mCameraId = cameraId; 
      return; 
     } 
    } catch (CameraAccessException e) { 
     e.printStackTrace(); 
    } catch (NullPointerException e) { 
     // Currently an NPE is thrown when the Camera2API is used but not supported on the 
     // device this code runs. 
     ErrorDialog.newInstance(getString(R.string.camera_error)) 
       .show(getChildFragmentManager(), FRAGMENT_DIALOG); 
    } 
} 
+0

也许[此链接](http://stackoverflow.com/questions/26925384)将帮助你 – 0X0nosugar

+0

考虑接受答案或评论下面的答案。 – Christ

+0

在你的上面的代码中替换if(facing!= null && facing == CameraCharacteristics.LENS_FACING_BACK){ continue; }它会带前置摄像头。如果你有一个。 – user1154390

回答

2

我们可以使用CameraManager遍历所有在系统中可用的摄像头,每一个指定cameraId。使用cameraId,我们可以获得指定摄像设备的属性。这些属性由类CameraCharacteristics表示。诸如“是前置摄像头还是后置摄像头”,“支持输出分辨率”等可以在这里查询。在谷歌的Git回购

你可以得到官方的样本应用程序here

This例如发现将展示给你使用Camera2 API

给看看这个article更多关于发动新的棉花糖机前,检查权限它。

相关问题