Size.java revision 634246650a5ae72bb80ab4fe4be5da1afa23b684
1/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.camera.util;
18
19import android.graphics.Point;
20import android.hardware.Camera;
21
22import java.util.ArrayList;
23import java.util.List;
24
25/**
26 * An immutable simple size container.
27 */
28public class Size {
29
30    /**
31     * An helper method to build a list of this class from a list of
32     * {@link android.hardware.Camera.Size}.
33     *
34     * @param cameraSizes Source.
35     * @return The built list.
36     */
37    public static List<Size> buildListFromCameraSizes(List<Camera.Size> cameraSizes) {
38        ArrayList<Size> list = new ArrayList<Size>(cameraSizes.size());
39        for (Camera.Size cameraSize : cameraSizes) {
40            list.add(new Size(cameraSize));
41        }
42        return list;
43    }
44
45    public final int width;
46    public final int height;
47
48    /**
49     * Constructor.
50     */
51    public Size(int width, int height) {
52        this.width = width;
53        this.height = height;
54    }
55
56    /**
57     * Copy constructor.
58     */
59    public Size(Size other) {
60        if (other == null) {
61            width = 0;
62            height = 0;
63        } else {
64            width = other.width;
65            height = other.height;
66        }
67    }
68
69    /**
70     * Constructor from a source {@link android.hardware.Camera.Size}.
71     *
72     * @param s The source size.
73     */
74    public Size(Camera.Size s) {
75        if (s == null) {
76            width = 0;
77            height = 0;
78        } else {
79            width = s.width;
80            height = s.height;
81        }
82    }
83
84    /**
85     * Constructor from a source {@link android.graphics.Point}.
86     *
87     * @param p The source size.
88     */
89    public Size(Point p) {
90        if (p == null) {
91            width = 0;
92            height = 0;
93        } else {
94            width = p.x;
95            height = p.y;
96        }
97    }
98
99    @Override
100    public String toString() {
101        return "Size: (" + this.width + " x " + this.height + ")";
102    }
103}
104