summaryrefslogtreecommitdiffstats
path: root/src/com/android/camera/CaptureModuleUtil.java
blob: 6074dc9e10aeb7f6d4fadd039d82397d74ae0923 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.camera;

import android.content.Context;
import android.content.res.Configuration;
import android.view.Surface;

import com.android.camera.debug.Log;
import com.android.camera.debug.Log.Tag;
import com.android.camera.util.CameraUtil;
import com.android.camera.util.Size;

import java.util.ArrayList;

/**
 * Common utility methods used in capture modules.
 */
public class CaptureModuleUtil {
    private static final Tag TAG = new Tag("CaptureModuleUtil");

    public static int getDeviceNaturalOrientation(Context context) {
        Configuration config = context.getResources().getConfiguration();
        int rotation = CameraUtil.getDisplayRotation();

        if (((rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180) &&
                config.orientation == Configuration.ORIENTATION_LANDSCAPE) ||
                ((rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270) &&
                config.orientation == Configuration.ORIENTATION_PORTRAIT)) {
            return Configuration.ORIENTATION_LANDSCAPE;
        } else {
            return Configuration.ORIENTATION_PORTRAIT;
        }
    }

    /**
     * Equivalent to the
     * {@link CameraUtil#getOptimalPreviewSize(java.util.List, double)}
     * method for the camera1 api.
     */
    public static Size getOptimalPreviewSize(Size[] sizes,double targetRatio) {
        return getOptimalPreviewSize(sizes, targetRatio, null);
    }

    /**
     * Returns the best preview size based on the current display resolution,
     * the available preview sizes, the target aspect ratio (typically the
     * aspect ratio of the picture to be taken) as well as a maximum allowed
     * tolerance. If tolerance is 'null', a default tolerance will be used.
     */
    public static Size getOptimalPreviewSize(Size[] sizes,
            double targetRatio, Double aspectRatioTolerance) {
        // TODO(andyhuibers): Don't hardcode this but use device's measurements.
        final int MAX_ASPECT_HEIGHT = 1080;

        // Count sizes with height <= 1080p to mimic camera1 api behavior.
        int count = 0;
        for (Size s : sizes) {
            if (s.getHeight() <= MAX_ASPECT_HEIGHT) {
                count++;
            }
        }
        ArrayList<Size> camera1Sizes = new ArrayList<Size>(count);

        // Set array of all sizes with height <= 1080p
        for (Size s : sizes) {
            if (s.getHeight() <= MAX_ASPECT_HEIGHT) {
                camera1Sizes.add(new Size(s.getWidth(), s.getHeight()));
            }
        }

        int optimalIndex = CameraUtil
                .getOptimalPreviewSizeIndex(camera1Sizes, targetRatio,
                        aspectRatioTolerance);

        if (optimalIndex == -1) {
            return null;
        }

        Size optimal = camera1Sizes.get(optimalIndex);
        for (Size s : sizes) {
            if (s.getWidth() == optimal.getWidth() && s.getHeight() == optimal.getHeight()) {
                return s;
            }
        }
        return null;
    }

    /**
     * Selects the preview buffer dimensions that are closest in size to the
     * size of the view containing the preview.
     */
    public static Size pickBufferDimensions(Size[] supportedPreviewSizes,
            double bestPreviewAspectRatio,
            Context context) {
        // Swap dimensions if the device is not in its natural orientation.
        boolean swapDimens = (CameraUtil.getDisplayRotation() % 180) == 90;
        // Swap dimensions if the device's natural orientation doesn't match
        // the sensor orientation.
        if (CaptureModuleUtil.getDeviceNaturalOrientation(context)
                == Configuration.ORIENTATION_PORTRAIT) {
            swapDimens = !swapDimens;
        }
        double bestAspect = bestPreviewAspectRatio;
        if (swapDimens) {
            bestAspect = 1 / bestAspect;
        }

        Size pick = CaptureModuleUtil.getOptimalPreviewSize(supportedPreviewSizes,
                bestPreviewAspectRatio, null);
        Log.d(TAG, "Picked buffer size: " + pick.toString());
        return pick;
    }
}