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