OverlayFrame.java revision 73f376cf6446c607b6f0355ad8282dd47608346d
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 17 18package android.media.videoeditor; 19 20import java.io.File; 21import java.io.FileNotFoundException; 22import java.io.FileOutputStream; 23import java.io.IOException; 24 25import android.graphics.Bitmap; 26import android.graphics.BitmapFactory; 27import android.graphics.Bitmap.CompressFormat; 28 29import java.io.DataOutputStream; 30import java.nio.ByteBuffer; 31import java.nio.IntBuffer; 32 33/** 34 * This class is used to overlay an image on top of a media item. 35 * {@hide} 36 */ 37public class OverlayFrame extends Overlay { 38 /** 39 * Instance variables 40 */ 41 private Bitmap mBitmap; 42 private String mFilename; 43 private String mBitmapFileName; 44 45 private int mOFWidth; 46 private int mOFHeight; 47 48 /** 49 * An object of this type cannot be instantiated by using the default 50 * constructor 51 */ 52 @SuppressWarnings("unused") 53 private OverlayFrame() { 54 this(null, null, (String)null, 0, 0); 55 } 56 57 /** 58 * Constructor for an OverlayFrame 59 * 60 * @param mediaItem The media item owner 61 * @param overlayId The overlay id 62 * @param bitmap The bitmap to be used as an overlay. The size of the 63 * bitmap must equal to the size of the media item to which it is 64 * added. The bitmap is typically a decoded PNG file. 65 * @param startTimeMs The overlay start time in milliseconds 66 * @param durationMs The overlay duration in milliseconds 67 * 68 * @throws IllegalArgumentException if the file type is not PNG or the 69 * startTimeMs and durationMs are incorrect. 70 */ 71 public OverlayFrame(MediaItem mediaItem, String overlayId, Bitmap bitmap, 72 long startTimeMs,long durationMs) { 73 super(mediaItem, overlayId, startTimeMs, durationMs); 74 mBitmap = bitmap; 75 mFilename = null; 76 mBitmapFileName = null; 77 } 78 79 /** 80 * Constructor for an OverlayFrame. This constructor can be used to 81 * restore the overlay after it was saved internally by the video editor. 82 * 83 * @param mediaItem The media item owner 84 * @param overlayId The overlay id 85 * @param filename The file name that contains the overlay. 86 * @param startTimeMs The overlay start time in milliseconds 87 * @param durationMs The overlay duration in milliseconds 88 * 89 * @throws IllegalArgumentException if the file type is not PNG or the 90 * startTimeMs and durationMs are incorrect. 91 */ 92 OverlayFrame(MediaItem mediaItem, String overlayId, String filename, 93 long startTimeMs,long durationMs) { 94 super(mediaItem, overlayId, startTimeMs, durationMs); 95 mBitmapFileName = filename; 96 mBitmap = BitmapFactory.decodeFile(mBitmapFileName); 97 mFilename = null; 98 } 99 100 /** 101 * Get the overlay bitmap. 102 * 103 * @return Get the overlay bitmap 104 */ 105 public Bitmap getBitmap() { 106 return mBitmap; 107 } 108 109 /** 110 * Get the overlay bitmap. 111 * 112 * @return Get the overlay bitmap as png file. 113 */ 114 String getBitmapImageFileName() { 115 return mBitmapFileName; 116 } 117 /** 118 * Set the overlay bitmap. 119 * 120 * @param bitmap The overlay bitmap. 121 */ 122 public void setBitmap(Bitmap bitmap) { 123 mBitmap = bitmap; 124 if (mFilename != null) { 125 /** 126 * Delete the file 127 */ 128 new File(mFilename).delete(); 129 /** 130 * Invalidate the filename 131 */ 132 mFilename = null; 133 } 134 135 /** 136 * Invalidate the transitions if necessary 137 */ 138 getMediaItem().invalidateTransitions(mStartTimeMs, mDurationMs); 139 } 140 141 /** 142 * Get the file name of this overlay 143 */ 144 String getFilename() { 145 return mFilename; 146 } 147 148 /* 149 * Set the file name of this overlay 150 */ 151 void setFilename(String filename) { 152 mFilename = filename; 153 } 154 /** 155 * Save the overlay to the project folder 156 * 157 * @param path The path where the overlay will be saved 158 * 159 * @return The filename 160 * @throws FileNotFoundException if the bitmap cannot be saved 161 * @throws IOException if the bitmap file cannot be saved 162 */ 163 String save(String path) throws FileNotFoundException, IOException { 164 if (mFilename != null) { 165 return mFilename; 166 } 167 168 // Create the compressed PNG file 169 mBitmapFileName = path + "/" + "Overlay" + getId() + ".png"; 170 if (!(new File(mBitmapFileName).exists())) { 171 final FileOutputStream out = new FileOutputStream (mBitmapFileName); 172 mBitmap.compress(CompressFormat.PNG, 100, out); 173 out.flush(); 174 out.close(); 175 } 176 177 mOFWidth = mBitmap.getWidth(); 178 mOFHeight = mBitmap.getHeight(); 179 180 mFilename = path + "/" + "Overlay" + getId() + ".rgb"; 181 if (!(new File(mFilename).exists())) { 182 /** 183 * Save the image to a file ; as a rgb 184 */ 185 final FileOutputStream fl = new FileOutputStream(mFilename); 186 final DataOutputStream dos = new DataOutputStream(fl); 187 188 /** 189 * populate the rgb file with bitmap data 190 */ 191 final int [] framingBuffer = new int[mOFWidth]; 192 ByteBuffer byteBuffer = ByteBuffer.allocate(framingBuffer.length * 4); 193 IntBuffer intBuffer; 194 195 byte[] array = byteBuffer.array(); 196 int tmp = 0; 197 while(tmp < mOFHeight) { 198 mBitmap.getPixels(framingBuffer,0,mOFWidth,0,tmp,mOFWidth,1); 199 intBuffer = byteBuffer.asIntBuffer(); 200 intBuffer.put(framingBuffer,0,mOFWidth); 201 dos.write(array); 202 tmp += 1; 203 } 204 fl.flush(); 205 fl.close(); 206 } 207 return mFilename; 208 } 209 210 /** 211 * Get the OverlayFrame Height 212 */ 213 int getOverlayFrameHeight() { 214 return mOFHeight; 215 } 216 217 /** 218 * Get the OverlayFrame Width 219 */ 220 int getOverlayFrameWidth() { 221 return mOFWidth; 222 } 223 224 /* 225 * Set the OverlayFrame Height 226 */ 227 void setOverlayFrameHeight(int height) { 228 mOFHeight = height; 229 } 230 231 /* 232 * Set the OverlayFrame Width 233 */ 234 void setOverlayFrameWidth(int width) { 235 mOFWidth = width; 236 } 237 /** 238 * Delete the overlay files 239 */ 240 void invalidate() { 241 if (mFilename != null) { 242 new File(mFilename).delete(); 243 mFilename = null; 244 mBitmap.recycle(); 245 mBitmap = null; 246 } 247 if (mBitmapFileName != null) { 248 new File(mBitmapFileName).delete(); 249 mBitmapFileName = null; 250 } 251 } 252} 253