Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- StreamConfigurationMap map = mCameraCharacteristics.get(
- CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
- if (map == null) {
- throw new IllegalStateException("Failed to get configuration map: " + mCameraId);
- }
- Size[] sizes = map.getOutputSizes(SurfaceTexture.class);
- Size findBestSize (Size[] sizes) {
- //Logic goes here
- }
- //...
- textureView.setBufferSize(bestSize.getWidth(), bestSize.getHeight());
- Surface surface = textureView.getSurface();
- try {
- mPreviewRequestBuilder = mCamera.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
- mPreviewRequestBuilder.addTarget(surface);
- mCamera.createCaptureSession(Arrays.asList(surface, mImageReader.getSurface()),
- mSessionCallback, null);
- } catch (final Exception e) {
- //...
- }
- //Suppose this value is obtained from Step 2.
- //I simply test here by hardcoding a 3:4 aspect ratio, where my phone has a thinner aspect ratio.
- float cameraAspectRatio = (float) 0.75;
- //Preparation
- DisplayMetrics metrics = new DisplayMetrics();
- getWindowManager().getDefaultDisplay().getMetrics(metrics);
- int screenWidth = metrics.widthPixels;
- int screenHeight = metrics.heightPixels;
- int finalWidth = screenWidth;
- int finalHeight = screenHeight;
- int widthDifference = 0;
- int heightDifference = 0;
- float screenAspectRatio = (float) screenWidth / screenHeight;
- //Determines whether we crop width or crop height
- if (screenAspectRatio > cameraAspectRatio) { //Keep width crop height
- finalHeight = (int) (screenWidth / cameraAspectRatio);
- heightDifference = finalHeight - screenHeight;
- } else { //Keep height crop width
- finalWidth = (int) (screenHeight * cameraAspectRatio);
- widthDifference = finalWidth - screenWidth;
- }
- //Apply the result to the Preview
- RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) cameraView.getLayoutParams();
- lp.width = finalWidth;
- lp.height = finalHeight;
- //Below 2 lines are to center the preview, since cropping default occurs at the right and bottom
- lp.leftMargin = - (widthDifference / 2);
- lp.topMargin = - (heightDifference / 2);
- cameraView.setLayoutParams(lp);
Add Comment
Please, Sign In to add comment