如何用java知道swf文件的原始大小(宽度和高度)?

有没有办法用 java知道swf文件的原始宽度和高度?
int width,height;

// my stream contains an image or a swf file
InputStream stream = file.getInputStream();

// mediaType has been set with the help of file extension 
switch (mediaType)
{
    case IMAGE:
        BufferedImage img = ImageIO.read(stream);
        width = img.getWidth();
        height = img.getHeight();
        break;
    case FLASH:
        // what is the code here ?
        break;
}

解决方法

我查看了brooksandrus来源,并且有点不知所措,所以我编写了自己的“一个顶级课程”解决方案.我意识到这是一个长篇大论,但不知道如何以其他方式发布它.
package resnbl.android.swfview;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.zip.DataFormatException;
import java.util.zip.Inflater;

/**
 * This class will read just enough of a SWF file's header to glean the essential
 * meta-data about the animation.
 *
 * This is based on <a href="http://www.adobe.com/devnet/swf.html">
 * SWF File Format Specification (version 10)</a>.
 *
 * @author Resnbl Software
 * @since Mar 22,2011
 */
public class SWFInfo
{
    static final int    UNCOMP_HDR_LEN = 8;     // portion of header that is never compressed

    public boolean      isCompressed;
    public int          version;
    public long         size;
    public int          width,height;
    public float        fps;
    public int          frameCount;

    // Instantiate through getInfo() methods
    private SWFInfo()
    { }

    /**
     * Get the header info for a (potential) SWF file specified by a file path String.
     *
     * @param path  String containing path to file.
     *
     * @return      {@link SWFinfo} object or null if file not found or not SWF.
     */
    public static SWFInfo getInfo(String path)
    {
        return getInfo(new File(path));
    }

    /**
     * Get the header info for a (potential) SWF file specified by a {@link File} object.
     *
     * @param path  {@link File} pointing to the desired SWF file.
     *
     * @return      {@link SWFinfo} object or null if file not found or not SWF.
     */
    public static SWFInfo getInfo(File file)
    {
        SWFInfo     info = new SWFInfo();
        byte[]      hdr = getBytes(file);

        if (hdr == null)
            return null;
        info.isCompressed = hdr[0] == 'C';
        info.version = hdr[3];
        info.size = hdr[4]&0xFF | (hdr[5]&0xFF)<<8 | (hdr[6]&0xFF)<<16 | hdr[7]<<24;

        BitReader rdr = new BitReader(hdr,UNCOMP_HDR_LEN);

        int[] dims = decodeRect(rdr);
        info.width = (dims[1] - dims[0]) / 20;  // convert twips to pixels
        info.height = (dims[3] - dims[2]) / 20;

        info.fps = (float) rdr.uI16() / 256f;   // 8.8 fixed-point format
        info.frameCount = rdr.uI16();

        return info;
    }

    /*
     * Read just enough of the file for our purposes
     */
    private static byte[] getBytes(File file)
    {
        if (file == null || !file.exists() || file.isDirectory())
            return null;

        byte[] bytes = new byte[128];   // should be enough...
        FileInputStream fis = null;

        try
        {
            fis = new FileInputStream(file);

            if (fis.read(bytes) < bytes.length)
                bytes = null;       // too few bytes to be a SWF
            else if (bytes[0] == 'C' && bytes[1] == 'W' && bytes[2] == 'S')
                bytes = expand(bytes,UNCOMP_HDR_LEN);  // compressed SWF
            else if (bytes[0] != 'F' || bytes[1] != 'W' || bytes[2] != 'S')
                bytes = null;       // not a SWF
            // else uncompressed SWF
        }
        catch (IOException e)
        { }
        finally
        {
            if (fis != null)
                try { fis.close(); }
                catch (IOException ee) { }
        }

        return bytes;
    }

    /*
     * All of the file past the initial {@link UNCOMP_HDR_LEN} bytes are compressed.
     * Decompress as much as is in the buffer already read and return them,* overlaying the original uncompressed data.
     *
     * Fortunately,the compression algorithm used by Flash is the ZLIB standard,* i.e.,the same algorithms used to compress .jar files
     */
    private static byte[] expand(byte[] bytes,int skip)
    {
        byte[] newBytes = new byte[bytes.length - skip];
        Inflater inflater = new Inflater();

        inflater.setInput(bytes,skip,newBytes.length);
        try
        {
            int outCount = inflater.inflate(newBytes);
            System.arraycopy(newBytes,bytes,outCount);
            Arrays.fill(bytes,skip + outCount,bytes.length,(byte) 0);
            return bytes;
        }
        catch (DataFormatException e)
        { }

        return null;
    }

