如何在Camera2 api上设置曝光度

1 人关注

我想设置相机的曝光。当相机启动时,我想设置较高的值,当它停止时,它被设置为较低的值。所以我使用了下面的代码。在模拟器上,它显示的范围是-9到9,但当我连接物理usb相机时,它的低范围和高范围显示为0值。我正试图获得 曝光时间范围,它也显示为空。Range exposure_time= cameraCharacteristics.get(CameraCharacteristics.SENSOR_INFO_EXPOSURE_TIME_RANGE)。

 public  void setExposure(Context context, double exposureAdjustment)  {
        CameraManager manager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
        try {
            camId = manager.getCameraIdList()[0];
        } catch (CameraAccessException e) {
            e.printStackTrace();
        try {
            cameraCharacteristics = manager.getCameraCharacteristics(camId);
        } catch (CameraAccessException e) {
            e.printStackTrace();
        Range<Integer> range1 = cameraCharacteristics.get(CameraCharacteristics.CONTROL_AE_COMPENSATION_RANGE);
        Log.d(TAG,"range1" +range1);
        Integer minExposure = range1.getLower();
        Log.d(TAG,"minExposure" +minExposure);
        Integer maxExposure = range1.getUpper();
        Log.d(TAG,"maxExposure" +maxExposure);
        if (minExposure != 0 || maxExposure != 0) {
            float newCalculatedValue = 0;
            if (exposureAdjustment >= 0) {
                newCalculatedValue = (float) (maxExposure * exposureAdjustment);
            } else {
                newCalculatedValue = (float) (minExposure  * exposureAdjustment);
            if (requestBuilder != null) {
                CaptureRequest captureRequest = requestBuilder.build();
                try {
                    captureSession.setRepeatingRequest(captureRequest, captureCallback, null);
                } catch (CameraAccessException e) {
                    e.printStackTrace();
                requestBuilder.set(CaptureRequest.CONTROL_AE_EXPOSURE_COMPENSATION, (int) newCalculatedValue);
                Log.d(TAG,"New Calculated VAlue "+newCalculatedValue);
                try {
                    captureSession.capture(captureRequest,captureCallback,null);
                } catch (CameraAccessException e) {
                    e.printStackTrace();
    
android
android-camera2
gopssays
gopssays
发布于 2022-04-21
2 个回答
shubham sharma
shubham sharma
发布于 2022-11-17
已采纳
0 人赞同

首先,你需要找出可用的自动曝光模式。 你可以通过......来做到这一点。

final int[] availableAeModes = characteristics.get(CameraCharacteristics.CONTROL_AE_AVAILABLE_MODES);
    for(int mode : availableAeModes){
      Timber.d("AE mode : %d",mode);

这些整数值的含义你可以映射到here. 你只能手动控制曝光,如果在可用的AE模式中,数值为control_ae_mode_off is present.

否则,你将无法控制曝光。

Now, I am assuming that the control_ae_mode_off is an available mode on your camera.

你可以通过操纵这两个参数来控制曝光(也有一些其他参数,你可以通过它们来控制曝光,但是,这两个参数对我来说非常有效)。

  • SENSOR_EXPOSURE_TIME
  • SENSOR_SENSITIVITY
  • 关于SENSOR_SENSITIVITY的设置,请通过......检查你的相机所支持的范围。

    final Range<Integer>  isoRange = 
    characteristics.get(CameraCharacteristics.SENSOR_INFO_SENSITIVITY_RANGE);
        if(null != isoRange) {
          Timber.d("iso range => lower : %d, higher : %d",isoRange.getLower(),isoRange.getUpper());
        } else {
          Timber.d("iso range => NULL NOT SUPPORTED");
    

    对于设置SENSOR_EXPOSURE_TIME,请通过以下方式检查你的相机支持的范围。

       final Range<Long>  exposureTimeRange = characteristics.get(CameraCharacteristics.SENSOR_INFO_EXPOSURE_TIME_RANGE);
        if(null!=exposureTimeRange){
          Timber.d("exposure time range => lower : %d, higher : %d",exposureTimeRange.getLower(),exposureTimeRange.getUpper());
        }else{
          Timber.d("exposure time range => NULL NOT SUPPORTED");
    

    现在,你有了曝光时间和灵敏度的范围。 下一步是用这些值来配置预览。

    This is how you configure your preview :

    final CaptureRequest.Builder previewRequest =
    //it's important to set the manual template, because you want to change exposure manually
         this.cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_MANUAL);
          previewRequest.addTarget(this.previewReader.getSurface());
          previewRequest.set(JPEG_ORIENTATION, 0);
    //setting ae mode to off state
          previewRequest.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_OFF);
    //we don't want to handle white balance manually, so set the white balance mode to auto
          previewRequest.set(CaptureRequest.CONTROL_AWB_MODE,
                  CaptureRequest.CONTROL_AWB_MODE_AUTO);
    //setting sensor sensitivity
            previewRequest.set(CaptureRequest.SENSOR_SENSITIVITY, <YOUR_SELECTED_SENSITIVITY>);
    //setting sensor exposure time
    previewRequest.set(CaptureRequest.SENSOR_EXPOSURE_TIME, <YOUR_SELECTED_EXPOSURE_TIME>);
          this.previewCaptureSession.setRepeatingRequest(previewRequest.build(),
                  null, this.cameraHandler);
    

    注:你可以看到,我使用的模板是TEMPLATE_MANUAL。一旦,模板被设置为手动。所有三个自动程序,即自动曝光、自动白平衡和自动对焦,都将变成手动。 上面的代码,没有考虑设置焦点,因为,我把它用在一个没有自动对焦的相机上。