最近接到一个任务,需要将flash图表转化成图片,开始以为很简单。结果google了一下,发现网上关于java操作flash的文章极少,而且大部分是如何来制作flash文件,关于将flash文件转成图片,音频等等的几乎没有。
http://www.flagstonesoftware.com这个网站提供的transform.jar似乎就是最权威的java flash api了,目前已经出来3.0beta1, 我用得是2.3.3。但是我发现他也没提供将flash文件成图片的API. 后来又goole了一圈,还是没有合适的,遂决定对transform.jar 的源代码进行深入研究。因为既然能将图片以一定的方式装进swf文件,那一定也可以以相反的方式转化回来,我只要看看它如何装进去的。
引用这个API一般少不了FSMovie这个类, 发现这个类里有个decodeFromFile(String fileName) 的方法。看注释,这个方法是将swf文件解析成单个的对象,可以是声音,图片,帧,形状等等。因为我只需要图片,那我只需要对图片对象进行处理。经过一番实验,写了下面的代码,目前对jpg图片组成的swf是能够解析的。相信只要稍加处理,应该可以处理更多的类型。
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.zip.DataFormatException;
import javax.imageio.ImageIO;
import com.flagstone.transform.FSDefineJPEGImage2;
import com.flagstone.transform.FSMovie;
import com.flagstone.transform.FSMovieObject;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
public class SWFUtil {
private static final long serialVersionUID = 1069978110917293106L;
public void transformToIMG(String swfFilePath, String imgFilePath)
throws IOException {
FSMovie movie;
try {
movie = new FSMovie(swfFilePath);
} catch (IOException e) {
e.printStackTrace();
return;
} catch (DataFormatException e) {
e.printStackTrace();
return;
}
List<FSMovieObject> oList = movie.getObjects();
for (FSMovieObject o : oList) {
if (o.getType() == FSMovieObject.DefineImage
|| o.getType() == FSMovieObject.DefineImage2
|| o.getType() == FSMovieObject.DefineJPEGImage
|| o.getType() == FSMovieObject.DefineJPEGImage2
|| o.getType() == FSMovieObject.DefineJPEGImage3) {
// System.out.println("catch image.... "+o.getType());
// TODO add more logic to process different image type.
// Currently there is only FSDefineJPEGImage2
BufferedImage bio = ImageIO.read(new ByteArrayInputStream(
((FSDefineJPEGImage2) o).getImage()));
FileOutputStream fos = new FileOutputStream(imgFilePath);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(fos);
encoder.encode(bio);
break;
}
}
}
}
对应的测试类如下(先用jpg图片生成swf,再对这个swf进行解析):
public class SWFUtilTest {
public static final String SWF_FILE = "test/img.swf";
public static final String IMAGE_TARGET_FILE = "test/img.jpg";
public static final String IMAGE_SOURCE_FILE = "test/source.jpg";
private static SWFUtil tool = null;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
tool = new SWFUtil();
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
tool = null;
}
@Test
public final void testTransformToIMG() {
try {
createMovie(IMAGE_SOURCE_FILE, SWF_FILE);
tool.transformToIMG(SWF_FILE, IMAGE_TARGET_FILE);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DataFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void createMovie(String sourceIMG, String targetSWF)
throws DataFormatException, IOException {
FSMovie movie = new FSMovie();
FSImageConstructor imageGenerator = new FSImageConstructor(sourceIMG);
int imageId = movie.newIdentifier();
int shapeId = movie.newIdentifier();
int xOrigin = imageGenerator.getWidth() / 2;
int yOrigin = imageGenerator.getHeight() / 2;
FSDefineObject image = imageGenerator.defineImage(imageId);
FSDefineShape3 shape = imageGenerator.defineEnclosingShape(shapeId,
imageId, -xOrigin, -yOrigin, new FSSolidLine(20, FSColorTable
.black()));
movie.setFrameRate(1.0f);
movie.setFrameSize(shape.getBounds());
movie.add(new FSSetBackgroundColor(FSColorTable.lightblue()));
movie.add(image);
movie.add(shape);
movie.add(new FSPlaceObject2(shapeId, 1, 0, 0));
movie.add(new FSShowFrame());
movie.encodeToFile(targetSWF);
}
}
分享到:
相关推荐
下载后,安装SWFToImage.exe 在安装目录\SWF To Image\Examples有实例 实现swf转图片 目前不支持64位系统。
Convert SWF movie to Image ,采用C#+ASP.NET开发实现。
Convert flash(swf) to JPG, BMP image sequences, TAG, TIFF, PNG image sequences with alpha transparent channel. Key Features: . It could provide the best capability to convert flash(swf) files to ...
Also make sure to copy-paste the .swf, .js and .xml file to your website. Plus your images of course... Licensing The Image Rotator is licensed under a Creative Commons License. It allows you to use...
Add-ons, Magic Swf2Avi 2008 can convert Flash(swf) to JPG, BMP image sequences, TAG, TIFF, PNG image sequences with alpha transparent channel. Update: 2008-08-27 Magic Swf2Avi 2008 build 5.2.7.281 1....
这是一个把其他文件格式转换成FLASH动画文件的工具,包括把word、ppt、pdf、image等等多种不同形式,而且画面的大小、画面质量及播放速度都可以实现设置。重要的是这个软件有册机,非常适于个人实用。
Most of our users want to use our component from a different folder than the location of the HTML page. To see how to embed a component that's in a different folder than your page please read ...
在发行包(SWFUpload v2)中含有2个版本的Flash控件(swfupload_f8.swf 与wfupload_f9.swf),其中第一个版本拥有最佳的兼容性,但是为此损失了部分功能;而第二个版本提供了一些附加的功能但是损失了兼容性。 ...
RXP does this for you, so every image in gallery will be adapted to viewer window size with the correct proportion scaling. * Every image can have its own caption stored in XML file. * RXP uses a ...
28.Flash查看(View)菜单中,Go To→Next表示? A. 打开当前作品的第一个场景 B. 打开当前作品的上一个场景 C. 打开当前作品的下一个场景 D. 打开当前作品的最后一个场景 29.Flash中选择是否合并为单声道的...
what happens to the tooltip when two points are in the same position? you can re-size the charts missing data save the chart as an image You can highlight or emphasize one (or many) points PC ...
- Dropped ID3 reading and auto image search and load (causing App to freeze). v5.0.6a (Sept. 19, 2010) - Updated EXE/APP wrapper (using v4) v 5.0.6 (Nov. 17, 2009) - Fixed Mac browse for cover art ...
Text to SWF 类别:DW Command 把文档中选定的文字转换成SWF文件(奇妙的功能!) 增强DW的ASP+编码功能的插件 · ASP+ Dream 增强DW的ASP+编码功能的插件 Set Alt Text 类别:DW Command 文件格式:mxp 一次...
注:SWF运行于Flash Player 9 (必须下载flash Player 9 ) 6、控件 Button、CheckBox、ColorPicker、DataGrid、DateField、HSlider、HorizontalList、Image、LinkButton Label、List MumericStepper、...
Adding a SWF to the template using swfObject 229 jQuery Flash plugin 230 Passing Flash a WordPress variable 231 Adding sIFR text with the jQuery Flash plugin 233 Flash in a WordPress post or page ...
SWF — Shockwave Flash Functions swish — Swish Functions Sybase — Sybase Functions TCP Wrappers — TCP Wrappers Functions tidy — Tidy Functions Tokenizer — Tokenizer Functions Unicode — Unicode ...
SWF — Shockwave Flash Functions swish — Swish Functions Sybase — Sybase Functions TCP Wrappers — TCP Wrappers Functions tidy — Tidy Functions Tokenizer — Tokenizer Functions Unicode — Unicode ...
".swf"="application/x-shockwave-flash" ".tdf"="application/x-tdf" ".tg4"="application/x-tg4" ".tga"="application/x-tga" ".tif"="image/tiff" ".tif"="application/x-tif" ".tiff"="image/tiff" ".tld"="text...
FLA:Macromedia Flash电影 FND:Microsoft Explorer保存的搜索文件(Find applet) FON:系统字体 FRT:Microsoft FoxPro报表文件 FRX:Visual Basic表单文本;Microsoft FoxPro报表文件 FXP:经Microsoft ...
'-------------------------------------------------------------------- ' Microsoft ADO ' ...' ' ' ' ADO constants include file for VBScript ' '------------------------------------------------------------...