OverlayFrame.java revision 21e9da6f446301756ddabbfb9d61155db5480366
1/*
2 * Copyright (C) 2010 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 android.media.videoeditor;
18
19import java.io.File;
20import java.io.FileNotFoundException;
21import java.io.FileOutputStream;
22import java.io.IOException;
23
24import android.graphics.Bitmap;
25import android.graphics.BitmapFactory;
26import android.graphics.Bitmap.CompressFormat;
27
28
29/**
30 * This class is used to overlay an image on top of a media item.
31 * {@hide}
32 */
33public class OverlayFrame extends Overlay {
34    // Instance variables
35    private final Bitmap mBitmap;
36    private String mFilename;
37
38    /**
39     * An object of this type cannot be instantiated by using the default
40     * constructor
41     */
42    @SuppressWarnings("unused")
43    private OverlayFrame() {
44        this(null, (String)null, 0, 0);
45    }
46
47    /**
48     * Constructor for an OverlayFrame
49     *
50     * @param overlayId The overlay id
51     * @param bitmap The bitmap to be used as an overlay. The size of the
52     *      bitmap must equal to the size of the media item to which it is
53     *      added. The bitmap is typically a decoded PNG file.
54     * @param startTimeMs The overlay start time in milliseconds
55     * @param durationMs The overlay duration in milliseconds
56     *
57     * @throws IllegalArgumentException if the file type is not PNG or the
58     *      startTimeMs and durationMs are incorrect.
59     */
60    public OverlayFrame(String overlayId, Bitmap bitmap, long startTimeMs,
61            long durationMs) {
62        super(overlayId, startTimeMs, durationMs);
63        mBitmap = bitmap;
64        mFilename = null;
65    }
66
67    /**
68     * Constructor for an OverlayFrame. This constructor can be used to
69     * restore the overlay after it was saved internally by the video editor.
70     *
71     * @param overlayId The overlay id
72     * @param filename The file name that contains the overlay.
73     * @param startTimeMs The overlay start time in milliseconds
74     * @param durationMs The overlay duration in milliseconds
75     *
76     * @throws IllegalArgumentException if the file type is not PNG or the
77     *      startTimeMs and durationMs are incorrect.
78     */
79    OverlayFrame(String overlayId, String filename, long startTimeMs, long durationMs) {
80        super(overlayId, startTimeMs, durationMs);
81        mFilename = filename;
82        mBitmap = BitmapFactory.decodeFile(mFilename);
83    }
84
85    /**
86     * @return Get the overlay bitmap
87     */
88    public Bitmap getBitmap() {
89        return mBitmap;
90    }
91
92    /**
93     * Get the file name of this overlay
94     */
95    String getFilename() {
96        return mFilename;
97    }
98
99    /**
100     * Save the overlay to the project folder
101     *
102     * @param editor The video editor
103     *
104     * @return
105     * @throws FileNotFoundException if the bitmap cannot be saved
106     * @throws IOException if the bitmap file cannot be saved
107     */
108    String save(VideoEditor editor) throws FileNotFoundException, IOException {
109        if (mFilename != null) {
110            return mFilename;
111        }
112
113        mFilename = editor.getPath() + "/" + getId() + ".png";
114        // Save the image to a local file
115        final FileOutputStream out = new FileOutputStream(mFilename);
116        mBitmap.compress(CompressFormat.PNG, 100, out);
117        out.flush();
118        out.close();
119        return mFilename;
120    }
121
122    /**
123     * Delete the overlay file
124     */
125    void invalidate() {
126        if (mFilename != null) {
127            new File(mFilename).delete();
128        }
129    }
130}
131