use of android.hardware.camera2.CameraCaptureSession in project android_frameworks_base by ResurrectionRemix.
the class CameraTestUtils method configureCameraSession.
/**
* Configure a new camera session with output surfaces and type.
*
* @param camera The CameraDevice to be configured.
* @param outputSurfaces The surface list that used for camera output.
* @param listener The callback CameraDevice will notify when capture results are available.
*/
public static CameraCaptureSession configureCameraSession(CameraDevice camera, List<Surface> outputSurfaces, boolean isHighSpeed, CameraCaptureSession.StateCallback listener, Handler handler) throws CameraAccessException {
BlockingSessionCallback sessionListener = new BlockingSessionCallback(listener);
if (isHighSpeed) {
camera.createConstrainedHighSpeedCaptureSession(outputSurfaces, sessionListener, handler);
} else {
camera.createCaptureSession(outputSurfaces, sessionListener, handler);
}
CameraCaptureSession session = sessionListener.waitAndGetSession(SESSION_CONFIGURE_TIMEOUT_MS);
assertFalse("Camera session should not be a reprocessable session", session.isReprocessable());
String sessionType = isHighSpeed ? "High Speed" : "Normal";
assertTrue("Capture session type must be " + sessionType, isHighSpeed == CameraConstrainedHighSpeedCaptureSession.class.isAssignableFrom(session.getClass()));
return session;
}
use of android.hardware.camera2.CameraCaptureSession in project android_frameworks_base by ResurrectionRemix.
the class CameraCaptureSessionImpl method getDeviceStateCallback.
/**
*
* Create an internal state callback, to be invoked on the mDeviceHandler
*
* <p>It has a few behaviors:
* <ul>
* <li>Convert device state changes into session state changes.
* <li>Keep track of async tasks that the session began (idle, abort).
* </ul>
* </p>
* */
@Override
public CameraDeviceImpl.StateCallbackKK getDeviceStateCallback() {
final CameraCaptureSession session = this;
return new CameraDeviceImpl.StateCallbackKK() {
private boolean mBusy = false;
private boolean mActive = false;
@Override
public void onOpened(CameraDevice camera) {
throw new AssertionError("Camera must already be open before creating a session");
}
@Override
public void onDisconnected(CameraDevice camera) {
if (DEBUG)
Log.v(TAG, mIdString + "onDisconnected");
close();
}
@Override
public void onError(CameraDevice camera, int error) {
// Should not be reached, handled by device code
Log.wtf(TAG, mIdString + "Got device error " + error);
}
@Override
public void onActive(CameraDevice camera) {
mIdleDrainer.taskStarted();
mActive = true;
if (DEBUG)
Log.v(TAG, mIdString + "onActive");
mStateCallback.onActive(session);
}
@Override
public void onIdle(CameraDevice camera) {
boolean isAborting;
if (DEBUG)
Log.v(TAG, mIdString + "onIdle");
synchronized (session) {
isAborting = mAborting;
}
/*
* Check which states we transitioned through:
*
* (ACTIVE -> IDLE)
* (BUSY -> IDLE)
*
* Note that this is also legal:
* (ACTIVE -> BUSY -> IDLE)
*
* and mark those tasks as finished
*/
if (mBusy && isAborting) {
mAbortDrainer.taskFinished();
synchronized (session) {
mAborting = false;
}
}
if (mActive) {
mIdleDrainer.taskFinished();
}
mBusy = false;
mActive = false;
mStateCallback.onReady(session);
}
@Override
public void onBusy(CameraDevice camera) {
mBusy = true;
// Don't signal the application since there's no clean mapping here
if (DEBUG)
Log.v(TAG, mIdString + "onBusy");
}
@Override
public void onUnconfigured(CameraDevice camera) {
if (DEBUG)
Log.v(TAG, mIdString + "onUnconfigured");
}
@Override
public void onSurfacePrepared(Surface surface) {
if (DEBUG)
Log.v(TAG, mIdString + "onPrepared");
mStateCallback.onSurfacePrepared(session, surface);
}
};
}
use of android.hardware.camera2.CameraCaptureSession in project android_frameworks_base by crdroidandroid.
the class CameraCaptureSessionImpl method getDeviceStateCallback.
/**
*
* Create an internal state callback, to be invoked on the mDeviceHandler
*
* <p>It has a few behaviors:
* <ul>
* <li>Convert device state changes into session state changes.
* <li>Keep track of async tasks that the session began (idle, abort).
* </ul>
* </p>
* */
@Override
public CameraDeviceImpl.StateCallbackKK getDeviceStateCallback() {
final CameraCaptureSession session = this;
return new CameraDeviceImpl.StateCallbackKK() {
private boolean mBusy = false;
private boolean mActive = false;
@Override
public void onOpened(CameraDevice camera) {
throw new AssertionError("Camera must already be open before creating a session");
}
@Override
public void onDisconnected(CameraDevice camera) {
if (DEBUG)
Log.v(TAG, mIdString + "onDisconnected");
close();
}
@Override
public void onError(CameraDevice camera, int error) {
// Should not be reached, handled by device code
Log.wtf(TAG, mIdString + "Got device error " + error);
}
@Override
public void onActive(CameraDevice camera) {
mIdleDrainer.taskStarted();
mActive = true;
if (DEBUG)
Log.v(TAG, mIdString + "onActive");
mStateCallback.onActive(session);
}
@Override
public void onIdle(CameraDevice camera) {
boolean isAborting;
if (DEBUG)
Log.v(TAG, mIdString + "onIdle");
synchronized (session) {
isAborting = mAborting;
}
/*
* Check which states we transitioned through:
*
* (ACTIVE -> IDLE)
* (BUSY -> IDLE)
*
* Note that this is also legal:
* (ACTIVE -> BUSY -> IDLE)
*
* and mark those tasks as finished
*/
if (mBusy && isAborting) {
mAbortDrainer.taskFinished();
synchronized (session) {
mAborting = false;
}
}
if (mActive) {
mIdleDrainer.taskFinished();
}
mBusy = false;
mActive = false;
mStateCallback.onReady(session);
}
@Override
public void onBusy(CameraDevice camera) {
mBusy = true;
// Don't signal the application since there's no clean mapping here
if (DEBUG)
Log.v(TAG, mIdString + "onBusy");
}
@Override
public void onUnconfigured(CameraDevice camera) {
if (DEBUG)
Log.v(TAG, mIdString + "onUnconfigured");
}
@Override
public void onSurfacePrepared(Surface surface) {
if (DEBUG)
Log.v(TAG, mIdString + "onPrepared");
mStateCallback.onSurfacePrepared(session, surface);
}
};
}
use of android.hardware.camera2.CameraCaptureSession in project android_packages_apps_Snap by LineageOS.
the class PostProcessor method reprocessImage.
private void reprocessImage(Image image, TotalCaptureResult metadata) {
if (mController.isLongShotActive()) {
mController.checkAndPlayShutterSound(mController.getMainCameraId());
}
synchronized (lock) {
if (mCameraDevice == null || mCaptureSession == null || mImageReader == null) {
Log.e(TAG, "Reprocess request is called even before taking picture");
image.close();
return;
}
if (mZSLReprocessImageReader == null) {
image.close();
return;
}
if (DEBUG_ZSL)
Log.d(TAG, "reprocess Image request " + image.getTimestamp());
CaptureRequest.Builder builder = null;
try {
builder = mCameraDevice.createReprocessCaptureRequest(metadata);
builder.set(CaptureRequest.JPEG_ORIENTATION, CameraUtil.getJpegRotation(mController.getMainCameraId(), mController.getDisplayOrientation()));
builder.set(CaptureRequest.JPEG_THUMBNAIL_SIZE, mController.getThumbSize());
builder.set(CaptureRequest.JPEG_THUMBNAIL_QUALITY, (byte) 80);
builder.set(CaptureRequest.COLOR_CORRECTION_ABERRATION_MODE, CaptureRequest.COLOR_CORRECTION_ABERRATION_MODE_HIGH_QUALITY);
builder.set(CaptureRequest.EDGE_MODE, CaptureRequest.EDGE_MODE_HIGH_QUALITY);
builder.set(CaptureRequest.NOISE_REDUCTION_MODE, CaptureRequest.NOISE_REDUCTION_MODE_HIGH_QUALITY);
// CDS 0-OFF, 1-ON, 2-AUTO
VendorTagUtil.setCdsMode(builder, 2);
VendorTagUtil.setJpegCropEnable(builder, (byte) 1);
Rect cropRect = image.getCropRect();
if (cropRect == null || cropRect.isEmpty()) {
cropRect = new Rect(0, 0, image.getWidth(), image.getHeight());
}
int targetWidth = mZSLReprocessImageReader.getWidth();
int targetHeight = mZSLReprocessImageReader.getHeight();
float targetRatio = (float) targetWidth / (float) targetHeight;
cropRect = CameraUtil.getFinalCropRect(cropRect, targetRatio);
// has crop rect. apply to jpeg request
VendorTagUtil.setJpegCropRect(builder, new int[] { cropRect.left, cropRect.top, cropRect.width(), cropRect.height() });
VendorTagUtil.setJpegRoiRect(builder, new int[] { 0, 0, targetWidth, targetHeight });
Location location = mController.getLocationManager().getCurrentLocation();
if (location != null) {
location = new Location(location);
Log.d(TAG, "sendReprocessRequest gps: " + location.toString());
// workaround for Google bug. Need to convert timestamp from ms -> sec
location.setTime(location.getTime() / 1000);
builder.set(CaptureRequest.JPEG_GPS_LOCATION, location);
}
builder.addTarget(mZSLReprocessImageReader.getSurface());
try {
mImageWriter.queueInputImage(image);
} catch (IllegalStateException e) {
Log.e(TAG, "Queueing more than it can have");
}
mCaptureSession.capture(builder.build(), new CameraCaptureSession.CaptureCallback() {
@Override
public void onCaptureCompleted(CameraCaptureSession session, CaptureRequest request, TotalCaptureResult result) {
Log.d(TAG, "reprocessImage onCaptureCompleted");
}
@Override
public void onCaptureFailed(CameraCaptureSession session, CaptureRequest request, CaptureFailure result) {
Log.d(TAG, "reprocessImage onCaptureFailed");
}
@Override
public void onCaptureSequenceCompleted(CameraCaptureSession session, int sequenceId, long frameNumber) {
Log.d(TAG, "reprocessImage onCaptureSequenceCompleted");
}
}, mHandler);
} catch (CameraAccessException e) {
}
}
}
use of android.hardware.camera2.CameraCaptureSession in project android_packages_apps_Snap by LineageOS.
the class Camera2GraphView method createSession.
private void createSession(final int id) {
if (mPaused || !mCameraOpened[id])
return;
Log.d(TAG, "createSession " + id);
List<Surface> list = new LinkedList<Surface>();
try {
// We set up a CaptureRequest.Builder with the output Surface.
mPreviewRequestBuilder[id] = getRequestBuilder(id);
mPreviewRequestBuilder[id].setTag(id);
CameraCaptureSession.StateCallback captureSessionCallback = new CameraCaptureSession.StateCallback() {
@Override
public void onConfigured(CameraCaptureSession cameraCaptureSession) {
if (mPaused || null == mCameraDevice[id]) {
return;
}
// When the session is ready, we start displaying the preview.
mCaptureSession[id] = cameraCaptureSession;
if (id == getMainCameraId()) {
mCurrentSession = cameraCaptureSession;
}
initializePreviewConfiguration(id);
setDisplayOrientation();
updateFaceDetection();
try {
if (isBackCamera() && getCameraMode() == DUAL_MODE) {
linkBayerMono(id);
mIsLinked = true;
}
// don't set repeating request for mono
if (id == MONO_ID && !canStartMonoPreview() && getCameraMode() == DUAL_MODE) {
mCaptureSession[id].capture(mPreviewRequestBuilder[id].build(), mCaptureCallback, mCameraHandler);
} else {
mCaptureSession[id].setRepeatingRequest(mPreviewRequestBuilder[id].build(), mCaptureCallback, mCameraHandler);
}
if (isClearSightOn()) {
ClearSightImageProcessor.getInstance().onCaptureSessionConfigured(id == BAYER_ID, cameraCaptureSession);
} else if (mChosenImageFormat == ImageFormat.PRIVATE && id == getMainCameraId()) {
mPostProcessor.onSessionConfigured(mCameraDevice[id], mCaptureSession[id]);
}
} catch (CameraAccessException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
}
}
@Override
public void onConfigureFailed(CameraCaptureSession cameraCaptureSession) {
Log.e(TAG, "cameracapturesession - onConfigureFailed " + id);
if (mActivity.isFinishing()) {
return;
}
new AlertDialog.Builder(mActivity).setTitle("Camera Initialization Failed").setMessage("Closing SnapdragonCamera").setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
closeCamera();
mActivity.finish();
}
}).setCancelable(false).setIcon(android.R.drawable.ic_dialog_alert).show();
}
@Override
public void onClosed(CameraCaptureSession session) {
Log.d(TAG, "cameracapturesession - onClosed");
}
};
waitForPreviewSurfaceReady();
Surface surface = getPreviewSurfaceForSession(id);
if (id == getMainCameraId()) {
mFrameProcessor.setOutputSurface(surface);
}
if (isClearSightOn()) {
mPreviewRequestBuilder[id].addTarget(surface);
list.add(surface);
ClearSightImageProcessor.getInstance().createCaptureSession(id == BAYER_ID, mCameraDevice[id], list, captureSessionCallback);
} else if (id == getMainCameraId()) {
if (mFrameProcessor.isFrameFilterEnabled()) {
mActivity.runOnUiThread(new Runnable() {
public void run() {
mUI.getSurfaceHolder().setFixedSize(mPreviewSize.getHeight(), mPreviewSize.getWidth());
}
});
}
List<Surface> surfaces = mFrameProcessor.getInputSurfaces();
for (Surface surs : surfaces) {
mPreviewRequestBuilder[id].addTarget(surs);
list.add(surs);
}
list.add(mImageReader[id].getSurface());
if (mSaveRaw) {
list.add(mRawImageReader[id].getSurface());
}
if (mChosenImageFormat == ImageFormat.YUV_420_888 || mChosenImageFormat == ImageFormat.PRIVATE) {
if (mPostProcessor.isZSLEnabled()) {
mPreviewRequestBuilder[id].addTarget(mImageReader[id].getSurface());
list.add(mPostProcessor.getZSLReprocessImageReader().getSurface());
if (mSaveRaw) {
mPreviewRequestBuilder[id].addTarget(mRawImageReader[id].getSurface());
}
mCameraDevice[id].createReprocessableCaptureSession(new InputConfiguration(mImageReader[id].getWidth(), mImageReader[id].getHeight(), mImageReader[id].getImageFormat()), list, captureSessionCallback, null);
} else {
mCameraDevice[id].createCaptureSession(list, captureSessionCallback, null);
}
} else {
mCameraDevice[id].createCaptureSession(list, captureSessionCallback, null);
}
} else {
mPreviewRequestBuilder[id].addTarget(surface);
list.add(surface);
list.add(mImageReader[id].getSurface());
// Here, we create a CameraCaptureSession for camera preview.
mCameraDevice[id].createCaptureSession(list, captureSessionCallback, null);
}
} catch (CameraAccessException e) {
}
}
Aggregations