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) {
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!:)
source share