Guest User

Untitled

a guest
Mar 23rd, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. StreamConfigurationMap map = mCameraCharacteristics.get(
  2. CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
  3. if (map == null) {
  4. throw new IllegalStateException("Failed to get configuration map: " + mCameraId);
  5. }
  6. Size[] sizes = map.getOutputSizes(SurfaceTexture.class);
  7.  
  8. Size findBestSize (Size[] sizes) {
  9. //Logic goes here
  10. }
  11.  
  12. //...
  13. textureView.setBufferSize(bestSize.getWidth(), bestSize.getHeight());
  14. Surface surface = textureView.getSurface();
  15. try {
  16. mPreviewRequestBuilder = mCamera.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
  17. mPreviewRequestBuilder.addTarget(surface);
  18. mCamera.createCaptureSession(Arrays.asList(surface, mImageReader.getSurface()),
  19. mSessionCallback, null);
  20. } catch (final Exception e) {
  21. //...
  22. }
  23.  
  24. //Suppose this value is obtained from Step 2.
  25. //I simply test here by hardcoding a 3:4 aspect ratio, where my phone has a thinner aspect ratio.
  26. float cameraAspectRatio = (float) 0.75;
  27.  
  28. //Preparation
  29. DisplayMetrics metrics = new DisplayMetrics();
  30. getWindowManager().getDefaultDisplay().getMetrics(metrics);
  31. int screenWidth = metrics.widthPixels;
  32. int screenHeight = metrics.heightPixels;
  33. int finalWidth = screenWidth;
  34. int finalHeight = screenHeight;
  35. int widthDifference = 0;
  36. int heightDifference = 0;
  37. float screenAspectRatio = (float) screenWidth / screenHeight;
  38.  
  39. //Determines whether we crop width or crop height
  40. if (screenAspectRatio > cameraAspectRatio) { //Keep width crop height
  41. finalHeight = (int) (screenWidth / cameraAspectRatio);
  42. heightDifference = finalHeight - screenHeight;
  43. } else { //Keep height crop width
  44. finalWidth = (int) (screenHeight * cameraAspectRatio);
  45. widthDifference = finalWidth - screenWidth;
  46. }
  47.  
  48. //Apply the result to the Preview
  49. RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) cameraView.getLayoutParams();
  50. lp.width = finalWidth;
  51. lp.height = finalHeight;
  52. //Below 2 lines are to center the preview, since cropping default occurs at the right and bottom
  53. lp.leftMargin = - (widthDifference / 2);
  54. lp.topMargin = - (heightDifference / 2);
  55. cameraView.setLayoutParams(lp);
Add Comment
Please, Sign In to add comment