1

I have gone through lot of stackoverflow threads on this topic but I did not get solution for my problem. I found a method which calcultate best PreviewSize for your screen. For example this method--

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;
}

To the above method I have to pass screen width and height. Consider I want a picture in 1024*768 but my screen resolution is 1280*720, above method returns 1280*720 as best PreviewSize and camera is capturing more than what actually visible in my display. Is there any way to calculate matching PreviewSize based on required picture size.

Praveena
  • 6,340
  • 2
  • 40
  • 53
  • Well please tell what you would consider the best `matching PreviewSize' for the sizes you gave. – greenapps Nov 23 '14 at 12:00
  • @greenapps best 'matching' preview size means, that preview should fit in fullscreen and should maintain aspect ratio of the picture and should show what actually camera is covering. – Praveena Nov 23 '14 at 13:31
  • _"I want a picture in 1024*768"_ - is this the size of your view, or of the still image you will capture when, e.g., a button is pressed? – Alex Cohn Nov 23 '14 at 15:45
  • @Alex Cohn its size of a still image.. Setting it using `params.setPictureSize()` – Praveena Nov 23 '14 at 17:58
  • Then simply supply the width and height of the still picture as **w** and **h** of your call to `getOptimalPreviewSize()`. – Alex Cohn Nov 23 '14 at 18:03
  • @AlexCohn thanks for that and even tried that.. In this scenario camera is catiching more than what is visible in my display. – Praveena Nov 24 '14 at 05:07
  • No I cannot understand. You mean the preview size is more than screen size? Or that you have black "pillars" around the preview because the aspect ratio does not fit? – Alex Cohn Nov 24 '14 at 19:26
  • @AlexCohn yes. preview size is more than screen size – Praveena Nov 25 '14 at 05:45
  • There usually is no problem with that, like having 1920x1080 preview on 1280x720 screen; not even in performance, because the camera frames are scaled down (or up, if needed) in hardware to fit the surface where they are displayed. – Alex Cohn Nov 25 '14 at 18:38
  • @AlexCohn I am understanding it little bit now but my original problem still exist.The taken picture using my camera app has covered more than what was visible in the camera preview. Should I manually crop it? – Praveena Nov 26 '14 at 03:12
  • Now, this is a whole different story. See _[Android: Taken photo is bigger than the preview](http://stackoverflow.com/questions/26958110/android-taken-photo-is-bigger-than-the-preview)_ – Alex Cohn Nov 26 '14 at 05:01
  • @AlexCohn What I understood from your answer in that link is, cropping is the only way to get it right if my desired picture size has different aspect ratio compared to preview. Please let me know if I am wrong here – Praveena Nov 26 '14 at 05:37
  • Right, but usually there exists an acceptable pair of picture/preview sizes that share the same aspect ratios and same field of view. – Alex Cohn Nov 26 '14 at 05:42

0 Answers0