Last active
May 24, 2020 07:05
-
-
Save ananthrajsingh/219e9a6cbfd840cec730a1cccdf30035 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Sets up member variables related to front camera. | |
* @param width The width of available size for camera preview | |
* @param height The height of available size for camera preview | |
*/ | |
private fun setUpCameraOutputsFront(width: Int, height: Int) { | |
val manager = activity?.getSystemService(Context.CAMERA_SERVICE) as CameraManager | |
try { | |
for (cameraId in manager.cameraIdList) { | |
val characteristics = manager.getCameraCharacteristics(cameraId) | |
// We need front facing camera. | |
val cameraDirection = characteristics.get(CameraCharacteristics.LENS_FACING) | |
if (cameraDirection != null && | |
cameraDirection == CameraCharacteristics.LENS_FACING_BACK) { | |
continue | |
} | |
val map = characteristics.get( | |
CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP) ?: continue | |
// For still image captures, we use the largest available size. | |
val aspectRatio = Collections.max(Arrays.asList(*map.getOutputSizes(ImageFormat.JPEG)), CompareSizesByViewAspectRatio(textureViewFront.height, textureViewFront.width)) | |
imageReaderFront = ImageReader.newInstance(aspectRatio.width, aspectRatio.height, ImageFormat.JPEG, /*maxImages*/ 2).apply { | |
setOnImageAvailableListener(onImageAvailableListenerFront, backgroundHandler) | |
} | |
// Find out if we need to swap dimension to get the preview size relative to sensor | |
// coordinate. | |
val displayRotation = activity!!.windowManager.defaultDisplay.rotation | |
sensorOrientationFront = characteristics.get(CameraCharacteristics.SENSOR_ORIENTATION)!! | |
val swappedDimensions = areDimensionsSwappedFront(displayRotation) | |
val displaySize = Point() | |
activity!!.windowManager.defaultDisplay.getSize(displaySize) | |
val rotatedPreviewWidth = if (swappedDimensions) height else width | |
val rotatedPreviewHeight = if (swappedDimensions) width else height | |
var maxPreviewWidth = if (swappedDimensions) displaySize.y else displaySize.x | |
var maxPreviewHeight = if (swappedDimensions) displaySize.x else displaySize.y | |
if (maxPreviewWidth > MAX_PREVIEW_WIDTH) maxPreviewWidth = MAX_PREVIEW_WIDTH | |
if (maxPreviewHeight > MAX_PREVIEW_HEIGHT) maxPreviewHeight = MAX_PREVIEW_HEIGHT | |
previewSizeFront = chooseOptimalSize(map.getOutputSizes(SurfaceTexture::class.java), | |
rotatedPreviewWidth, rotatedPreviewHeight, | |
maxPreviewWidth, maxPreviewHeight, | |
aspectRatio) | |
this.cameraIdFront = cameraId | |
// We've found a viable camera and finished setting up member variables, | |
// so we don't need to iterate through other available cameras. | |
return | |
} | |
} catch (e: CameraAccessException) { | |
Log.e(TAG, e.toString()) | |
} catch (e: NullPointerException) { | |
// Currently an NPE is thrown when the Camera2API is used but not supported on the | |
// device this code runs. | |
ErrorDialog.newInstance(getString(R.string.camera_error)) | |
.show(childFragmentManager, FRAGMENT_DIALOG) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment