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.one.v2.initialization; 18 19 import com.android.camera.CaptureModuleUtil; 20 import com.android.camera.one.PreviewSizeSelector; 21 import com.android.camera.util.Size; 22 23 import java.util.ArrayList; 24 import java.util.Collections; 25 import java.util.Comparator; 26 import java.util.List; 27 28 /** 29 * Picks a preview size. TODO Remove dependency on static CaptureModuleUtil 30 * function and write tests. 31 */ 32 class Camera2PreviewSizeSelector implements PreviewSizeSelector { 33 private final List<Size> mSupportedPreviewSizes; 34 35 public Camera2PreviewSizeSelector(List<Size> supportedPreviewSizes) { 36 mSupportedPreviewSizes = new ArrayList<>(supportedPreviewSizes); 37 } 38 39 public Size pickPreviewSize(Size pictureSize) { 40 if (pictureSize == null) { 41 // TODO The default should be selected by the caller, and 42 // pictureSize should never be null. 43 pictureSize = getLargestPictureSize(); 44 } 45 float pictureAspectRatio = pictureSize.getWidth() / (float) pictureSize.getHeight(); 46 47 Size size = CaptureModuleUtil.getOptimalPreviewSize( 48 (Size[]) mSupportedPreviewSizes.toArray(new Size[mSupportedPreviewSizes.size()]), 49 pictureAspectRatio, null); 50 return size; 51 } 52 53 /** 54 * @return The largest supported picture size. 55 */ 56 private Size getLargestPictureSize() { 57 return Collections.max(mSupportedPreviewSizes, new Comparator<Size>() { 58 @Override 59 public int compare(Size size1, Size size2) { 60 int area1 = size1.getWidth() * size1.getHeight(); 61 int area2 = size2.getWidth() * size2.getHeight(); 62 return Integer.compare(area1, area2); 63 } 64 }); 65 } 66 } 67