Sascha Haeberling | e3dfd5a | 2014-08-05 15:54:42 -0700 | [diff] [blame] | 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 | |
| 17 | package com.android.camera.util; |
| 18 | |
Paul Rohde | 01d5603 | 2014-12-18 11:45:34 -0800 | [diff] [blame] | 19 | import android.annotation.TargetApi; |
Sascha Haeberling | f262790 | 2014-09-02 14:20:57 -0700 | [diff] [blame] | 20 | import android.graphics.Point; |
Paul Rohde | 01d5603 | 2014-12-18 11:45:34 -0800 | [diff] [blame] | 21 | import android.os.Build.VERSION_CODES; |
Senpo Hu | 2272f8a | 2014-12-16 11:56:39 -0800 | [diff] [blame^] | 22 | import android.hardware.Camera; |
| 23 | import android.text.TextUtils; |
Sascha Haeberling | f262790 | 2014-09-02 14:20:57 -0700 | [diff] [blame] | 24 | |
| 25 | import java.util.ArrayList; |
| 26 | import java.util.List; |
| 27 | |
Sascha Haeberling | e3dfd5a | 2014-08-05 15:54:42 -0700 | [diff] [blame] | 28 | /** |
| 29 | * Simple size class until we are 'L' only and can use android.util.Size. |
| 30 | */ |
| 31 | public class Size { |
Senpo Hu | 2272f8a | 2014-12-16 11:56:39 -0800 | [diff] [blame^] | 32 | public static final String LIST_DELIMITER = ","; |
| 33 | |
Sascha Haeberling | e3dfd5a | 2014-08-05 15:54:42 -0700 | [diff] [blame] | 34 | private final int width; |
| 35 | private final int height; |
| 36 | |
Senpo Hu | 2272f8a | 2014-12-16 11:56:39 -0800 | [diff] [blame^] | 37 | public Size(Point point) { |
| 38 | this.width = point.x; |
| 39 | this.height = point.y; |
| 40 | } |
| 41 | |
| 42 | @TargetApi(VERSION_CODES.L) |
| 43 | public Size(android.util.Size size) { |
| 44 | this.width = size.getWidth(); |
| 45 | this.height = size.getHeight(); |
| 46 | } |
| 47 | |
| 48 | public Size(int width, int height) { |
| 49 | this.width = width; |
| 50 | this.height = height; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Constructor from a source {@link android.hardware.Camera.Size}. |
| 55 | * |
| 56 | * @param other The source size. |
| 57 | */ |
| 58 | public Size(Camera.Size other) { |
| 59 | this.width = other.width; |
| 60 | this.height = other.height; |
| 61 | } |
| 62 | |
| 63 | public Size(com.android.ex.camera2.portability.Size size) { |
| 64 | this.width = size.width(); |
| 65 | this.height = size.height(); |
| 66 | } |
| 67 | |
| 68 | public int getWidth() { |
| 69 | return width; |
| 70 | } |
| 71 | public int getHeight() { |
| 72 | return height; |
| 73 | } |
| 74 | |
| 75 | public int width() { |
| 76 | return width; |
| 77 | } |
| 78 | public int height() { |
| 79 | return height; |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public String toString() { |
| 84 | return width + "x" + height; |
| 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public boolean equals(Object other) { |
| 89 | if (!(other instanceof Size)) { |
| 90 | return false; |
| 91 | } |
| 92 | |
| 93 | Size otherSize = (Size) other; |
| 94 | return otherSize.width == this.width && otherSize.height == this.height; |
| 95 | } |
| 96 | |
| 97 | public com.android.ex.camera2.portability.Size toPortabilitySize() { |
| 98 | return new com.android.ex.camera2.portability.Size(width, height); |
| 99 | } |
| 100 | |
Paul Rohde | 01d5603 | 2014-12-18 11:45:34 -0800 | [diff] [blame] | 101 | @TargetApi(VERSION_CODES.L) |
Sascha Haeberling | e3dfd5a | 2014-08-05 15:54:42 -0700 | [diff] [blame] | 102 | public static Size[] convert(android.util.Size[] sizes) { |
| 103 | Size[] converted = new Size[sizes.length]; |
| 104 | for (int i = 0; i < sizes.length; ++i) { |
| 105 | converted[i] = new Size(sizes[i].getWidth(), sizes[i].getHeight()); |
| 106 | } |
| 107 | return converted; |
| 108 | } |
| 109 | |
Sascha Haeberling | f262790 | 2014-09-02 14:20:57 -0700 | [diff] [blame] | 110 | public static List<Size> convert(List<com.android.ex.camera2.portability.Size> sizes) { |
| 111 | ArrayList<Size> converted = new ArrayList<>(sizes.size()); |
| 112 | for (com.android.ex.camera2.portability.Size size : sizes) { |
| 113 | converted.add(new Size(size.width(), size.height())); |
| 114 | } |
| 115 | return converted; |
| 116 | } |
| 117 | |
Senpo Hu | 2272f8a | 2014-12-16 11:56:39 -0800 | [diff] [blame^] | 118 | /** |
| 119 | * Encode List of this class as comma-separated list of integers. |
| 120 | * |
| 121 | * @param sizes List of this class to encode. |
| 122 | * @return encoded string. |
| 123 | */ |
| 124 | public static String listToString(List<Size> sizes) { |
| 125 | ArrayList<Integer> flatSizes = new ArrayList<>(); |
| 126 | for (Size s : sizes) { |
| 127 | flatSizes.add(s.width()); |
| 128 | flatSizes.add(s.height()); |
Sascha Haeberling | e3dfd5a | 2014-08-05 15:54:42 -0700 | [diff] [blame] | 129 | } |
Senpo Hu | 2272f8a | 2014-12-16 11:56:39 -0800 | [diff] [blame^] | 130 | return TextUtils.join(LIST_DELIMITER, flatSizes); |
| 131 | } |
Sascha Haeberling | e3dfd5a | 2014-08-05 15:54:42 -0700 | [diff] [blame] | 132 | |
Senpo Hu | 2272f8a | 2014-12-16 11:56:39 -0800 | [diff] [blame^] | 133 | /** |
| 134 | * Decode comma-separated even-length list of integers into a List of this class. |
| 135 | * |
| 136 | * @param encodedSizes encoded string. |
| 137 | * @return List of this class. |
| 138 | */ |
| 139 | public static List<Size> stringToList(String encodedSizes) { |
| 140 | String[] flatSizes = TextUtils.split(encodedSizes, LIST_DELIMITER); |
| 141 | ArrayList<Size> list = new ArrayList<>(); |
| 142 | for (int i = 0; i < flatSizes.length; i += 2) { |
| 143 | int width = Integer.parseInt(flatSizes[i]); |
| 144 | int height = Integer.parseInt(flatSizes[i + 1]); |
| 145 | list.add(new Size(width, height)); |
| 146 | } |
| 147 | return list; |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * An helper method to build a list of this class from a list of |
| 152 | * {@link android.hardware.Camera.Size}. |
| 153 | * |
| 154 | * @param cameraSizes Source. |
| 155 | * @return The built list. |
| 156 | */ |
| 157 | public static List<Size> buildListFromCameraSizes(List<Camera.Size> cameraSizes) { |
| 158 | ArrayList<Size> list = new ArrayList<Size>(cameraSizes.size()); |
| 159 | for (Camera.Size cameraSize : cameraSizes) { |
| 160 | list.add(new Size(cameraSize)); |
| 161 | } |
| 162 | return list; |
Sascha Haeberling | e3dfd5a | 2014-08-05 15:54:42 -0700 | [diff] [blame] | 163 | } |
| 164 | } |