OverlayFrame.java revision 549a65b5a40e6377e2311ba5efb09ae4c94092f0
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 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, null, (String)null, 0, 0); 45 } 46 47 /** 48 * Constructor for an OverlayFrame 49 * 50 * @param mediaItem The media item owner 51 * @param overlayId The overlay id 52 * @param bitmap The bitmap to be used as an overlay. The size of the 53 * bitmap must equal to the size of the media item to which it is 54 * added. The bitmap is typically a decoded PNG file. 55 * @param startTimeMs The overlay start time in milliseconds 56 * @param durationMs The overlay duration in milliseconds 57 * 58 * @throws IllegalArgumentException if the file type is not PNG or the 59 * startTimeMs and durationMs are incorrect. 60 */ 61 public OverlayFrame(MediaItem mediaItem, String overlayId, Bitmap bitmap, long startTimeMs, 62 long durationMs) { 63 super(mediaItem, overlayId, startTimeMs, durationMs); 64 mBitmap = bitmap; 65 mFilename = null; 66 } 67 68 /** 69 * Constructor for an OverlayFrame. This constructor can be used to 70 * restore the overlay after it was saved internally by the video editor. 71 * 72 * @param mediaItem The media item owner 73 * @param overlayId The overlay id 74 * @param filename The file name that contains the overlay. 75 * @param startTimeMs The overlay start time in milliseconds 76 * @param durationMs The overlay duration in milliseconds 77 * 78 * @throws IllegalArgumentException if the file type is not PNG or the 79 * startTimeMs and durationMs are incorrect. 80 */ 81 OverlayFrame(MediaItem mediaItem, String overlayId, String filename, long startTimeMs, 82 long durationMs) { 83 super(mediaItem, overlayId, startTimeMs, durationMs); 84 mFilename = filename; 85 mBitmap = BitmapFactory.decodeFile(mFilename); 86 } 87 88 /** 89 * @return Get the overlay bitmap 90 */ 91 public Bitmap getBitmap() { 92 return mBitmap; 93 } 94 95 /** 96 * @param bitmap The overlay bitmap 97 */ 98 public void setBitmap(Bitmap bitmap) { 99 mBitmap = bitmap; 100 if (mFilename != null) { 101 // Delete the file 102 new File(mFilename).delete(); 103 // Invalidate the filename 104 mFilename = null; 105 } 106 107 // Invalidate the transitions if necessary 108 getMediaItem().invalidateTransitions(mStartTimeMs, mDurationMs); 109 } 110 111 /** 112 * Get the file name of this overlay 113 */ 114 String getFilename() { 115 return mFilename; 116 } 117 118 /** 119 * Save the overlay to the project folder 120 * 121 * @param path The path where the overlay will be saved 122 * 123 * @return The filename 124 * @throws FileNotFoundException if the bitmap cannot be saved 125 * @throws IOException if the bitmap file cannot be saved 126 */ 127 String save(String path) throws FileNotFoundException, IOException { 128 if (mFilename != null) { 129 return mFilename; 130 } 131 132 mFilename = path + "/" + getId() + ".png"; 133 // Save the image to a local file 134 final FileOutputStream out = new FileOutputStream(mFilename); 135 mBitmap.compress(CompressFormat.PNG, 100, out); 136 out.flush(); 137 out.close(); 138 return mFilename; 139 } 140 141 /** 142 * Delete the overlay file 143 */ 144 void invalidate() { 145 if (mFilename != null) { 146 new File(mFilename).delete(); 147 } 148 } 149} 150