Android full screen camera - while saving selected camera ratio

We are trying to create something similar to the Instagram Camera screen. ie allow the user to take square photos. By doing this, Ui should allow the user to see the camera in fullScreen mode. We want to force the user to take an image in portrait mode

Getting a possible attitude towards the camera

We calculate the best ratio available from camera on

  private Camera.Size getOptimalPreviewSize(List<Camera.Size> sizes, int w, int h) { final double ASPECT_TOLERANCE = 0.1; double targetRatio = (double) h / w; if (sizes == null) { return null; } Camera.Size optimalSize = null; double minDiff = Double.MAX_VALUE; int targetHeight = h; for (Camera.Size size : sizes) { double ratio = (double) size.width / size.height; if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) { continue; } if (Math.abs(size.height - targetHeight) < minDiff) { optimalSize = size; minDiff = Math.abs(size.height - targetHeight); } } if (optimalSize == null) { minDiff = Double.MAX_VALUE; for (Camera.Size size : sizes) { if (Math.abs(size.height - targetHeight) < minDiff) { optimalSize = size; minDiff = Math.abs(size.height - targetHeight); } } } return optimalSize; } 

Make it fullscreen.

 public FrameLayout setCameraLayout(int width, int height) { float newProportion = (float) width / (float) height; // Get the width of the screen int screenWidth = this.customCameraActivity.getWindowManager().getDefaultDisplay() .getWidth(); int screenHeight = this.customCameraActivity.getWindowManager().getDefaultDisplay() .getHeight(); float screenProportion = (float) screenWidth / (float) screenHeight; // Get the SurfaceView layout parameters ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams)preview.getLayoutParams(); if (newProportion > screenProportion) { lp.width = screenWidth; lp.height = (int) ((float) screenWidth / newProportion); } else { lp.width = (int) (newProportion * (float) screenHeight); lp.height = screenHeight; } //calculate the amount that takes to make it full screen (in the `height` parameter) float propHeight = screenHeight / lp.height; //make it full screen( lp.width = (int)(lp.width * propHeight); lp.height = (int)(lp.height * propHeight); frameLayout.setLayoutParams(lp); return frameLayout; } 

setCameraLayout subscribers

OnCreate of Activity and then surfaceChanged

  @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { if (getHolder().getSurface() == null) { return; } try { cameraManager.getCamera().stopPreview(); } catch (Exception e) { // tried to stop a non-existent preview } try { Camera.Parameters cameraSettings = cameraManager.getCamera().getParameters(); cameraSettings.setPreviewSize(mPreviewSize.width, mPreviewSize.height); this.cameraView.setCameraLayout(mPreviewSize.width, mPreviewSize.height); cameraManager.getCamera().setParameters(cameraSettings); cameraManager.getCamera().setPreviewDisplay(holder); cameraManager.getCamera().startPreview(); } catch (Exception e) { Log.d(TAG, "Error starting camera preview: " + e.getMessage()); } } 

goal

View in full screen camera on the phone.

Problem

Now we get a distortion-free preview, which is GOOD! , and it has the same height as phone , which is also GOOD! But! The preview width is larger than the phone width (of course), so it turns out that the center of the camera is not in the center of the phone. Possible solutions that we were thinking about:

  • move left to a negative position so that the center of the preview is in the center of the screen.
  • crop location and draw only the center of the new preview , which should be visible for phone screens

Any ideas how to overcome this problem?

Thanks a lot!:)

+5
source share
2 answers

I was able to successfully center the preview by setting the gravity of SurfaceView:

 FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(wid, hei); lp.gravity = Gravity.CENTER; mSv.setLayoutParams(lp); 

The snapshots of the code are taken from here , and you can use this code if you have the patience to decrypt it. I think I solved similar problems (rotation of the preview, distortion, insert / fill)

Good luck.

+6
source

If I understand from the code snippets, you re-create the camera instance each time the user changes orientation or rotates the camera. Is there a reason for this?

Why not limit the viewing to just a portrait and have an overlay look that rotates? I am sure that this is what others do (Instagram, Snapchat, etc.). I will edit with an example of this if I find it.

+1
source

Source: https://habr.com/ru/post/1216474/


All Articles