如何在拍摄照片时从Android手机获取AutoExposureCompensation级别(亮度)?
我可以拍照。我可以访问摄像机的参数,包括曝光补偿(当我检查时总是为零),但我需要在拍摄照片时获得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;
}
答案 0 :(得分:3)
我认为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更新 甚至在结果发出之前就已经发生了。如果申请是 在自动和手动控制之间切换,并希望消除 切换过程中出现任何闪烁,建议采用以下步骤:
- 以自动曝光模式开始:
- 锁定AE
- 等待输出具有AE锁定的第一个结果
- 将该结果中的曝光设置复制到请求中,将请求设置为手动AE
- 提交捕获请求,继续根据需要运行手动AE。
醇>
另外,根据AE模式(相同来源)的描述
当设置为任何ON模式时,摄像机选择的值 设备自动曝光例程,用于给定的重写字段 捕获将在其CaptureResult中提供。
因此,一旦您完成第一个CaptureRequest,您可以使用以下回调中的TotalCaptureResult
:
void onCaptureCompleted (CameraCaptureSession session,
CaptureRequest request,
TotalCaptureResult result)
{
int aecompensationlevel = result.get(CaptureResult.CONTROL_AE_EXPOSURE_COMPENSATION)
}
答案 1 :(得分:1)
好的,问题出现了如何在编写代码来控制Android相机时具体设置曝光,传感器灵敏度和其他变量。这只适用于Lollipop或更高版本。要发布很多代码,但我会尝试加入亮点
简而言之,我使用带有CameraManager的TextureView(AutoFitTextureView)。当我打开相机时,我调用了一个名为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;
}
答案 2 :(得分:0)
好的,我自己也会为发现类似问题的人自己回答这个问题。
Manish接近他的回答,但即使在onCaptureSession事件中使用,CONTROL_AE_EXPOSURE_COMPENSATION也只返回0(零),这是没用的;这只是相机的默认起始值。
但是,CameraCaptureSession.CaptureCallback(onCaptureSession事件)可以让您从SENSOR_EXPOSURE_TIME和SENSOR_SENSITIVITY获取值,以创建解决使用Android相机的自动曝光问题的方法。
以下是我使用的代码片段:
private void captureStillPicture() {
try {
...
CameraCaptureSession.CaptureCallback CaptureCallback
= new CameraCaptureSession.CaptureCallback() {
@Override
public void onCaptureCompleted(@NonNull CameraCaptureSession session,
@NonNull CaptureRequest request,
@NonNull TotalCaptureResult result) {
long sensorTime= result.get(CaptureResult.SENSOR_EXPOSURE_TIME);
long sensorSensitivity = result.get(CaptureResult.SENSOR_SENSITIVITY);
int ONE_SECOND = 1000000000; //1 billion nanoseconds
int MICRO_SECOND = 1000000;
int MILLI_SECOND = 1000;
String exposureText = "";
if (sensorTime > ONE_SECOND) {
exposureText = String.format("%.2f s", sensorTime / 1e9);
} else if (sensorTime > MILLI_SECOND) {
exposureText = String.format("%.2f ms", sensorTime / 1e6);
} else if (sensorTime > MICRO_SECOND) {
exposureText = String.format("%.2f us", sensorTime / 1e3);
} else {
exposureText = String.format("%d ns", sensorTime);
}
int aecompensationlevel=result.get(CaptureResult.CONTROL_AE_EXPOSURE_COMPENSATION); //only returns zero
showToast("Saved: " + mFile +" | " +exposureText );
Log.d(TAG, mFile.toString());
}
};
这是我调试器的结果图片: