VideoRotationMetadataLoader.java revision 8ee16b8a323ffa20e6fb1270d498ec445f64defc
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
17package com.android.camera.data;
18
19import android.media.MediaMetadataRetriever;
20
21import com.android.camera.debug.Log;
22
23public class VideoRotationMetadataLoader {
24    private static final Log.Tag TAG = new Log.Tag("VidRotDataLoader");
25
26    private static final String ROTATE_90 = "90";
27    private static final String ROTATE_270 = "270";
28
29    static boolean isRotated(FilmstripItem filmstripItem) {
30        final String rotation = filmstripItem.getMetadata().getVideoOrientation();
31        return ROTATE_90.equals(rotation) || ROTATE_270.equals(rotation);
32    }
33
34    static void loadRotationMetadata(final FilmstripItem data) {
35        final String path = data.getData().getFilePath();
36        MediaMetadataRetriever retriever = new MediaMetadataRetriever();
37        try {
38            retriever.setDataSource(path);
39            data.getMetadata().setVideoOrientation(retriever.extractMetadata(
40                MediaMetadataRetriever.METADATA_KEY_VIDEO_ROTATION));
41
42            String val = retriever.extractMetadata(
43                    MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH);
44
45            data.getMetadata().setVideoWidth(Integer.parseInt(val));
46
47            val = retriever.extractMetadata(
48                    MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT);
49
50            data.getMetadata().setVideoHeight(Integer.parseInt(val));
51        } catch (RuntimeException ex) {
52            // setDataSource() can cause RuntimeException beyond
53            // IllegalArgumentException. e.g: data contain *.avi file.
54            Log.e(TAG, "MediaMetdataRetriever.setDataSource() fail", ex);
55        }
56    }
57}
58