Size.java revision 8097973089420749dcd1ab4974a629c2466b31cc
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.ex.camera2.portability; 18 19import android.graphics.Point; 20import android.hardware.Camera; 21import android.text.TextUtils; 22 23import java.util.ArrayList; 24import java.util.List; 25 26/** 27 * An immutable simple size container. 28 */ 29public 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 */ 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 * Encode List of this class as comma-separated list of integers. 49 * 50 * @param sizes List of this class to encode. 51 * @return encoded string. 52 */ 53 public static String listToString(List<Size> sizes) { 54 ArrayList<Integer> flatSizes = new ArrayList<>(); 55 for (Size s : sizes) { 56 flatSizes.add(s.width()); 57 flatSizes.add(s.height()); 58 } 59 return TextUtils.join(DELIMITER, flatSizes); 60 } 61 62 /** 63 * Decode comma-separated even-length list of integers into a List of this class. 64 * 65 * @param encodedSizes encoded string. 66 * @return List of this class. 67 */ 68 public static List<Size> stringToList(String encodedSizes) { 69 String[] flatSizes = TextUtils.split(encodedSizes, DELIMITER); 70 ArrayList<Size> list = new ArrayList<>(); 71 for (int i = 0; i < flatSizes.length; i += 2) { 72 int width = Integer.parseInt(flatSizes[i]); 73 int height = Integer.parseInt(flatSizes[i + 1]); 74 list.add(new Size(width,height)); 75 } 76 return list; 77 } 78 79 private final Point val; 80 81 /** 82 * Constructor. 83 */ 84 public Size(int width, int height) { 85 val = new Point(width, height); 86 } 87 88 /** 89 * Copy constructor. 90 */ 91 public Size(Size other) { 92 if (other == null) { 93 val = new Point(0, 0); 94 } else { 95 val = new Point(other.width(), other.height()); 96 } 97 } 98 99 /** 100 * Constructor from a source {@link android.hardware.Camera.Size}. 101 * 102 * @param other The source size. 103 */ 104 public Size(Camera.Size other) { 105 if (other == null) { 106 val = new Point(0, 0); 107 } else { 108 val = new Point(other.width, other.height); 109 } 110 } 111 112 /** 113 * Constructor from a source {@link android.graphics.Point}. 114 * 115 * @param p The source size. 116 */ 117 public Size(Point p) { 118 if (p == null) { 119 val = new Point(0, 0); 120 } else { 121 val = new Point(p); 122 } 123 } 124 125 public int width() { 126 return val.x; 127 } 128 129 public int height() { 130 return val.y; 131 } 132 133 @Override 134 public boolean equals(Object o) { 135 if (o instanceof Size) { 136 Size other = (Size) o; 137 return val.equals(other.val); 138 } 139 return false; 140 } 141 142 @Override 143 public int hashCode() { 144 return val.hashCode(); 145 } 146 147 @Override 148 public String toString() { 149 return "Size: (" + this.width() + " x " + this.height() + ")"; 150 } 151} 152