如何通过Java从Android手机摄像头获得曝光补偿级别?

拍摄照片时,如何从Android手机获取自动曝光补偿级别(亮度)?

我可以拍照。我可以访问相机的参数,包括曝光补偿(检查时始终为零),但我需要在拍摄照片时获得AE补偿级别,而不是在拍摄之前和之后。

背景:我希望在特定时间拍摄的所有照片都使用与拍摄照片相同的AE补偿级别。我不希望Android相机通常对曝光水平或白平衡进行数百次调整。我想得到一次,并设置所有后续照片,相同的设置。

我尝试过对图片,OpenCV,片段等使用“意图”。我似乎无法获得其中任何一个的AE补偿设置。以下是我尝试过的最新代码,从JavaCameraView的扩展版本开始:

import org.opencv.android.JavaCameraView;
import android.content.Context;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.Size;
import android.util.AttributeSet;
import android.util.Log;
@SuppressWarnings("deprecation")
public class NewJavaCameraView extends JavaCameraView implements PictureCallback {

public int getExposureCompensation(){
    return mCamera.getParameters().getExposureCompensation();
}
 @SuppressWarnings("deprecation")
 public void takePicture(final String fileName) {
    Log.i(TAG, "Taking picture");
    this.mPictureFileName = fileName;

    Camera.Parameters params = mCamera.getParameters();
    int exposureComp = params.getExposureCompensation();
    mCamera.setPreviewCallback(null);

    // PictureCallback is implemented by the current class
    int otherexposureComp =this.getExposureCompensation();
    mCamera.takePicture(null, null, this);
}

 @SuppressWarnings("deprecation")
 @Override
 public void onPictureTaken(byte[] data, Camera camera) {

    Camera.Parameters params = mCamera.getParameters();
    int exposureComp = params.getExposureCompensation();
    int otherexposureComp =this.getExposureCompensation();
    mCamera.startPreview();
    mCamera.setPreviewCallback(this);

    // Write the image in a file (in jpeg format)
    try {
        FileOutputStream fos = new FileOutputStream(mPictureFileName);

        fos.write(data);
        fos.close();

    } catch (java.io.IOException e) {
        Log.e("Picture", "photoCallback", e);
    }
}

以下是 Android View 中使用上述类的一些代码:

public class DiscPhoto extends Activity implements CvCameraViewListener2, OnTouchListener {
 private static final String TAG = "OCVSample::Activity";
 private NewJavaCameraView mOpenCvCameraView;
 private List<Size> mResolutionList;

 private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
    @Override
    public void onManagerConnected(int status) {
        switch (status) {
            case LoaderCallbackInterface.SUCCESS:
            {
                Log.i(TAG, "OpenCV loaded successfully");
                mOpenCvCameraView.enableView();
                mOpenCvCameraView.setOnTouchListener(DiscPhoto.this);
            } break;
            default:
            {
                super.onManagerConnected(status);
            } break;
        }
    }
};

public DiscPhoto() {
    Log.i(TAG, "Instantiated new " + this.getClass());
}

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    Log.i(TAG, "called onCreate");
    super.onCreate(savedInstanceState);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

    setContentView(R.layout.activity_disc_photo);

    mOpenCvCameraView = (NewJavaCameraView) findViewById(R.id.discPhotoPage);
    mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);
    mOpenCvCameraView.setCvCameraViewListener(this);
}

@SuppressLint("SimpleDateFormat")
@Override
public boolean onTouch(View v, MotionEvent event) {
    Log.i(TAG,"onTouch event");
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
    String currentDateandTime = sdf.format(new Date());
    String fileName = Environment.getExternalStorageDirectory().getPath() +
            "/sample_picture_" + currentDateandTime + ".jpg";
    mOpenCvCameraView.takePicture(fileName);
    Toast.makeText(this, fileName + " saved", Toast.LENGTH_SHORT).show();
    return false;
}

答案 1

