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.ex.camera2.portability; 18 19 import android.graphics.Point; 20 import android.hardware.Camera; 21 import android.text.TextUtils; 22 23 import java.util.ArrayList; 24 import java.util.List; 25 26 /** 27 * An immutable simple size container. 28 */ 29 public class Size { 30 public static final String DELIMITER = ","; 31 32 /** 33 * An helper method to build a list of this class from a list of 34 * {@link android.hardware.Camera.Size}. 35 * 36 * @param cameraSizes Source. 37 * @return The built list. 38 */ buildListFromCameraSizes(List<Camera.Size> cameraSizes)39 public static List<Size> buildListFromCameraSizes(List<Camera.Size> cameraSizes) { 40 ArrayList<Size> list = new ArrayList<Size>(cameraSizes.size()); 41 for (Camera.Size cameraSize : cameraSizes) { 42 list.add(new Size(cameraSize)); 43 } 44 return list; 45 } 46 47 /** 48 * A helper method to build a list of this class from a list of {@link android.util.Size}. 49 * 50 * @param cameraSizes Source. 51 * @return The built list. 52 */ buildListFromAndroidSizes(List<android.util.Size> androidSizes)53 public static List<Size> buildListFromAndroidSizes(List<android.util.Size> androidSizes) { 54 ArrayList<Size> list = new ArrayList<Size>(androidSizes.size()); 55 for (android.util.Size androidSize : androidSizes) { 56 list.add(new Size(androidSize)); 57 } 58 return list; 59 } 60 61 /** 62 * Encode List of this class as comma-separated list of integers. 63 * 64 * @param sizes List of this class to encode. 65 * @return encoded string. 66 */ listToString(List<Size> sizes)67 public static String listToString(List<Size> sizes) { 68 ArrayList<Integer> flatSizes = new ArrayList<>(); 69 for (Size s : sizes) { 70 flatSizes.add(s.width()); 71 flatSizes.add(s.height()); 72 } 73 return TextUtils.join(DELIMITER, flatSizes); 74 } 75 76 /** 77 * Decode comma-separated even-length list of integers into a List of this class. 78 * 79 * @param encodedSizes encoded string. 80 * @return List of this class. 81 */ stringToList(String encodedSizes)82 public static List<Size> stringToList(String encodedSizes) { 83 String[] flatSizes = TextUtils.split(encodedSizes, DELIMITER); 84 ArrayList<Size> list = new ArrayList<>(); 85 for (int i = 0; i < flatSizes.length; i += 2) { 86 int width = Integer.parseInt(flatSizes[i]); 87 int height = Integer.parseInt(flatSizes[i + 1]); 88 list.add(new Size(width,height)); 89 } 90 return list; 91 } 92 93 private final Point val; 94 95 /** 96 * Constructor. 97 */ Size(int width, int height)98 public Size(int width, int height) { 99 val = new Point(width, height); 100 } 101 102 /** 103 * Copy constructor. 104 */ Size(Size other)105 public Size(Size other) { 106 if (other == null) { 107 val = new Point(0, 0); 108 } else { 109 val = new Point(other.width(), other.height()); 110 } 111 } 112 113 /** 114 * Constructor from a source {@link android.hardware.Camera.Size}. 115 * 116 * @param other The source size. 117 */ Size(Camera.Size other)118 public Size(Camera.Size other) { 119 if (other == null) { 120 val = new Point(0, 0); 121 } else { 122 val = new Point(other.width, other.height); 123 } 124 } 125 126 /** 127 * Constructor from a source {@link android.util.Size}. 128 * 129 * @param other The source size. 130 */ Size(android.util.Size other)131 public Size(android.util.Size other) { 132 if (other == null) { 133 val = new Point(0, 0); 134 } else { 135 val = new Point(other.getWidth(), other.getHeight()); 136 } 137 } 138 139 /** 140 * Constructor from a source {@link android.graphics.Point}. 141 * 142 * @param p The source size. 143 */ Size(Point p)144 public Size(Point p) { 145 if (p == null) { 146 val = new Point(0, 0); 147 } else { 148 val = new Point(p); 149 } 150 } 151 width()152 public int width() { 153 return val.x; 154 } 155 height()156 public int height() { 157 return val.y; 158 } 159 160 @Override equals(Object o)161 public boolean equals(Object o) { 162 if (o instanceof Size) { 163 Size other = (Size) o; 164 return val.equals(other.val); 165 } 166 return false; 167 } 168 169 @Override hashCode()170 public int hashCode() { 171 return val.hashCode(); 172 } 173 174 @Override toString()175 public String toString() { 176 return "Size: (" + this.width() + " x " + this.height() + ")"; 177 } 178 } 179