use of android.hardware.camera2.CameraCaptureSession in project android_frameworks_base by crdroidandroid.
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 MazeSolver by Thukor.
the class Camera2 method takePicture.
protected void takePicture() throws IOException {
if (null == cameraDevice) {
Log.e(TAG, "cameraDevice is null");
return;
}
CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
try {
CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraDevice.getId());
Size[] jpegSizes = null;
if (characteristics != null) {
jpegSizes = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP).getOutputSizes(ImageFormat.JPEG);
}
int width = 480;
int height = 480;
if (jpegSizes != null && 0 < jpegSizes.length) {
width = jpegSizes[0].getWidth();
height = jpegSizes[0].getHeight();
}
ImageReader reader = ImageReader.newInstance(width, height, ImageFormat.JPEG, 1);
List<Surface> outputSurfaces = new ArrayList<Surface>(2);
outputSurfaces.add(reader.getSurface());
outputSurfaces.add(new Surface(textureView.getSurfaceTexture()));
final CaptureRequest.Builder captureBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);
captureBuilder.addTarget(reader.getSurface());
captureBuilder.set(CaptureRequest.CONTROL_MODE, CameraMetadata.CONTROL_MODE_AUTO);
// Orientation
int rotation = getWindowManager().getDefaultDisplay().getRotation();
captureBuilder.set(CaptureRequest.JPEG_ORIENTATION, ORIENTATIONS.get(rotation));
// final File file = new File(Environment.getExternalStorageDirectory()+"/MazeSolver/nude.jpg");
final File file = createImageFileName();
lastFile = file;
ImageReader.OnImageAvailableListener readerListener = new ImageReader.OnImageAvailableListener() {
@Override
public void onImageAvailable(ImageReader reader) {
Image image = null;
try {
image = reader.acquireLatestImage();
ByteBuffer buffer = image.getPlanes()[0].getBuffer();
byte[] bytes = new byte[buffer.capacity()];
buffer.get(bytes);
save(bytes);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (image != null) {
image.close();
}
}
}
private void save(byte[] bytes) throws IOException {
OutputStream output = null;
try {
output = new FileOutputStream(file);
output.write(bytes);
} finally {
if (null != output) {
output.close();
}
}
}
};
reader.setOnImageAvailableListener(readerListener, mBackgroundHandler);
final CameraCaptureSession.CaptureCallback captureListener = new CameraCaptureSession.CaptureCallback() {
@Override
public void onCaptureCompleted(CameraCaptureSession session, CaptureRequest request, TotalCaptureResult result) {
super.onCaptureCompleted(session, request, result);
Toast.makeText(Camera2.this, "Saved:" + file, Toast.LENGTH_SHORT).show();
createCameraPreview();
}
};
cameraDevice.createCaptureSession(outputSurfaces, new CameraCaptureSession.StateCallback() {
@Override
public void onConfigured(CameraCaptureSession session) {
try {
session.capture(captureBuilder.build(), captureListener, mBackgroundHandler);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
@Override
public void onConfigureFailed(CameraCaptureSession session) {
}
}, mBackgroundHandler);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
use of android.hardware.camera2.CameraCaptureSession in project habpanelviewer by vbier.
the class CameraImplV2 method startPreview.
@Override
public void startPreview(SurfaceTexture surface, IPreviewListener previewListener) {
if (mPreviewRunning) {
previewListener.started();
return;
}
if (mCamera != null) {
Log.v(TAG, "trying to start preview...");
try {
CameraCharacteristics characteristics = mCamManager.getCameraCharacteristics(mCamera.getId());
StreamConfigurationMap map = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
if (map == null) {
Log.w(TAG, "Could not find a valid preview size");
}
final Point previewSize = chooseOptimalSize(toPointArray(map.getOutputSizes(ImageFormat.YUV_420_888)));
Log.v(TAG, "preview image size is " + previewSize.x + "x" + previewSize.y);
mActivity.runOnUiThread(() -> configureTransform(previewSize, mActivity.getWindowManager().getDefaultDisplay().getRotation()));
mImageReader = ImageReader.newInstance(previewSize.x, previewSize.y, ImageFormat.YUV_420_888, 2);
mImageReader.setOnImageAvailableListener(reader -> {
if (!mPreviewRunning) {
mPreviewRunning = true;
previewListener.started();
}
Image i = reader.acquireLatestImage();
try {
LumaData ld = null;
for (ILumaListener l : mListeners) {
if (l.needsPreview()) {
if (ld == null && i != null) {
Log.v(TAG, "preview image available and needed: size " + i.getWidth() + "x" + i.getHeight());
ByteBuffer luma = i.getPlanes()[0].getBuffer();
final byte[] data = new byte[luma.capacity()];
luma.get(data);
ld = new LumaData(data, i.getWidth(), i.getHeight());
}
l.preview(ld);
}
}
} finally {
if (i != null) {
i.close();
}
}
}, mPreviewHandler);
CaptureRequest.Builder mPreviewRequestBuilder = mCamera.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
mPreviewRequestBuilder.addTarget(mImageReader.getSurface());
Surface previewSurface = new Surface(surface);
mPreviewRequestBuilder.addTarget(previewSurface);
ArrayList<Surface> surfaces = new ArrayList<>();
surfaces.add(mImageReader.getSurface());
surfaces.add(previewSurface);
mCamera.createCaptureSession(surfaces, new CameraCaptureSession.StateCallback() {
@Override
public void onConfigured(@NonNull CameraCaptureSession cameraCaptureSession) {
Log.d(TAG, "Capture session configured");
// The mCamera is already closed
if (null == mCamera) {
previewListener.error("Capture session has no camera");
return;
}
mCaptureSession = cameraCaptureSession;
try {
// Auto focus should be continuous for mCamera preview.
mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
// Finally, we start displaying the mCamera preview.
cameraCaptureSession.setRepeatingRequest(mPreviewRequestBuilder.build(), null, null);
} catch (CameraAccessException e) {
previewListener.exception(e);
previewListener.error("Could not create preview request");
}
}
@Override
public void onClosed(@NonNull CameraCaptureSession session) {
super.onClosed(session);
mPreviewRunning = false;
}
@Override
public void onConfigureFailed(@NonNull CameraCaptureSession cameraCaptureSession) {
previewListener.error("Could not create capture session");
}
}, mPreviewHandler);
} catch (CameraAccessException e) {
previewListener.exception(e);
previewListener.error("Could not create preview");
}
} else {
previewListener.error("Camera not found");
}
}
use of android.hardware.camera2.CameraCaptureSession in project android_packages_apps_Snap by LineageOS.
the class ClearSightImageProcessor method capture.
public void capture(boolean bayer, CameraCaptureSession session, CaptureRequest.Builder requestBuilder, Handler captureCallbackHandler) throws CameraAccessException {
Log.d(TAG, "capture: " + bayer);
final int cam = bayer ? CAM_TYPE_BAYER : CAM_TYPE_MONO;
CameraCaptureSession.CaptureCallback captureCallback = new CameraCaptureSession.CaptureCallback() {
@Override
public void onCaptureCompleted(CameraCaptureSession session, CaptureRequest request, TotalCaptureResult result) {
Log.d(TAG, "capture - onCaptureCompleted: " + cam);
if (isClosing())
Log.d(TAG, "capture - onCaptureCompleted - closing");
else {
mImageProcessHandler.obtainMessage(MSG_NEW_CAPTURE_RESULT, cam, 0, result).sendToTarget();
if (cam == CAM_TYPE_BAYER) {
float lensFocusDistance = result.get(CaptureResult.LENS_FOCUS_DISTANCE);
Log.d(TAG, "lensFocusDistance=" + lensFocusDistance);
mDepthProcessHandler.obtainMessage(MSG_NEW_LENS_FOCUS_DISTANCE_BAYER, 0, 0, lensFocusDistance).sendToTarget();
}
}
}
@Override
public void onCaptureFailed(CameraCaptureSession session, CaptureRequest request, CaptureFailure result) {
Log.d(TAG, "capture - onCaptureFailed: " + cam);
if (isClosing())
Log.d(TAG, "capture - onCaptureFailed - closing");
else
mImageProcessHandler.obtainMessage(MSG_NEW_CAPTURE_FAIL, cam, 0, result).sendToTarget();
}
@Override
public void onCaptureSequenceCompleted(CameraCaptureSession session, int sequenceId, long frameNumber) {
Log.d(TAG, "capture - onCaptureSequenceCompleted: " + cam);
}
};
List<CaptureRequest> burstList = new ArrayList<CaptureRequest>();
requestBuilder.addTarget(mImageReader[cam].getSurface());
for (int i = 0; i < mNumBurstCount; i++) {
requestBuilder.setTag(new Object());
CaptureRequest request = requestBuilder.build();
burstList.add(request);
}
mImageProcessHandler.obtainMessage(MSG_START_CAPTURE, cam, burstList.size(), 0).sendToTarget();
session.captureBurst(burstList, captureCallback, captureCallbackHandler);
}
use of android.hardware.camera2.CameraCaptureSession in project android_packages_apps_Snap by LineageOS.
the class ClearSightImageProcessor method createCaptureSession.
public void createCaptureSession(boolean bayer, CameraDevice device, List<Surface> surfaces, CameraCaptureSession.StateCallback captureSessionCallback) throws CameraAccessException {
Log.d(TAG, "createCaptureSession: " + bayer);
int cam = bayer ? CAM_TYPE_BAYER : CAM_TYPE_MONO;
surfaces.add(mImageReader[cam].getSurface());
surfaces.add(mEncodeImageReader[cam].getSurface());
// Here, we create a CameraCaptureSession for camera preview.
device.createReprocessableCaptureSession(new InputConfiguration(mImageReader[cam].getWidth(), mImageReader[cam].getHeight(), mImageReader[cam].getImageFormat()), surfaces, captureSessionCallback, null);
}
Aggregations