1 /*
2  * Copyright (C) 2015 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.media.MediaMetadataRetriever;
20 
21 import com.android.camera.debug.Log;
22 
23 import java.io.IOException;
24 
25 public class VideoRotationMetadataLoader {
26     private static final Log.Tag TAG = new Log.Tag("VidRotDataLoader");
27 
28     private static final String ROTATE_90 = "90";
29     private static final String ROTATE_270 = "270";
30 
isRotated(FilmstripItem filmstripItem)31     static boolean isRotated(FilmstripItem filmstripItem) {
32         final String rotation = filmstripItem.getMetadata().getVideoOrientation();
33         return ROTATE_90.equals(rotation) || ROTATE_270.equals(rotation);
34     }
35 
loadRotationMetadata(final FilmstripItem data)36     static boolean loadRotationMetadata(final FilmstripItem data) {
37         final String path = data.getData().getFilePath();
38         MediaMetadataRetriever retriever = new MediaMetadataRetriever();
39         try {
40             retriever.setDataSource(path);
41             data.getMetadata().setVideoOrientation(retriever.extractMetadata(
42                 MediaMetadataRetriever.METADATA_KEY_VIDEO_ROTATION));
43 
44             String val = retriever.extractMetadata(
45                     MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH);
46 
47             data.getMetadata().setVideoWidth(Integer.parseInt(val));
48 
49             val = retriever.extractMetadata(
50                     MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT);
51 
52             data.getMetadata().setVideoHeight(Integer.parseInt(val));
53         } catch (RuntimeException ex) {
54             // setDataSource() can cause RuntimeException beyond
55             // IllegalArgumentException. e.g: data contain *.avi file.
56             Log.e(TAG, "MediaMetdataRetriever.setDataSource() fail", ex);
57         } finally {
58             try {
59                 retriever.release();
60             } catch (IOException e) {
61                 // Ignore errors occurred while releasing the MediaMetadataRetriever.
62             }
63         }
64         return true;
65     }
66 }
67