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.data;
18 
19 import android.graphics.Bitmap;
20 import android.graphics.BitmapFactory;
21 import android.graphics.Matrix;
22 import android.graphics.Point;
23 import android.media.MediaMetadataRetriever;
24 
25 import com.android.camera.debug.Log;
26 
27 import java.io.IOException;
28 import java.io.InputStream;
29 
30 import javax.microedition.khronos.opengles.GL11;
31 
32 /**
33  * An utility class for data in content provider.
34  */
35 public class FilmstripItemUtils {
36 
37     private static final Log.Tag TAG = new Log.Tag("LocalDataUtil");
38 
39     /**
40      * @param mimeType The MIME type to check.
41      * @return Whether the MIME is a video type.
42      */
isMimeTypeVideo(String mimeType)43     public static boolean isMimeTypeVideo(String mimeType) {
44         return mimeType != null && mimeType.startsWith("video/");
45     }
46 
47     /**
48      * Checks whether the MIME type represents an image media item.
49      *
50      * @param mimeType The MIME type to check.
51      * @return Whether the MIME is a image type.
52      */
isMimeTypeImage(String mimeType)53     public static boolean isMimeTypeImage(String mimeType) {
54         return mimeType != null && mimeType.startsWith("image/");
55     }
56 
57 
58     /**
59      * Decodes the dimension of a bitmap.
60      *
61      * @param is An input stream with the data of the bitmap.
62      * @return The decoded width/height is stored in Point.x/Point.y
63      *         respectively.
64      */
decodeBitmapDimension(InputStream is)65     public static Point decodeBitmapDimension(InputStream is) {
66         Point size = null;
67         BitmapFactory.Options justBoundsOpts = new BitmapFactory.Options();
68         justBoundsOpts.inJustDecodeBounds = true;
69         BitmapFactory.decodeStream(is, null, justBoundsOpts);
70         if (justBoundsOpts.outWidth > 0 && justBoundsOpts.outHeight > 0) {
71             size = new Point(justBoundsOpts.outWidth, justBoundsOpts.outHeight);
72         } else {
73             Log.e(TAG, "Bitmap dimension decoding failed");
74         }
75         return size;
76     }
77 
78     /**
79      * Load the thumbnail of an image from an {@link java.io.InputStream}.
80      *
81      * @param stream The input stream of the image.
82      * @param imageWidth Image width.
83      * @param imageHeight Image height.
84      * @param widthBound The bound of the width of the decoded image.
85      * @param heightBound The bound of the height of the decoded image.
86      * @param orientation The orientation of the image. The image will be rotated
87      *                    clockwise in degrees.
88      * @param maximumPixels The bound for the number of pixels of the decoded image.
89      * @return {@code null} if the decoding failed.
90      */
loadImageThumbnailFromStream(InputStream stream, int imageWidth, int imageHeight, int widthBound, int heightBound, int orientation, int maximumPixels)91     public static Bitmap loadImageThumbnailFromStream(InputStream stream, int imageWidth,
92             int imageHeight, int widthBound, int heightBound, int orientation,
93             int maximumPixels) {
94 
95         /** 32K buffer. */
96         byte[] decodeBuffer = new byte[32 * 1024];
97 
98         if (orientation % 180 != 0) {
99             int temp = imageHeight;
100             imageHeight = imageWidth;
101             imageWidth = temp;
102         }
103 
104         // Generate Bitmap of maximum size that fits into widthBound x heightBound.
105         // Algorithm: start with full size and step down in powers of 2.
106         int targetWidth = imageWidth;
107         int targetHeight = imageHeight;
108         int sampleSize = 1;
109         while (targetHeight > heightBound || targetWidth > widthBound ||
110                 targetHeight > GL11.GL_MAX_TEXTURE_SIZE || targetWidth > GL11.GL_MAX_TEXTURE_SIZE ||
111                 targetHeight * targetWidth > maximumPixels) {
112             sampleSize <<= 1;
113             targetWidth = imageWidth / sampleSize;
114             targetHeight = imageWidth / sampleSize;
115         }
116 
117         // For large (> MAXIMUM_TEXTURE_SIZE) high aspect ratio (panorama)
118         // Bitmap requests:
119         //   Step 1: ask for double size.
120         //   Step 2: scale maximum edge down to MAXIMUM_TEXTURE_SIZE.
121         //
122         // Here's the step 1: double size.
123         if ((heightBound > GL11.GL_MAX_TEXTURE_SIZE || widthBound > GL11.GL_MAX_TEXTURE_SIZE) &&
124                 targetWidth * targetHeight < maximumPixels / 4 && sampleSize > 1) {
125             sampleSize >>= 2;
126         }
127 
128         BitmapFactory.Options opts = new BitmapFactory.Options();
129         opts.inSampleSize = sampleSize;
130         opts.inTempStorage = decodeBuffer;
131         Bitmap b = BitmapFactory.decodeStream(stream, null, opts);
132 
133         if (b == null) {
134             return null;
135         }
136 
137         // Step 2: scale maximum edge down to maximum texture size.
138         // If Bitmap maximum edge > MAXIMUM_TEXTURE_SIZE, which can happen for panoramas,
139         // scale to fit in MAXIMUM_TEXTURE_SIZE.
140         if (b.getWidth() > GL11.GL_MAX_TEXTURE_SIZE || b.getHeight() >
141                 GL11.GL_MAX_TEXTURE_SIZE) {
142             int maxEdge = Math.max(b.getWidth(), b.getHeight());
143             b = Bitmap.createScaledBitmap(b, b.getWidth() * GL11.GL_MAX_TEXTURE_SIZE / maxEdge,
144                     b.getHeight() * GL11.GL_MAX_TEXTURE_SIZE / maxEdge, false);
145         }
146 
147         // Not called often because most modes save image data non-rotated.
148         if (orientation != 0 && b != null) {
149             Matrix m = new Matrix();
150             m.setRotate(orientation);
151             b = Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(), m, false);
152         }
153 
154         return b;
155     }
156 
157     /**
158      * Loads the thumbnail of a video.
159      *
160      * @param path The path to the video file.
161      * @return {@code null} if the loading failed.
162      */
loadVideoThumbnail(String path)163     public static Bitmap loadVideoThumbnail(String path) {
164         Bitmap bitmap = null;
165         MediaMetadataRetriever retriever = new MediaMetadataRetriever();
166         try {
167             retriever.setDataSource(path);
168             byte[] data = retriever.getEmbeddedPicture();
169             if (data != null) {
170                 bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
171             }
172             if (bitmap == null) {
173                 bitmap = retriever.getFrameAtTime();
174             }
175         } catch (IllegalArgumentException e) {
176             Log.e(TAG, "MediaMetadataRetriever.setDataSource() fail:" + e.getMessage());
177         }
178         try {
179             retriever.release();
180         } catch (IOException e) {
181             // We ignore errors occurred while releasing the MediaMetadataRetriever.
182         }
183         return bitmap;
184     }
185 }
186