我认为camera2 API(https://developer.android.com/reference/android/hardware/camera2/CaptureRequest.html)将满足您的需求。

资料来源:https://developer.android.com/reference/android/hardware/camera2/CaptureRequest.html#CONTROL_AE_LOCK

由于相机设备具有正在进行的请求管道,因此锁定的设置不一定与从相机设备接收的最新捕获结果中存在的设置相对应,因为甚至在结果发送之前,也可能已经发生了额外的捕获和AE更新。如果应用程序在自动和手动控制之间切换,并希望消除切换过程中的任何闪烁,则建议执行以下过程:

  1. 在自动自动曝光模式下启动:
  2. 锁定自动曝光
  3. 等待输出 AE 锁定的第一个结果
  4. 将该结果中的曝光设置复制到请求中,将请求设置为手动 AE
  5. 提交捕获请求,继续根据需要运行手动 AE。

同样根据AE模式的描述(同一来源)

当设置为任何 ON 模式时,相机设备自动曝光例程为给定捕获的被覆盖字段选择的值将在其 CaptureResult 中可用。

因此,一旦您进行了第一次 CaptureRequest,您就可以使用以下 from 回调:TotalCaptureResult

void onCaptureCompleted (CameraCaptureSession session, 
                CaptureRequest request, 
                TotalCaptureResult result)
{
       int aecompensationlevel = result.get(CaptureResult.CONTROL_AE_EXPOSURE_COMPENSATION)
}

答案 2

好吧,问题出现了如何在编写代码以控制Android相机时具体设置曝光,传感器灵敏度和其他变量。这仅适用于棒棒糖或更高版本。有很多代码要发布,但我会尝试放入亮点

简而言之,我将TextureView(AutoFitTextureView)与CameraManager一起使用。当我打开相机时,我调用一个名为createPreviewSessions()的void函数。

  void openCamera() {
    try {
        mManager.openCamera(mCameraId, new CameraDevice.StateCallback() {
            @Override
            public void onOpened(CameraDevice camera) {
                createPreviewSession();
            }
        }

private void createPreviewSession() {
    try {
        SurfaceTexture texture = mTextureView.getSurfaceTexture();
        assert texture != null;
        texture.setDefaultBufferSize(mPreviewSize.getWidth(), mPreviewSize.getHeight());
        final List<Surface> surfaceList = new ArrayList<>();
        Surface surface = mImageReader.getSurface();
        surfaceList.add(surface);

        mCamera.createCaptureSession(surfaceList, new CameraCaptureSession.StateCallback() {
            @Override
            public void onConfigured(CameraCaptureSession session) {
                mSession = session;

                CaptureRequest request = createRequest(surfaceList, milliSecond, sensorSetting); //module variables
            } ...
       } ...
}
private CaptureRequest createRequest(List<Surface> surfaces, int milliSeconds, int sensorSetting) {
    Log.v("createRequest","here");
    try {
        CaptureRequest.Builder builder = mCamera.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
        builder.set(CaptureRequest.CONTROL_EFFECT_MODE, CameraMetadata.CONTROL_EFFECT_MODE_OFF);
        for (Surface surface : surfaces)
            builder.addTarget(surface);
        int exposureTime = milliSeconds * (milliSecondFactor); //billionth
        CaptureRequestSettings.SetRequestBuilder(builder,CONTROL_AWB_MODE_DAYLIGHT);

        builder.set(CaptureRequest.SENSOR_EXPOSURE_TIME, Long.valueOf(exposureTime));       //set hard values based on settings caught when photo taken
        builder.set(CaptureRequest.SENSOR_SENSITIVITY, Integer.valueOf(sensorSetting));     //same thing
        builder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
        builder.set(CaptureRequest.CONTROL_AWB_MODE, CaptureRequest.CONTROL_AWB_MODE_OFF);  //CaptureRequest.CONTROL_AWB_MODE_OFF); //off here just like video mode
        builder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_OFF);    //off ... don't want auto exposure

        return builder.build();
    } catch (CameraAccessException e) {
        Log.e("CaptureRequest", "CameraAccessException: " +e.getMessage());
    } catch (Exception e) {
        Log.e("CaptureRequest", "Regular Exception: " +e.getMessage());
    }
    Log.v("createRequest","shouldn't get here");
    return null;
}

推荐