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