2

We implemented the custom camera functionality in my app. This was working fine in all the devices like nexus4,5,samsung s3,s4 etc except in Samsung galaxy S4 mini. The issue is image was stretching in front camera only but it's working good in back camera of that device.

For Samsung galaxy s4 mini, we are getting the screen size is 540X960 and we got supported preview size is 1280x720. The aspect ratio also same but we are facing image stretching issue. I was tried a lot but i am unable to solve this issue especially in front camera of samsung galaxy s4 mini. Please can anyone help me.

Code

int currentCameraId = Camera.CameraInfo.CAMERA_FACING_FRONT;
Camera camera = Camera.open(currentCameraId);
try {
      camera.setPreviewDisplay(surfaceHolder);
      Camera.Parameters parameters = camera.getParameters();
      parameters.setPictureFormat(PixelFormat.JPEG);
      parameters.set("orientation", "portrait");

      camera.setDisplayOrientation(90);
      parameters.setRotation(270);
      Display display = getWindowManager().getDefaultDisplay();
      int widthd = display.getWidth(); // deprecated
      int heightd = display.getHeight();

      List<Camera.Size> previewSizes = parameters.getSupportedPreviewSizes();

      List<Camera.Size> pictureSizes = parameters.getSupportedPictureSizes();

      Camera.Size mPreviewSize = getOptimalPreviewSize(previewSizes, widthd, heightd);

      Camera.Size mpicSize = getOptimalPreviewSize(pictureSizes, widthd, heightd);


      parameters.setPreviewSize(mPreviewSize.width,mPreviewSize.height);
      parameters.setPictureSize(mpicSize.width,mpicSize.height);
      camera.setParameters(parameters);
   } catch (IOException exception) {
       camera.release();
   }
   camera.startPreview();

Please refer the following code for computing the optimal size.

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;
    }
naresh
  • 10,332
  • 25
  • 81
  • 124
  • 1
    How are you configuring the size of the preview window to have the correct aspect ratio? – fadden Jun 16 '14 at 14:25
  • @fadden: Updated the code please once refer it. – naresh Jun 17 '14 at 13:02
  • 1
    You may want to adjust the size of the surface to match the camera, rather than trying to find a camera preview whose size matches the surface. Also, unless you're using a "lights out" theme, the size of the surface won't match the size of the display. See Grafika (https://github.com/google/grafika) for some examples of manipulating the view size; it uses AspectFrameLayout + SurfaceView for some, and a TextureView for others. – fadden Jun 17 '14 at 14:15
  • @fadden: May be it's true. Could you please suggest which files are required for implementing the custom camera which having the capability to capture the pictures as well as record the video and save it in the SD card. – naresh Jun 19 '14 at 14:53
  • Well, the "Continuous Capture" and "Show + Capture Camera" activities are good places to start. – fadden Jun 19 '14 at 14:55
  • @fadden: Here they set the camera size as 1280x720 but I would like to use the maximum space of the screen for the camera in portrait mode only. Is it possible? – naresh Jun 19 '14 at 15:28
  • @fadden: I set the theme for my app as "android:theme="@android:style/Theme.NoTitleBar.Fullscreen". Is it working or we should use the light theme only? – naresh Jun 19 '14 at 15:35
  • I have the same problem, I don't know why but when I setImageSize with 1920x1080 (one of the sizes supported) the picture is scrached and the preview as well. If i set 1280x720 (exactly same ratio of the previous one) as ImageSize, the image is correct and preview as well. In conclusion for now i did an horrible thing : I test the device name and ignore the – Gigi Feb 07 '17 at 11:58
  • 1
    See this answer : it works for me : http://stackoverflow.com/questions/42690797/android-camera-preview-is-stretched-in-samsung-galaxy-s4-mini?answertab=active#tab-top – Nawrez Mar 09 '17 at 14:32

0 Answers0