    /**
     * Return Stage frame rectangle as 4 <code>int</code>s: LRTB
     *
     * Note the values are in TWIPS (= 1/20th of a pixel)
     *
     * I do this to avoid a loading the <code>Rect</code> class which is an
     * <code>android.graphics</code> class,and not available if you want to
     * test this with desktop Java.
     *
     * @param rdr
     * @return
     */
    public static int[] decodeRect(BitReader rdr)
    {
        int[] dims = new int[4];
        int nBits = rdr.uBits(5);

        dims[0] = rdr.sBits(nBits);     // X min = left     always 0
        dims[1] = rdr.sBits(nBits);     // X max = right
        dims[2] = rdr.sBits(nBits);     // Y min = top      always 0
        dims[3] = rdr.sBits(nBits);     // Y max = bottom

        return dims;
    }

    /**
     * This can be run from a desktop command line sitting at the .../bin directory as:
     *
     * java resnbl.android.swfview.SWFInfo swf_file
     *
     * @param args path to swf_file to parse
     */
// commented out to prevent Eclipse from thinkg this is a standard Java app when used for Android!
//  public static void main(String[] args)
//  {
//      if (args.length == 0)
//          throw new IllegalArgumentException("No swf_file parameter given");
//
//      File file = new File(args[0]);
//      SWFInfo info = SWFInfo.getInfo(file);
//
//      if (info != null)
//      {
//          System.out.println("File: " + file);
//          System.out.println("Flash ver: " + info.version + " FPS: " + info.fps + " Frames: " + info.frameCount);
//          System.out.println("File size: " + file.length() + " Compressed: " + info.isCompressed + " Uncompressed size: " + info.size);
//          System.out.println("Dimensions: " + info.width + "x" + info.height);
//      }
//      else
//          System.out.println("File not a .SWF: " + file);
//  }

    /**
     * Read an arbitrary number of bits from a byte[].
     *
     * This should be turned into a full-featured independant class (someday...).
     */
    static class BitReader
    {
        private byte[]      bytes;
        private int         byteIdx;
        private int         bitIdx = 0;

        /**
         * Start reading from the beginning of the supplied array.
         * @param bytes byte[] to process
         */
        public BitReader(byte[] bytes)
        {
            this(bytes,0);
        }

        /**
         * Start reading from an arbitrary index into the array.
         * @param bytes         byte[] to process
         * @param startIndex    byte # to start at
         */
        public BitReader(byte[] bytes,int startIndex)
        {
            this.bytes = bytes;
            byteIdx = startIndex;
        }

        /**
         * Fetch the next <code>bitCount</code> bits as an unsigned int.
         * @param bitCount  # bits to read
         * @return int
         */
        public int uBits(int bitCount)
        {
            int value = 0;

            while (--bitCount >= 0)
                value = value << 1 | getBit();
            return value;
        }

        /**
         * Fetch the next <code>bitCount</code> bits as a <em>signed</em> int.
         * @param bitCount  # bits to read
         * @return int
         */
        public int sBits(int bitCount)
        {
            // First bit is the "sign" bit
            int value = getBit() == 0 ? 0 : -1;
            --bitCount;

            while (--bitCount >= 0)
                value = value << 1 | getBit();
            return value;
        }

        // Get the next bit in the array
        private int getBit()
        {
            int value = (bytes[byteIdx] >> (7 - bitIdx)) & 0x01;

            if (++bitIdx == 8)
            {
                bitIdx = 0;
                ++byteIdx;
            }

            return value;
        }

        /**
         * Fetch the next 2 "whole" bytes as an unsigned int (little-endian).
         * @return  int
         */
        public int uI16()
        {
            sync();     // back to "byte-aligned" mode
            return (bytes[byteIdx++] & 0xff) | (bytes[byteIdx++] & 0xff) << 8;
        }

        /**
         * Bump indexes to the next byte boundary.
         */
        public void sync()
        {
            if (bitIdx > 0)
            {
                ++byteIdx;
                bitIdx = 0;
            }
        }
    }
}

