`
jackle_liu
  • 浏览: 145700 次
文章分类
社区版块
存档分类
最新评论

Flash 转 图片(swf to image)

阅读更多

  最近接到一个任务,需要将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);
    }
}
 

 

0
0
分享到:
评论
1 楼 诱人的坚持 2014-08-08  
你好 ,FSMovie这个对象是你自己的 还是transform.jar包中的  为什么我下载一个包下来没有这个对象 能否给我解释一下这 我很需要 谢谢

相关推荐

    flash转图片dll(swf转jpg)

    下载后,安装SWFToImage.exe 在安装目录\SWF To Image\Examples有实例 实现swf转图片 目前不支持64位系统。

    基于ASP.NET实现的在线转换Flash格式文件到各种图片程序代码

    Convert SWF movie to Image ,采用C#+ASP.NET开发实现。

    swf视频转换器(支持很多格式)

    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 ...

    flash .swf

    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...

    Magic Swf2Avi 2008

    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....

    ailt-all-document-to-swf-converter

    这是一个把其他文件格式转换成FLASH动画文件的工具,包括把word、ppt、pdf、image等等多种不同形式,而且画面的大小、画面质量及播放速度都可以实现设置。重要的是这个软件有册机,非常适于个人实用。

    flash相册系统

    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- Flash+JS 上传

     在发行包(SWFUpload v2)中含有2个版本的Flash控件(swfupload_f8.swf 与wfupload_f9.swf),其中第一个版本拥有最佳的兼容性,但是为此损失了部分功能;而第二个版本提供了一些附加的功能但是损失了兼容性。 ...

    flash相册,国外免费相册

    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 ...

    flash shiti

    28.Flash查看(View)菜单中,Go To→Next表示? A. 打开当前作品的第一个场景 B. 打开当前作品的上一个场景 C. 打开当前作品的下一个场景 D. 打开当前作品的最后一个场景 29.Flash中选择是否合并为单声道的...

    open-flash-chart-2-ichor.zip

    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 ...

    Rave Player网页视频播放器

    - 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 ...

    34款经典Dreamweaver插件

    Text to SWF 类别:DW Command 把文档中选定的文字转换成SWF文件(奇妙的功能!) 增强DW的ASP+编码功能的插件 · ASP+ Dream 增强DW的ASP+编码功能的插件 Set Alt Text 类别:DW Command 文件格式:mxp 一次...

    Flex课程学习(附带源码)

    注:SWF运行于Flash Player 9 (必须下载flash Player 9 ) 6、控件 Button、CheckBox、ColorPicker、DataGrid、DateField、HSlider、HorizontalList、Image、LinkButton Label、List MumericStepper、...

    WordPress 2.8 Theme Design.pdf

    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 ...

    PHP5 完整官方 中文教程

    SWF — Shockwave Flash Functions swish — Swish Functions Sybase — Sybase Functions TCP Wrappers — TCP Wrappers Functions tidy — Tidy Functions Tokenizer — Tokenizer Functions Unicode — Unicode ...

    PHP5中文参考手册

    SWF — Shockwave Flash Functions swish — Swish Functions Sybase — Sybase Functions TCP Wrappers — TCP Wrappers Functions tidy — Tidy Functions Tokenizer — Tokenizer Functions Unicode — Unicode ...

    文件下载及web文件的contentType类型大全

    ".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...

    网管教程 从入门到精通软件篇.txt

    FLA:Macromedia Flash电影 FND:Microsoft Explorer保存的搜索文件(Find applet) FON:系统字体 FRT:Microsoft FoxPro报表文件 FRX:Visual Basic表单文本;Microsoft FoxPro报表文件 FXP:经Microsoft ...

    网上图书销售数据库+ASP

    '-------------------------------------------------------------------- ' Microsoft ADO ' ...' ' ' ' ADO constants include file for VBScript ' '------------------------------------------------------------...

Global site tag (gtag.js) - Google Analytics