我正在寫一個Adobe Air應用程序。用戶將能夠將視頻導入到應用程序中,但爲了節省應用程序的某些部分的內存,我需要先將這些視頻轉換爲縮略圖.jpgs,並僅在縮略圖中顯示縮略圖。因此這些縮略圖必須在運行時動態生成。所以我的問題是,如何將視頻轉換爲縮略圖。到目前爲止,我嘗試了兩種不同的方法。我已經使用了ImageSnapshot類,但是我只能將視頻添加到顯示列表中,而不是選項。所以然後我試圖使用BitmapData類繪製位圖數據,並遇到同樣的問題。如果我先將視頻添加到顯示列表中,它纔會起作用。有誰知道我怎樣才能從一幀視頻中獲取位圖數據,而無需先將視頻添加到屏幕上?這裏是我現在使用的代碼,它將最終的.jpg作爲test.jpg寫入桌面,並且只能用於.mp4視頻。如果你把上面寫着this.AddElement(VD),它不再適用於線:在Flex/Actionscript中捕獲視頻縮略圖
<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx">
<fx:Script>
<![CDATA[
import mx.graphics.ImageSnapshot;
import mx.graphics.codec.JPEGEncoder;
import spark.components.VideoDisplay;
import spark.primitives.BitmapImage;
private const acceptableTypes:FileFilter = new FileFilter("Media", "*.mp4");
private var vd:VideoDisplay;
protected function importVideo(event:MouseEvent):void
{
var file:File = File.userDirectory;
file.addEventListener(Event.SELECT, mediaSelectHandler);
file.browseForOpen("Select File To Import", [acceptableTypes]);
}
private function mediaSelectHandler(event:Event):void {
vd = new VideoDisplay();
vd.autoPlay = false;
vd.horizontalCenter = 0;
vd.verticalCenter = 0;
vd.source = event.currentTarget.nativePath;
this.addElement(vd);
}
private function getSnapShot(event:MouseEvent):void{
var bd:BitmapData = new BitmapData(vd.width, vd.height);
var matrix:Matrix = new Matrix();
bd.draw(vd, matrix);
//save to hard drive
var file:File = File.desktopDirectory;
var imgfile:File = file.resolvePath("test.jpg");
var jpegEncoder:JPEGEncoder = new JPEGEncoder(90);
var jpegStream:ByteArray = jpegEncoder.encode(bd);
var filestream:FileStream = new FileStream();
filestream.open(imgfile, FileMode.WRITE);
filestream.writeBytes(jpegStream, 0, jpegStream.length);
filestream.close();
}
]]>
</fx:Script>
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<s:HGroup width="500" height="40" horizontalCenter="0" bottom="50">
<s:Button width="100" height="28" label="IMPORT" click="importVideo(event)"/>
<s:Button width="100" height="28" label="SNAPSHOT" click="getSnapShot(event)"/>
</s:HGroup>
</s:WindowedApplication>
請不要在全部首都寫出問題標題。 –