Home | History | Annotate | Download | only in camera
      1 /*
      2  * Copyright (C) 2011 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;
     18 
     19 import android.graphics.Bitmap;
     20 import android.media.MediaMetadataRetriever;
     21 
     22 import java.io.FileDescriptor;
     23 
     24 public class Thumbnail {
     25     public static Bitmap createVideoThumbnailBitmap(FileDescriptor fd, int targetWidth) {
     26         return createVideoThumbnailBitmap(null, fd, targetWidth);
     27     }
     28 
     29     public static Bitmap createVideoThumbnailBitmap(String filePath, int targetWidth) {
     30         return createVideoThumbnailBitmap(filePath, null, targetWidth);
     31     }
     32 
     33     private static Bitmap createVideoThumbnailBitmap(String filePath, FileDescriptor fd,
     34             int targetWidth) {
     35         Bitmap bitmap = null;
     36         MediaMetadataRetriever retriever = new MediaMetadataRetriever();
     37         try {
     38             if (filePath != null) {
     39                 retriever.setDataSource(filePath);
     40             } else {
     41                 retriever.setDataSource(fd);
     42             }
     43             bitmap = retriever.getFrameAtTime(-1);
     44         } catch (IllegalArgumentException ex) {
     45             // Assume this is a corrupt video file
     46         } catch (RuntimeException ex) {
     47             // Assume this is a corrupt video file.
     48         } finally {
     49             try {
     50                 retriever.release();
     51             } catch (RuntimeException ex) {
     52                 // Ignore failures while cleaning up.
     53             }
     54         }
     55         if (bitmap == null) return null;
     56 
     57         // Scale down the bitmap if it is bigger than we need.
     58         int width = bitmap.getWidth();
     59         int height = bitmap.getHeight();
     60         if (width > targetWidth) {
     61             float scale = (float) targetWidth / width;
     62             int w = Math.round(scale * width);
     63             int h = Math.round(scale * height);
     64             bitmap = Bitmap.createScaledBitmap(bitmap, w, h, true);
     65         }
     66         return bitmap;
     67     }
     68 }
     69