1 /*
2  * Copyright (C) 2017 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.dialer.callcomposer.util;
18 
19 import android.graphics.Bitmap;
20 import android.support.annotation.VisibleForTesting;
21 import com.android.dialer.common.Assert;
22 import com.android.dialer.common.LogUtil;
23 
24 /** Utility class for resizing images before sending them as enriched call attachments. */
25 public final class BitmapResizer {
26   @VisibleForTesting static final int MAX_OUTPUT_RESOLUTION = 640;
27 
28   /**
29    * Returns a bitmap that is a resized version of the parameter image. The image will only be
30    * resized down and sized to be appropriate for an enriched call.
31    */
resizeForEnrichedCalling(Bitmap image)32   public static Bitmap resizeForEnrichedCalling(Bitmap image) {
33     Assert.isWorkerThread();
34 
35     int width = image.getWidth();
36     int height = image.getHeight();
37 
38     LogUtil.i(
39         "BitmapResizer.resizeForEnrichedCalling", "starting height: %d, width: %d", height, width);
40 
41     if (width <= MAX_OUTPUT_RESOLUTION && height <= MAX_OUTPUT_RESOLUTION) {
42       LogUtil.i("BitmapResizer.resizeForEnrichedCalling", "no resizing needed");
43       return image;
44     }
45 
46     if (width > height) {
47       // landscape
48       float ratio = width / (float) MAX_OUTPUT_RESOLUTION;
49       width = MAX_OUTPUT_RESOLUTION;
50       height = (int) (height / ratio);
51     } else if (height > width) {
52       // portrait
53       float ratio = height / (float) MAX_OUTPUT_RESOLUTION;
54       height = MAX_OUTPUT_RESOLUTION;
55       width = (int) (width / ratio);
56     } else {
57       // square
58       height = MAX_OUTPUT_RESOLUTION;
59       width = MAX_OUTPUT_RESOLUTION;
60     }
61 
62     LogUtil.i(
63         "BitmapResizer.resizeForEnrichedCalling", "ending height: %d, width: %d", height, width);
64 
65     return Bitmap.createScaledBitmap(image, width, height, true);
66   }
67 }
68