注意:我用它们的HTML等价物全局替换了尖括号,以使其正确显示.如果你剪切并粘贴它,希望你不必恢复那个改变.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Alt+回车 导入包,自动修正Ctrl+N 查找类Ctrl+Shift+N 查找文件Ctrlʺlt+L 格式化代码Ctrlʺlt+O 优化导入的类和包Alt+Insert 生成代码(如get,set方法,构造函数等)Ctrlʾ或者Alt+Shiftʼ 最近更改的代码Ctrl+R 替换文本Ct
运行程序出现下面错误:HTTP Status 500 ---------------------------------------------------------------------------------type Exception reportmessagedescription Th
1、建立DM的profile,使用的模版在install_root/profileTemplates/dmgr下句法为:manageprofile.sh -create -templatePath install_root/profileTemplates/dmgr调用参数为:-create 建立一
使用dom4j解析XML时,要快速获取某个节点的数据,使用XPath是个不错的方法,dom4j的快速手册里也建议使 用这种方式,标题都写的这么阔气:Powerful Navigation with XPath。 方法是使用Document的selectNodes(String XPath)方法,代码
英文操作系统导致 Debug 下的变量查看时显示乱码,可通过改变字体解决此问题。
eclipse中javascript报错问题处理:三个地方:&lt;1&gt;&quot;eclipse设置 &quot;:Java代码window-&gt;preference-&gt;Validator-&gt;Errors/Warnings-&gt;Enable Javascript Sema
打开eclipse中文字体很小,简直难以辨认。在网上搜索发现这是由于Eclipse 用的字体是 Consolas,显示中文的时候默认太小了。解决方式有两种:一、把字体设置为Courier New操作步骤:打开Elcipse,点击菜单栏上的“Windows”——点击“Preferences”——点击“
如果不加密码,默认只能本机访问,加密码也是为了安全考虑 1.进入Redis&#160;的安装目录,找到redis.conf文件。用vi命令打开文件 输入 / requirepass 进行查找,输入n查找下一个。 (最好复制一个新的conf文件) 在红背景处设置密码 2.重启 Redis &amp;
设置LINUX 自启动: 在/etc/rc.d/rc.local中加入: conf 目录下一个文件&#160;server.xml
ArrayList 和Vector是采用数组方式存储数据,此数组元素数大于实际存储的数据以便增加和插入元素,都允许直接序号索引元素,但是插入数据要设计到数组元素移动等内存操作,所以索引数据快插入数据慢,Vector由于使用了synchronized方法(线程安全)所以性能上比ArrayList要差,
在实现设计模式之前,首先来复习以下UML中的五种关系图 依赖&lt;关联&lt;聚合&lt;组合 &lt;1&gt;依赖 依赖关系用虚线加箭头表示,如图所示: 上图表示:Animal类依赖于Water类(动物依赖于水)。 依赖是类的五种关系中耦合最小的一种关系。因为依赖关系在生成代码的时候,这两个关
第一步:准备包:日志相关包jcl-over-slf4j-1.6.1.jarlogback-classic-0.9.29.jarlogback-core-0.9.29.jarslf4j-api-1.6.1.jarjstl包jstl-1.2.jarspring 相关包org.springframewor
当运行这个web程序时,无法运行,提示错误如下: 当时安装的tomcat是tomcat7版本,安装的jdk版本是1.6。 配置的tomcat如下:window-Preferences-Server-Runtime Environment,添加tomcat。如下: 检查多次,tomcat安装,环境配置
代码中 会让补全,否则会报&#160;diamond operator is not supported in -source 1.5 需要在POM中指定 source 版本号
原因:这是由于jdk的版本与项目的要求不一致造成的,如果是maven项目,首先查看一下pom.xml,以我的项目为例: 从其中可以看出要求的编译插件为1.8版本,而我本机上安装的jdk为1.7版本,因此需要首先下载安装1.8版本的jdk下载链接为 jdk下载链接 然后在intellij idea中点
照着教程弄的第一个 DEMO,结果启不来。 解决办法:在Controller 上面加上&#160;@EnableAutoConfiguration 成功启动 Demo的其它内容及配置如下图,新建一个 空的 Maven 项目 Pom.xml 主界面: Control.java 运行报错 :: Spri
如下图所示,我的是 2018,不同版本,Schema 可能要 Save As一下
Ant Design Pro Vue 打包发布到Tomcat后,刷新报错404解决方法 在应用下面加 WEB-INF&#160;建&#160;web.xml&#160;内容如下 &lt;?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-1&qu
效果如图: JAVA&#160;代码 public static void main(String[] args) throws Exception { String str = &quot;&lt;row PTID=\&quot;80268175\&quot; ZYH=\&quot;2002868
HTTP Status 500 - Handler processing failed; nested exception is java.lang.AbstractMethodError: org.apache.xerces.dom.ElementNSImpl.setUserData(Ljava/